@@ -25,12 +25,13 @@ const (
2525 extendMask = uint64 (0xff00000000000000 )
2626 reservedBitsMask = uint64 (0x00ffff0000000000 )
2727 maxTTL = ^ (extendMask | reservedBitsMask )
28+ historyCount = 10
2829)
2930
3031var (
3132 zkconn = & zkConn {nil }
3233 //store history commands
33- history = make ([]string , 0 )
34+ history = make ([]string , 0 , historyCount )
3435 //default acls when creating nodes in zk
3536 unSafeACL = []zk.ACL {
3637 zk.ACL {Perms : zk .PermAll , Scheme : "world" , ID : "anyone" },
@@ -109,36 +110,37 @@ func isConnected(con *zk.Conn) bool {
109110func loop () {
110111 reader := bufio .NewReader (os .Stdin )
111112 for {
112- command , err := reader .ReadString ('\n' )
113+ line , err := reader .ReadString ('\n' )
113114 if err != nil {
114115 fmt .Println ("Error reading string" , err )
115116 continue
116117 }
117- command = strings .Replace (command , "\n " , "" , - 1 )
118- err = processCommand (zkconn .con , command )
118+ line = strings .Replace (line , "\n " , "" , - 1 )
119+ err = execCmd (zkconn .con , line )
119120 if err != nil {
120121 fmt .Println (err )
121122 }
122123 }
123124}
124125
125- func processCommand (con * zk.Conn , command string ) error {
126- if command == "" {
126+ func execCmd (con * zk.Conn , line string ) error {
127+ if line == "" {
127128 return nil
128129 }
129- cmds := strings .Fields (command )
130+ cmds := strings .Fields (line )
130131 if len (cmds ) <= 0 {
131132 return nil
132133 }
133134
134- addToHistory (command )
135+ defer saveHistory (line )
136+
135137 cmd := cmds [0 ]
136138 args := cmds [1 :]
137139
138140 switch cmd {
139141 case "history" :
140142 count := len (history )
141- for i := count - 10 ; i < count ; i ++ {
143+ for i := count - historyCount ; i < count ; i ++ {
142144 if i < 0 {
143145 continue
144146 }
@@ -331,8 +333,11 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
331333 return nil
332334}
333335
334- func addToHistory (cmd string ) {
336+ func saveHistory (cmd string ) {
335337 history = append (history , cmd )
338+ if len (history ) > historyCount {
339+ history = history [1 : historyCount + 1 ]
340+ }
336341}
337342
338343//TODO: print only supported cmds
0 commit comments