code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/service_config_locate.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 cmd
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  
    22  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    23  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    25  	"code.vegaprotocol.io/vega/paths"
    26  	svcStoreV1 "code.vegaprotocol.io/vega/wallet/service/store/v1"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var (
    32  	locateServiceConfigLong = cli.LongDesc(`
    33  		Locate the wallet service configuration file.
    34  	`)
    35  
    36  	locateServiceConfigExample = cli.Examples(`
    37  		# Locate the wallet service configuration file
    38  		{{.Software}} service config locate
    39  	`)
    40  )
    41  
    42  type LocateServiceConfigsResponse struct {
    43  	Path string `json:"path"`
    44  }
    45  
    46  type LocateServiceConfigsHandler func() (*LocateServiceConfigsResponse, error)
    47  
    48  func NewCmdLocateServiceConfig(w io.Writer, rf *RootFlags) *cobra.Command {
    49  	h := func() (*LocateServiceConfigsResponse, error) {
    50  		vegaPaths := paths.New(rf.Home)
    51  
    52  		svcConfig, err := svcStoreV1.InitialiseStore(vegaPaths)
    53  		if err != nil {
    54  			return nil, fmt.Errorf("couldn't initialise service store: %w", err)
    55  		}
    56  
    57  		return &LocateServiceConfigsResponse{
    58  			Path: svcConfig.GetServiceConfigsPath(),
    59  		}, nil
    60  	}
    61  
    62  	return BuildCmdLocateServiceConfigs(w, h, rf)
    63  }
    64  
    65  func BuildCmdLocateServiceConfigs(w io.Writer, handler LocateServiceConfigsHandler, rf *RootFlags) *cobra.Command {
    66  	cmd := &cobra.Command{
    67  		Use:     "locate",
    68  		Short:   " Locate the wallet service configuration file",
    69  		Long:    locateServiceConfigLong,
    70  		Example: locateServiceConfigExample,
    71  		RunE: func(_ *cobra.Command, _ []string) error {
    72  			resp, err := handler()
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			switch rf.Output {
    78  			case flags.InteractiveOutput:
    79  				PrintLocateServiceConfigsResponse(w, resp)
    80  			case flags.JSONOutput:
    81  				return printer.FprintJSON(w, resp)
    82  			}
    83  
    84  			return nil
    85  		},
    86  	}
    87  
    88  	return cmd
    89  }
    90  
    91  func PrintLocateServiceConfigsResponse(w io.Writer, resp *LocateServiceConfigsResponse) {
    92  	p := printer.NewInteractivePrinter(w)
    93  
    94  	str := p.String()
    95  	defer p.Print(str)
    96  
    97  	str.Text("The service configuration file is located at: ").SuccessText(resp.Path).NextLine()
    98  }