@cryptotaxi247 / kubo / commits / 07008042f

ipfs ls: added --headers option

- added tests for 'ipfs ls --headers' - comments from CR (opts) - sharness: fix ls test whitespace

Henry committed Mar 20, 2015 at 15:46 UTC 07008042f1e6aaeb0b047d8cd3536646dd0f337e
2 files changed +43 -6
core/commands/ls.go
+18 -5
@@ -42,6 +42,9 @@ it contains, with the following format:
42 Arguments: []cmds.Argument{
43 cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from").EnableStdin(),
44 },
45 + Options: []cmds.Option{
46 + cmds.BoolOption("headers", "", "Print table headers (Hash, Name, Size)"),
47 + },
48 Run: func(req cmds.Request, res cmds.Response) {
49 node, err := req.Context().GetNode()
50 if err != nil {
@@ -49,6 +52,12 @@ it contains, with the following format:
52 return
53 }
54
55 + // get options early -> exit early in case of error
56 + if _, _, err := req.Option("headers").Bool(); err != nil {
57 + res.SetError(err, cmds.ErrNormal)
58 + return
59 + }
60 +
61 paths := req.Arguments()
62
63 dagnodes := make([]*merkledag.Node, 0)
@@ -91,21 +100,25 @@ it contains, with the following format:
100 },
101 Marshalers: cmds.MarshalerMap{
102 cmds.Text: func(res cmds.Response) (io.Reader, error) {
94 - output := res.Output().(*LsOutput).Objects
103 +
104 + headers, _, _ := res.Request().Option("headers").Bool()
105 + output := res.Output().(*LsOutput)
106 var buf bytes.Buffer
107 w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
97 - for _, object := range output {
98 - if len(output) > 1 {
108 + for _, object := range output.Objects {
109 + if len(output.Objects) > 1 {
110 fmt.Fprintf(w, "%s:\n", object.Hash)
111 }
101 - fmt.Fprintln(w, "Hash\tSize\tName\t")
112 + if headers {
113 + fmt.Fprintln(w, "Hash\tSize\tName\t")
114 + }
115 for _, link := range object.Links {
116 if link.Type == unixfspb.Data_Directory {
117 link.Name += "/"
118 }
119 fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
120 }
108 - if len(output) > 1 {
121 + if len(output.Objects) > 1 {
122 fmt.Fprintln(w)
123 }
124 }
test/sharness/t0045-ls.sh
+25 -1
@@ -42,6 +42,30 @@ test_expect_success "'ipfs ls <three dir hashes>' succeeds" '
42
43 test_expect_success "'ipfs ls <three dir hashes>' output looks good" '
44 cat <<-\EOF >expected_ls &&
45 + QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
46 + QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
47 + QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy 1143 d2/
48 + QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH 13 f1
49 + QmNtocSs7MoDkJMc1RkyisCSKvLadujPsfJfSdJ3e1eA1M 13 f2
50 +
51 + QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy:
52 + QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd 1035 1024
53 + QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 14 a
54 +
55 + QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
56 + QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
57 + QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
58 +
59 + EOF
60 + test_cmp expected_ls actual_ls
61 +'
62 +
63 +test_expect_success "'ipfs ls --headers <three dir hashes>' succeeds" '
64 + ipfs ls --headers QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls_headers
65 +'
66 +
67 +test_expect_success "'ipfs ls --headers <three dir hashes>' output looks good" '
68 + cat <<-\EOF >expected_ls_headers &&
69 QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
70 Hash Size Name
71 QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
@@ -60,7 +84,7 @@ test_expect_success "'ipfs ls <three dir hashes>' output looks good" '
84 QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
85
86 EOF
63 - test_cmp expected_ls actual_ls
87 + test_cmp expected_ls_headers actual_ls_headers
88 '
89
90 test_done