github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/swarm/list.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of Spectrum.
     3  //
     4  // Spectrum 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  // Spectrum 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 Spectrum. 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/SmartMeshFoundation/Spectrum/cmd/utils"
    26  	swarm "github.com/SmartMeshFoundation/Spectrum/swarm/api/client"
    27  	"gopkg.in/urfave/cli.v1"
    28  )
    29  
    30  func list(ctx *cli.Context) {
    31  	args := ctx.Args()
    32  
    33  	if len(args) < 1 {
    34  		utils.Fatalf("Please supply a manifest reference as the first argument")
    35  	} else if len(args) > 2 {
    36  		utils.Fatalf("Too many arguments - usage 'swarm ls manifest [prefix]'")
    37  	}
    38  	manifest := args[0]
    39  
    40  	var prefix string
    41  	if len(args) == 2 {
    42  		prefix = args[1]
    43  	}
    44  
    45  	bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
    46  	client := swarm.NewClient(bzzapi)
    47  	list, err := client.List(manifest, prefix)
    48  	if err != nil {
    49  		utils.Fatalf("Failed to generate file and directory list: %s", err)
    50  	}
    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  }