Skip to content

Commit

Permalink
ipfs ls: added --headers option
Browse files Browse the repository at this point in the history
- added tests for 'ipfs ls --headers'
- comments from CR (opts)
- sharness: fix ls test whitespace
  • Loading branch information
cryptix authored and jbenet committed Mar 20, 2015
1 parent c0f74b9 commit 0700804
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
23 changes: 18 additions & 5 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ it contains, with the following format:
Arguments: []cmds.Argument{
cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from").EnableStdin(),
},
Options: []cmds.Option{
cmds.BoolOption("headers", "", "Print table headers (Hash, Name, Size)"),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

// get options early -> exit early in case of error
if _, _, err := req.Option("headers").Bool(); err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

paths := req.Arguments()

dagnodes := make([]*merkledag.Node, 0)
Expand Down Expand Up @@ -91,21 +100,25 @@ it contains, with the following format:
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
output := res.Output().(*LsOutput).Objects

headers, _, _ := res.Request().Option("headers").Bool()
output := res.Output().(*LsOutput)
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
for _, object := range output {
if len(output) > 1 {
for _, object := range output.Objects {
if len(output.Objects) > 1 {
fmt.Fprintf(w, "%s:\n", object.Hash)
}
fmt.Fprintln(w, "Hash\tSize\tName\t")
if headers {
fmt.Fprintln(w, "Hash\tSize\tName\t")
}
for _, link := range object.Links {
if link.Type == unixfspb.Data_Directory {
link.Name += "/"
}
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
}
if len(output) > 1 {
if len(output.Objects) > 1 {
fmt.Fprintln(w)
}
}
Expand Down
26 changes: 25 additions & 1 deletion test/sharness/t0045-ls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ test_expect_success "'ipfs ls <three dir hashes>' succeeds" '

test_expect_success "'ipfs ls <three dir hashes>' output looks good" '
cat <<-\EOF >expected_ls &&
QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy 1143 d2/
QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH 13 f1
QmNtocSs7MoDkJMc1RkyisCSKvLadujPsfJfSdJ3e1eA1M 13 f2
QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy:
QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd 1035 1024
QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 14 a
QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls actual_ls
'

test_expect_success "'ipfs ls --headers <three dir hashes>' succeeds" '
ipfs ls --headers QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss >actual_ls_headers
'

test_expect_success "'ipfs ls --headers <three dir hashes>' output looks good" '
cat <<-\EOF >expected_ls_headers &&
QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj:
Hash Size Name
QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss 246 d1/
Expand All @@ -60,7 +84,7 @@ test_expect_success "'ipfs ls <three dir hashes>' output looks good" '
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls actual_ls
test_cmp expected_ls_headers actual_ls_headers
'

test_done

0 comments on commit 0700804

Please sign in to comment.