list.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. var listCommand = cli.Command{
  27. Action: list,
  28. CustomHelpTemplate: helpTemplate,
  29. Name: "ls",
  30. Usage: "list files and directories contained in a manifest",
  31. ArgsUsage: "<manifest> [<prefix>]",
  32. Description: "Lists files and directories contained in a manifest",
  33. }
  34. func list(ctx *cli.Context) {
  35. args := ctx.Args()
  36. if len(args) < 1 {
  37. utils.Fatalf("Please supply a manifest reference as the first argument")
  38. } else if len(args) > 2 {
  39. utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'")
  40. }
  41. manifest := args[0]
  42. var prefix string
  43. if len(args) == 2 {
  44. prefix = args[1]
  45. }
  46. bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
  47. client := swarm.NewClient(bzzapi)
  48. list, err := client.List(manifest, prefix, "")
  49. if err != nil {
  50. utils.Fatalf("Failed to generate file and directory list: %s", err)
  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. }