@@ -186,7 +186,6 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
186186 printChildren (children , stat )
187187 }
188188 }
189- break
190189 case "get" :
191190 get , err := cmdParser .ParseGet (args )
192191 if err != nil {
@@ -204,7 +203,6 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
204203 if get .WithStat {
205204 printStat (stat )
206205 }
207- break
208206 case "stat" :
209207 statCmd , err := cmdParser .ParseStat (args )
210208 if err != nil {
@@ -218,7 +216,6 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
218216 return fmt .Errorf ("Node does not exist: %s" , statCmd .Path )
219217 }
220218 printStat (stat )
221- break
222219 case "set" :
223220 set , err := cmdParser .ParseSet (args )
224221 if err != nil {
@@ -231,7 +228,6 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
231228 if set .WithStat {
232229 printStat (stat )
233230 }
234- break
235231 case "create" :
236232 create , err := cmdParser .ParseCreate (args )
237233 if err != nil {
@@ -286,20 +282,20 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
286282 return err
287283 }
288284 fmt .Println ("Created" , s )
289- break
290285 case "getAcl" :
291- fmt .Println ("Not supported yet" )
292- break
286+ fallthrough
293287 case "setAcl" :
294- fmt .Println ("Not supported yet" )
295- break
288+ fallthrough
296289 case "setquota" :
290+ fallthrough
297291 case "listquota" :
292+ fallthrough
298293 case "delquota" :
294+ fallthrough
299295 case "addauth" :
296+ fallthrough
300297 case "config" :
301298 fmt .Println ("Not supported yet" )
302- break
303299 case "sync" :
304300 if len (args ) != 1 {
305301 return fmt .Errorf ("sync path" )
@@ -309,7 +305,6 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
309305 return err
310306 }
311307 fmt .Println ("Sync is OK" )
312- break
313308 case "delete" :
314309 del , err := cmdParser .ParseDelete (args )
315310 if err != nil {
@@ -318,17 +313,17 @@ func execZkCmd(con *zk.Conn, cmd string, args []string) error {
318313 if err := con .Delete (del .Path , del .Version ); err != nil {
319314 return err
320315 }
321- break
322316 case "deleteall" :
323- fmt .Println ("Not supported yet" )
324- break
317+ da , err := cmdParser .ParseDeleteAll (args )
318+ if err != nil {
319+ return err
320+ }
321+ return deleteRecursive (zkconn .con , da .Path )
325322 case "close" :
326323 con .Close ()
327- break
328324 default :
329325 printUsage ()
330326 fmt .Println ("Command not found: Command not found" , cmd )
331- break
332327 }
333328 return nil
334329}
@@ -340,34 +335,60 @@ func saveHistory(cmd string) {
340335 }
341336}
342337
343- //TODO: print only supported cmds
344338func printUsage () {
345- fmt .Println (`ZooKeeper -server host:port cmd args
346- addauth scheme auth
347- close
348- config [-c] [-w] [-s]
349- connect host:port
350- create [-s] [-e] [-c] [-t ttl] path [data] [acl]
351- delete [-v version] path
352- deleteall path
353- delquota [-n|-b] path
354- get [-s] [-w] path
355- getAcl [-s] path
356- history
357- listquota path
358- ls [-s] [-w] [-R] path
359- ls2 path [watch]
360- printwatches on|off
361- quit
362- reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
363- redo cmdno
364- removewatches path [-c|-d|-a] [-l]
365- rmr path
366- set [-s] [-v version] path data
367- setAcl [-s] [-v version] [-R] path acl
368- setquota -n|-b val path
369- stat [-w] path
370- sync path` )
339+ fmt .Println ("ZooKeeper -server host:port cmd args" )
340+ fmt .Println ("\t \t connect host:port" )
341+ fmt .Println ("\t \t history" )
342+ fmt .Println ("\t \t quit" )
343+ for _ , c := range cmdParser .SupportedCmds {
344+ fmt .Println ("\t \t " , c .Usage ())
345+ }
346+ }
347+
348+ func deleteRecursive (con * zk.Conn , path string ) error {
349+ err := util .ValidatePath (path )
350+ if err != nil {
351+ return err
352+ }
353+
354+ paths := listSubTreeBFS (con , path )
355+
356+ deleteReqs := make ([]interface {}, 0 )
357+ total := len (paths )
358+ for i := total - 1 ; i >= 0 ; i -- {
359+ p := paths [i ]
360+ deleteReqs = append (deleteReqs , & zk.DeleteRequest {Path : p , Version : - 1 })
361+ }
362+
363+ if _ , err := con .Multi (deleteReqs ... ); err != nil {
364+ return err
365+ }
366+ return nil
367+ }
368+
369+ func listSubTreeBFS (con * zk.Conn , pathRoot string ) []string {
370+ queue := make ([]string , 0 )
371+ tree := make ([]string , 0 )
372+
373+ queue = append (queue , pathRoot )
374+ tree = append (tree , pathRoot )
375+
376+ for len (queue ) > 0 {
377+ node := queue [0 ]
378+ queue = queue [1 :]
379+ children , _ , err := con .Children (node )
380+ if err != nil {
381+ log .Println (err )
382+ continue
383+ }
384+ for _ , child := range children {
385+ childPath := node + "/" + child
386+ queue = append (queue , childPath )
387+ tree = append (tree , childPath )
388+ }
389+ }
390+
391+ return tree
371392}
372393
373394func visitSubTree (con * zk.Conn , path string ) {
0 commit comments