github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/list.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450071060025344>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  	"os"
    18  	"strings"
    19  	"text/tabwriter"
    20  
    21  	"github.com/ethereum/go-ethereum/cmd/utils"
    22  	swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    23  	"gopkg.in/urfave/cli.v1"
    24  )
    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  
    35  func list(ctx *cli.Context) {
    36  	args := ctx.Args()
    37  
    38  	if len(args) < 1 {
    39  		utils.Fatalf("Please supply a manifest reference as the first argument")
    40  	} else if len(args) > 2 {
    41  		utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'")
    42  	}
    43  	manifest := args[0]
    44  
    45  	var prefix string
    46  	if len(args) == 2 {
    47  		prefix = args[1]
    48  	}
    49  
    50  	bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    51  	client := swarm.NewClient(bzzapi)
    52  	list, err := client.List(manifest, prefix, "")
    53  	if err != nil {
    54  		utils.Fatalf("Failed to generate file and directory list: %s", err)
    55  	}
    56  
    57  	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
    58  	defer w.Flush()
    59  	fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH")
    60  	for _, prefix := range list.CommonPrefixes {
    61  		fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix)
    62  	}
    63  	for _, entry := range list.Entries {
    64  		fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path)
    65  	}
    66  }
    67