@cryptotaxi247 / kubo / commits / c9733c5da

core/commands/unixfs/ls: Set Argument in JSON output

Change the approach to the directory-header control so we can set the Argument value in the JSON response. Stripping the trailing newline from the JSON output is annoying, but looking over [1] I saw no easy way to add a newline to the JSON output. And with the general framework that commands/ attempts to be, it feels a bit funny to customize the JSON output for a command-line program. Perhaps a workable solution is to have the command-line client append newlines to any output that otherwise lacks them? But that seems like a change best left to a separate series. [1]: http://golang.org/pkg/encoding/json/ License: MIT Signed-off-by: W. Trevor King <wking@tremily.us>

W. Trevor King committed Jun 9, 2015 at 15:43 UTC c9733c5da78dc0c04f6c5b95c5504ee3ae936bc9
2 files changed +30 -3
core/commands/unixfs/ls.go
+4 -3
@@ -72,7 +72,7 @@ directories, the child size is the IPFS link size.
72 return
73 }
74
75 - output[i] = &LsObject{}
75 + output[i] = &LsObject{Argument: fpath}
76
77 t := unixFSNode.GetType()
78 switch t {
@@ -92,7 +92,6 @@ directories, the child size is the IPFS link size.
92 Size: unixFSNode.GetFilesize(),
93 }}
94 case unixfspb.Data_Directory:
95 - output[i].Argument = fpath
95 output[i].Links = make([]LsLink, len(merkleNode.Links))
96 for j, link := range merkleNode.Links {
97 getCtx, cancel := context.WithTimeout(context.TODO(), time.Minute)
@@ -132,7 +131,9 @@ directories, the child size is the IPFS link size.
131 w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
132 lastObjectDirHeader := false
133 for i, object := range output.Objects {
135 - if len(output.Objects) > 1 && object.Argument != "" {
134 + singleObject := (len(object.Links) == 1 &&
135 + object.Links[0].Name == object.Argument)
136 + if len(output.Objects) > 1 && !singleObject {
137 if i > 0 {
138 fmt.Fprintln(w)
139 }
test/sharness/t0200-unixfs-ls.sh
+26
@@ -72,6 +72,32 @@ test_ls_cmd() {
72 EOF
73 test_cmp expected_ls_file actual_ls_file
74 '
75 +
76 + test_expect_success "'ipfs --encoding=json file ls <file hashes>' succeeds" '
77 + ipfs --encoding=json file ls /ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024 >actual_json_ls_file
78 + '
79 +
80 + test_expect_success "'ipfs --encoding=json file ls <file hashes>' output looks good" '
81 + cat <<-\EOF >expected_json_ls_file_trailing_newline &&
82 + {
83 + "Objects": [
84 + {
85 + "Argument": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
86 + "Links": [
87 + {
88 + "Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
89 + "Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
90 + "Size": 1024,
91 + "Type": 2
92 + }
93 + ]
94 + }
95 + ]
96 + }
97 + EOF
98 + printf %s "$(cat expected_json_ls_file_trailing_newline)" >expected_json_ls_file &&
99 + test_cmp expected_json_ls_file actual_json_ls_file
100 + '
101 }
102
103