code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/service_config_reset.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  	vgterm "code.vegaprotocol.io/vega/libs/term"
    26  	"code.vegaprotocol.io/vega/paths"
    27  	"code.vegaprotocol.io/vega/wallet/service"
    28  	svcStoreV1 "code.vegaprotocol.io/vega/wallet/service/store/v1"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var (
    34  	resetServiceConfigLong = cli.LongDesc(`
    35  	    Reset the service configuration to its defaults.
    36  	`)
    37  
    38  	resetServiceConfigExample = cli.Examples(`
    39  		# Reset the service configuration
    40  		{{.Software}} service config reset
    41  	`)
    42  )
    43  
    44  type ResetServiceConfigHandler func() (*service.Config, error)
    45  
    46  func NewCmdResetServiceConfig(w io.Writer, rf *RootFlags) *cobra.Command {
    47  	h := func() (*service.Config, error) {
    48  		vegaPaths := paths.New(rf.Home)
    49  
    50  		svcStore, err := svcStoreV1.InitialiseStore(vegaPaths)
    51  		if err != nil {
    52  			return nil, fmt.Errorf("couldn't initialise service store: %w", err)
    53  		}
    54  
    55  		defaultCfg := service.DefaultConfig()
    56  
    57  		if err := svcStore.SaveConfig(defaultCfg); err != nil {
    58  			return nil, fmt.Errorf("could not save the default service configuration: %w", err)
    59  		}
    60  
    61  		return defaultCfg, nil
    62  	}
    63  
    64  	return BuildCmdResetServiceConfig(w, h, rf)
    65  }
    66  
    67  func BuildCmdResetServiceConfig(w io.Writer, handler ResetServiceConfigHandler, rf *RootFlags) *cobra.Command {
    68  	f := &ResetServiceConfigFlags{}
    69  
    70  	cmd := &cobra.Command{
    71  		Use:     "reset",
    72  		Short:   "Reset the service configuration to its defaults",
    73  		Long:    resetServiceConfigLong,
    74  		Example: resetServiceConfigExample,
    75  		RunE: func(_ *cobra.Command, _ []string) error {
    76  			if !f.Force && vgterm.HasTTY() {
    77  				if !flags.AreYouSure() {
    78  					return nil
    79  				}
    80  			}
    81  
    82  			cfg, err := handler()
    83  			if err != nil {
    84  				return err
    85  			}
    86  
    87  			switch rf.Output {
    88  			case flags.InteractiveOutput:
    89  				PrintResetServiceConfigResponse(w, cfg)
    90  			case flags.JSONOutput:
    91  				return printer.FprintJSON(w, cfg)
    92  			}
    93  
    94  			return nil
    95  		},
    96  	}
    97  
    98  	cmd.Flags().BoolVarP(&f.Force,
    99  		"force", "f",
   100  		false,
   101  		"Do not ask for confirmation",
   102  	)
   103  
   104  	return cmd
   105  }
   106  
   107  type ResetServiceConfigFlags struct {
   108  	Force bool
   109  }
   110  
   111  func (f *ResetServiceConfigFlags) Validate() error {
   112  	if !f.Force && vgterm.HasNoTTY() {
   113  		return ErrForceFlagIsRequiredWithoutTTY
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func PrintResetServiceConfigResponse(w io.Writer, cfg *service.Config) {
   120  	p := printer.NewInteractivePrinter(w)
   121  
   122  	str := p.String()
   123  	defer p.Print(str)
   124  
   125  	str.CheckMark().SuccessText("The service configuration has been reset.").NextSection()
   126  	str.Text("Service URL: ").WarningText(cfg.Server.String()).NextSection()
   127  	str.Text("Log level: ").WarningText(cfg.LogLevel.String()).NextSection()
   128  	str.Text("API V1").NextLine()
   129  	str.Pad().Text("Maximum token duration: ").WarningText(cfg.APIV1.MaximumTokenDuration.String()).NextSection()
   130  	str.Text("API V2").NextLine()
   131  	str.Pad().Text("Nodes:").NextLine()
   132  	str.Pad().Pad().Text("Maximum retry per request: ").WarningText(fmt.Sprintf("%d", cfg.APIV2.Nodes.MaximumRetryPerRequest)).NextLine()
   133  	str.Pad().Pad().Text("Maximum request duration: ").WarningText(cfg.APIV2.Nodes.MaximumRequestDuration.String()).NextLine()
   134  }