github.com/XinFinOrg/xdcchain@v1.1.0/cmd/swarm/list.go (about)

     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  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"text/tabwriter"
    24  
    25  	"github.com/ethereum/go-ethereum/cmd/utils"
    26  	swarm "github.com/ethereum/go-ethereum/swarm/api/client"
    27  	"gopkg.in/urfave/cli.v1"
    28  )
    29  
    30  var listCommand = cli.Command{
    31  	Action:             list,
    32  	CustomHelpTemplate: helpTemplate,
    33  	Name:               "ls",
    34  	Usage:              "list files and directories contained in a manifest",
    35  	ArgsUsage:          "<manifest> [<prefix>]",
    36  	Description:        "Lists files and directories contained in a manifest",
    37  }
    38  
    39  func list(ctx *cli.Context) {
    40  	args := ctx.Args()
    41  
    42  	if len(args) < 1 {
    43  		utils.Fatalf("Please supply a manifest reference as the first argument")
    44  	} else if len(args) > 2 {
    45  		utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'")
    46  	}
    47  	manifest := args[0]
    48  
    49  	var prefix string
    50  	if len(args) == 2 {
    51  		prefix = args[1]
    52  	}
    53  
    54  	bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    55  	client := swarm.NewClient(bzzapi)
    56  	list, err := client.List(manifest, prefix, "")
    57  	if err != nil {
    58  		utils.Fatalf("Failed to generate file and directory list: %s", err)
    59  	}
    60  
    61  	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
    62  	defer w.Flush()
    63  	fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH")
    64  	for _, prefix := range list.CommonPrefixes {
    65  		fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix)
    66  	}
    67  	for _, entry := range list.Entries {
    68  		fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path)
    69  	}
    70  }