code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/paths/list.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package paths
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"sort"
    22  	"text/tabwriter"
    23  
    24  	"code.vegaprotocol.io/vega/core/config"
    25  	"code.vegaprotocol.io/vega/paths"
    26  )
    27  
    28  type ListCmd struct {
    29  	config.VegaHomeFlag
    30  }
    31  
    32  func (opts *ListCmd) Execute(_ []string) error {
    33  	vegaPaths := paths.New(opts.VegaHome)
    34  
    35  	allPaths := paths.List(vegaPaths)
    36  
    37  	if err := printTable("Cache", allPaths.CachePaths); err != nil {
    38  		return fmt.Errorf("couldn't print cache paths table: %w", err)
    39  	}
    40  	fmt.Print("\n\n")
    41  	if err := printTable("Config", allPaths.ConfigPaths); err != nil {
    42  		return fmt.Errorf("couldn't print config paths table: %w", err)
    43  	}
    44  	fmt.Print("\n\n")
    45  	if err := printTable("Data", allPaths.DataPaths); err != nil {
    46  		return fmt.Errorf("couldn't print data paths table: %w", err)
    47  	}
    48  	fmt.Print("\n\n")
    49  	if err := printTable("State", allPaths.StatePaths); err != nil {
    50  		return fmt.Errorf("couldn't print state paths table: %w", err)
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func printTable(role string, paths map[string]string) error {
    57  	sortedPaths := make([]struct {
    58  		name string
    59  		path string
    60  	}, len(paths))
    61  
    62  	var i uint64
    63  	for name, path := range paths {
    64  		sortedPaths[i] = struct {
    65  			name string
    66  			path string
    67  		}{
    68  			name: name,
    69  			path: path,
    70  		}
    71  		i++
    72  	}
    73  
    74  	sort.SliceStable(sortedPaths, func(i, j int) bool {
    75  		return sortedPaths[i].path < sortedPaths[j].path
    76  	})
    77  
    78  	w := new(tabwriter.Writer)
    79  	w.Init(os.Stdout, 8, 8, 1, '\t', 0)
    80  
    81  	_, _ = fmt.Fprintf(w, "\n  %s\t%s\t", "NAME", "PATH")
    82  	for _, path := range sortedPaths {
    83  		_, _ = fmt.Fprintf(w, "\n  %s\t%s\t", path.name, path.path)
    84  	}
    85  
    86  	fmt.Printf("# %s paths\n\n", role)
    87  	if err := w.Flush(); err != nil {
    88  		return fmt.Errorf("couldn't flush paths table: %w", err)
    89  	}
    90  
    91  	return nil
    92  }