github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/swarm/list.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-wtc. 3 // 4 // go-wtc is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-wtc is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-wtc. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "os" 22 "strings" 23 "text/tabwriter" 24 25 "github.com/wtc/go-wtc/cmd/utils" 26 swarm "github.com/wtc/go-wtc/swarm/api/client" 27 "gopkg.in/urfave/cli.v1" 28 ) 29 30 func list(ctx *cli.Context) { 31 args := ctx.Args() 32 33 if len(args) < 1 { 34 utils.Fatalf("Please supply a manifest reference as the first argument") 35 } else if len(args) > 2 { 36 utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'") 37 } 38 manifest := args[0] 39 40 var prefix string 41 if len(args) == 2 { 42 prefix = args[1] 43 } 44 45 bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") 46 client := swarm.NewClient(bzzapi) 47 list, err := client.List(manifest, prefix) 48 if err != nil { 49 utils.Fatalf("Failed to generate file and directory list: %s", err) 50 } 51 52 w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0) 53 defer w.Flush() 54 fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH") 55 for _, prefix := range list.CommonPrefixes { 56 fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix) 57 } 58 for _, entry := range list.Entries { 59 fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path) 60 } 61 }