提交 dfb81abf 作者: Lucas Molas

commands: files ls: sort output

Imitate Unix `ls` command, sort by default; disable with `-U` flag.

License: MIT
Signed-off-by: 's avatarLucas Molas <schomatis@gmail.com>
上级 10201db1
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"io" "io"
"os" "os"
gopath "path" gopath "path"
"sort"
"strings" "strings"
bservice "github.com/ipfs/go-ipfs/blockservice" bservice "github.com/ipfs/go-ipfs/blockservice"
...@@ -405,6 +406,7 @@ Examples: ...@@ -405,6 +406,7 @@ Examples:
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption("l", "Use long listing format."), cmdkit.BoolOption("l", "Use long listing format."),
cmdkit.BoolOption("U", "Do not sort; list entries in directory order."),
}, },
Run: func(req oldcmds.Request, res oldcmds.Response) { Run: func(req oldcmds.Request, res oldcmds.Response) {
var arg string var arg string
...@@ -482,8 +484,15 @@ Examples: ...@@ -482,8 +484,15 @@ Examples:
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
long, _, _ := res.Request().Option("l").Bool()
noSort, _, _ := res.Request().Option("U").Bool()
if !noSort {
sort.Slice(out.Entries, func(i, j int) bool {
return strings.Compare(out.Entries[i].Name, out.Entries[j].Name) < 0
})
}
long, _, _ := res.Request().Option("l").Bool()
for _, o := range out.Entries { for _, o := range out.Entries {
if long { if long {
fmt.Fprintf(buf, "%s\t%s\t%d\n", o.Name, o.Hash, o.Size) fmt.Fprintf(buf, "%s\t%s\t%d\n", o.Name, o.Hash, o.Size)
......
...@@ -56,19 +56,29 @@ test_sharding() { ...@@ -56,19 +56,29 @@ test_sharding() {
test_expect_success "can make 100 files in a directory $EXTRA" ' test_expect_success "can make 100 files in a directory $EXTRA" '
printf "" > list_exp_raw printf "" > list_exp_raw
for i in `seq 100` for i in `seq 100 -1 1`
do do
echo $i | ipfs files write --create /foo/file$i || return 1 echo $i | ipfs files write --create /foo/file$i || return 1
echo file$i >> list_exp_raw echo file$i >> list_exp_raw
done done
' '
# Create the files in reverse (unsorted) order (`seq 100 -1 1`)
# to check the sort in the `ipfs files ls` command. `ProtoNode`
# links are always sorted at the DAG layer so the sorting feature
# is tested with sharded directories.
test_expect_success "listing works $EXTRA" ' test_expect_success "sorted listing works $EXTRA" '
ipfs files ls /foo |sort > list_out && ipfs files ls /foo > list_out &&
sort list_exp_raw > list_exp && sort list_exp_raw > list_exp &&
test_cmp list_exp list_out test_cmp list_exp list_out
' '
test_expect_success "unsorted listing works $EXTRA" '
ipfs files ls -U /foo > list_out &&
sort list_exp_raw > sort_list_not_exp &&
! test_cmp sort_list_not_exp list_out
'
test_expect_success "can read a file from sharded directory $EXTRA" ' test_expect_success "can read a file from sharded directory $EXTRA" '
ipfs files read /foo/file65 > file_out && ipfs files read /foo/file65 > file_out &&
echo "65" > file_exp && echo "65" > file_exp &&
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论