code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/service_config_describe.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  	"code.vegaprotocol.io/vega/wallet/service"
    27  	svcStoreV1 "code.vegaprotocol.io/vega/wallet/service/store/v1"
    28  
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  var (
    33  	describeServiceConfigLong = cli.LongDesc(`
    34  	    Describe the service configuration.
    35  	`)
    36  
    37  	describeServiceConfigExample = cli.Examples(`
    38  		# Describe the service configuration
    39  		{{.Software}} service config describe
    40  	`)
    41  )
    42  
    43  type DescribeServiceConfigHandler func() (*service.Config, error)
    44  
    45  func NewCmdDescribeServiceConfig(w io.Writer, rf *RootFlags) *cobra.Command {
    46  	h := func() (*service.Config, error) {
    47  		vegaPaths := paths.New(rf.Home)
    48  
    49  		svcStore, err := svcStoreV1.InitialiseStore(vegaPaths)
    50  		if err != nil {
    51  			return nil, fmt.Errorf("couldn't initialise service store: %w", err)
    52  		}
    53  
    54  		cfg, err := svcStore.GetConfig()
    55  		if err != nil {
    56  			return nil, fmt.Errorf("could not retrieve the service configuration: %w", err)
    57  		}
    58  
    59  		return cfg, nil
    60  	}
    61  
    62  	return BuildCmdDescribeServiceConfig(w, h, rf)
    63  }
    64  
    65  func BuildCmdDescribeServiceConfig(w io.Writer, handler DescribeServiceConfigHandler, rf *RootFlags) *cobra.Command {
    66  	cmd := &cobra.Command{
    67  		Use:     "describe",
    68  		Short:   "Describe the service configuration",
    69  		Long:    describeServiceConfigLong,
    70  		Example: describeServiceConfigExample,
    71  		RunE: func(_ *cobra.Command, _ []string) error {
    72  			cfg, err := handler()
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			switch rf.Output {
    78  			case flags.InteractiveOutput:
    79  				PrintDescribeServiceConfigResponse(w, cfg)
    80  			case flags.JSONOutput:
    81  				return printer.FprintJSON(w, cfg)
    82  			}
    83  
    84  			return nil
    85  		},
    86  	}
    87  
    88  	return cmd
    89  }
    90  
    91  func PrintDescribeServiceConfigResponse(w io.Writer, cfg *service.Config) {
    92  	p := printer.NewInteractivePrinter(w)
    93  
    94  	str := p.String()
    95  	defer p.Print(str)
    96  
    97  	str.NextLine()
    98  	str.Text("Service URL: ").WarningText(cfg.Server.String()).NextSection()
    99  	str.Text("Log level: ").WarningText(cfg.LogLevel.String()).NextSection()
   100  	str.Text("API V1").NextLine()
   101  	str.Pad().Text("Maximum token duration: ").WarningText(cfg.APIV1.MaximumTokenDuration.String()).NextSection()
   102  	str.Text("API V2").NextLine()
   103  	str.Pad().Text("Nodes:").NextLine()
   104  	str.Pad().Pad().Text("Maximum retry per request: ").WarningText(fmt.Sprintf("%d", cfg.APIV2.Nodes.MaximumRetryPerRequest)).NextLine()
   105  	str.Pad().Pad().Text("Maximum request duration: ").WarningText(cfg.APIV2.Nodes.MaximumRequestDuration.String()).NextLine()
   106  }