github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/swarm/list.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  	"os"
    17  	"strings"
    18  	"text/tabwriter"
    19  
    20  	"github.com/Sberex/go-sberex/cmd/utils"
    21  	swarm "github.com/Sberex/go-sberex/swarm/api/client"
    22  	"gopkg.in/urfave/cli.v1"
    23  )
    24  
    25  func list(ctx *cli.Context) {
    26  	args := ctx.Args()
    27  
    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  
    35  	var prefix string
    36  	if len(args) == 2 {
    37  		prefix = args[1]
    38  	}
    39  
    40  	bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    41  	client := swarm.NewClient(bzzapi)
    42  	list, err := client.List(manifest, prefix)
    43  	if err != nil {
    44  		utils.Fatalf("Failed to generate file and directory list: %s", err)
    45  	}
    46  
    47  	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
    48  	defer w.Flush()
    49  	fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH")
    50  	for _, prefix := range list.CommonPrefixes {
    51  		fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix)
    52  	}
    53  	for _, entry := range list.Entries {
    54  		fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path)
    55  	}
    56  }