/
ipfs_list_files.go
60 lines (50 loc) · 1.56 KB
/
ipfs_list_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"context"
"fmt"
"github.com/alexflint/go-arg"
ipfs "github.com/ipfs/go-ipfs-api"
)
var (
sh *ipfs.Shell
args struct {
APIServer string `arg:"-a, --api" help:"the API endpoint URL for your IPFS" default:"127.1:5001"`
Path string `arg:"-p, --path" help:"path to show listing for" default:"/"`
}
)
func byteCountFromDecimal(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
func main() {
arg.MustParse(&args)
sh = ipfs.NewShell(args.APIServer)
opts := func(rb *ipfs.RequestBuilder) error {
rb.Option("long", true)
return nil
}
// list files
files, err := sh.FilesLs(context.Background(), args.Path, opts)
if err != nil {
fmt.Println(err)
return
}
for _, file := range files {
var file_type string
if file.Type == 1 {
file_type = "D"
} else {
file_type = "F"
}
fmtSize := byteCountFromDecimal(file.Size)
fmt.Printf("[%s] %s:%s, size: %s\n", file_type, file.Name, file.Hash, fmtSize)
}
}