list.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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-ethereum 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-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "strings"
  21. "text/tabwriter"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  24. "gopkg.in/urfave/cli.v1"
  25. )
  26. func list(ctx *cli.Context) {
  27. args := ctx.Args()
  28. if len(args) < 1 {
  29. utils.Fatalf("Please supply a manifest reference as the first argument")
  30. } else if len(args) > 2 {
  31. utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'")
  32. }
  33. manifest := args[0]
  34. var prefix string
  35. if len(args) == 2 {
  36. prefix = args[1]
  37. }
  38. bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
  39. client := swarm.NewClient(bzzapi)
  40. list, err := client.List(manifest, prefix)
  41. if err != nil {
  42. utils.Fatalf("Failed to generate file and directory list: %s", err)
  43. }
  44. w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
  45. defer w.Flush()
  46. fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH")
  47. for _, prefix := range list.CommonPrefixes {
  48. fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix)
  49. }
  50. for _, entry := range list.Entries {
  51. fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path)
  52. }
  53. }