ls_command.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "github.com/urfave/cli"
  18. "go.etcd.io/etcd/client"
  19. )
  20. func NewLsCommand() cli.Command {
  21. return cli.Command{
  22. Name: "ls",
  23. Usage: "retrieve a directory",
  24. ArgsUsage: "[key]",
  25. Flags: []cli.Flag{
  26. cli.BoolFlag{Name: "sort", Usage: "returns result in sorted order"},
  27. cli.BoolFlag{Name: "recursive, r", Usage: "returns all key names recursively for the given path"},
  28. cli.BoolFlag{Name: "p", Usage: "append slash (/) to directories"},
  29. cli.BoolFlag{Name: "quorum, q", Usage: "require quorum for get request"},
  30. },
  31. Action: func(c *cli.Context) error {
  32. lsCommandFunc(c, mustNewKeyAPI(c))
  33. return nil
  34. },
  35. }
  36. }
  37. // lsCommandFunc executes the "ls" command.
  38. func lsCommandFunc(c *cli.Context, ki client.KeysAPI) {
  39. key := "/"
  40. if len(c.Args()) != 0 {
  41. key = c.Args()[0]
  42. }
  43. sort := c.Bool("sort")
  44. recursive := c.Bool("recursive")
  45. quorum := c.Bool("quorum")
  46. ctx, cancel := contextWithTotalTimeout(c)
  47. resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sort, Recursive: recursive, Quorum: quorum})
  48. cancel()
  49. if err != nil {
  50. handleError(c, ExitServerError, err)
  51. }
  52. printLs(c, resp)
  53. }
  54. // printLs writes a response out in a manner similar to the `ls` command in unix.
  55. // Non-empty directories list their contents and files list their name.
  56. func printLs(c *cli.Context, resp *client.Response) {
  57. if c.GlobalString("output") == "simple" {
  58. if !resp.Node.Dir {
  59. fmt.Println(resp.Node.Key)
  60. }
  61. for _, node := range resp.Node.Nodes {
  62. rPrint(c, node)
  63. }
  64. } else {
  65. // user wants JSON or extended output
  66. printResponseKey(resp, c.GlobalString("output"))
  67. }
  68. }
  69. // rPrint recursively prints out the nodes in the node structure.
  70. func rPrint(c *cli.Context, n *client.Node) {
  71. if n.Dir && c.Bool("p") {
  72. fmt.Println(fmt.Sprintf("%v/", n.Key))
  73. } else {
  74. fmt.Println(n.Key)
  75. }
  76. for _, node := range n.Nodes {
  77. rPrint(c, node)
  78. }
  79. }