Commit c9733c5d authored by W. Trevor King's avatar W. Trevor King

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: default avatarW. Trevor King <wking@tremily.us>
parent 3e6905e8
......@@ -72,7 +72,7 @@ directories, the child size is the IPFS link size.
return
}
output[i] = &LsObject{}
output[i] = &LsObject{Argument: fpath}
t := unixFSNode.GetType()
switch t {
......@@ -92,7 +92,6 @@ directories, the child size is the IPFS link size.
Size: unixFSNode.GetFilesize(),
}}
case unixfspb.Data_Directory:
output[i].Argument = fpath
output[i].Links = make([]LsLink, len(merkleNode.Links))
for j, link := range merkleNode.Links {
getCtx, cancel := context.WithTimeout(context.TODO(), time.Minute)
......@@ -132,7 +131,9 @@ directories, the child size is the IPFS link size.
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
lastObjectDirHeader := false
for i, object := range output.Objects {
if len(output.Objects) > 1 && object.Argument != "" {
singleObject := (len(object.Links) == 1 &&
object.Links[0].Name == object.Argument)
if len(output.Objects) > 1 && !singleObject {
if i > 0 {
fmt.Fprintln(w)
}
......
......@@ -72,6 +72,32 @@ test_ls_cmd() {
EOF
test_cmp expected_ls_file actual_ls_file
'
test_expect_success "'ipfs --encoding=json file ls <file hashes>' succeeds" '
ipfs --encoding=json file ls /ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024 >actual_json_ls_file
'
test_expect_success "'ipfs --encoding=json file ls <file hashes>' output looks good" '
cat <<-\EOF >expected_json_ls_file_trailing_newline &&
{
"Objects": [
{
"Argument": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
"Links": [
{
"Name": "/ipfs/QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy/1024",
"Hash": "QmbQBUSRL9raZtNXfpTDeaxQapibJEG6qEY8WqAN22aUzd",
"Size": 1024,
"Type": 2
}
]
}
]
}
EOF
printf %s "$(cat expected_json_ls_file_trailing_newline)" >expected_json_ls_file &&
test_cmp expected_json_ls_file actual_json_ls_file
'
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment