github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/commands/reset/command.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program 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   * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package reset
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/mysteriumnetwork/node/cmd/commands/cli/clio"
    25  	"github.com/mysteriumnetwork/node/config"
    26  	"github.com/mysteriumnetwork/node/config/remote"
    27  	"github.com/mysteriumnetwork/node/core/auth"
    28  	"github.com/urfave/cli/v2"
    29  )
    30  
    31  // CommandName for the reset command.
    32  const CommandName = "reset"
    33  
    34  // flagResetTequilapiAuth instructs to reset Tequilapi auth.
    35  var flagResetTequilapiAuth = cli.BoolFlag{
    36  	Name: "tequilapi",
    37  	Usage: fmt.Sprintf("Reset Tequilapi auth credentials to values provider in --%s and --%s flags",
    38  		config.FlagTequilapiUsername.Name,
    39  		config.FlagTequilapiPassword.Name,
    40  	),
    41  	Value: true,
    42  }
    43  
    44  // NewCommand creates reset command.
    45  func NewCommand() *cli.Command {
    46  	return &cli.Command{
    47  		Name:      CommandName,
    48  		Usage:     "Resets Mysterium Node to defaults",
    49  		ArgsUsage: " ",
    50  		Flags:     []cli.Flag{&flagResetTequilapiAuth},
    51  		Action: func(ctx *cli.Context) error {
    52  			cmd, err := newAction(ctx)
    53  			if err != nil {
    54  				return err
    55  			}
    56  
    57  			return cmd.Run(ctx)
    58  		},
    59  	}
    60  }
    61  
    62  // newAction creates instance of reset action.
    63  func newAction(ctx *cli.Context) (*resetAction, error) {
    64  	client, err := clio.NewTequilApiClient(ctx)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	cfg, err := remote.NewConfig(client)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	return &resetAction{
    75  		writer: ctx.App.Writer,
    76  		cfg:    cfg,
    77  	}, nil
    78  }
    79  
    80  // resetAction represent entrypoint for reset command with top level components.
    81  type resetAction struct {
    82  	writer io.Writer
    83  	cfg    *remote.Config
    84  }
    85  
    86  // Run runs action tasks.
    87  func (rc *resetAction) Run(ctx *cli.Context) error {
    88  	if ctx.Bool(flagResetTequilapiAuth.Name) {
    89  		err := rc.resetTequilapi()
    90  		if err != nil {
    91  			fmt.Fprintln(rc.writer, err)
    92  		}
    93  		return err
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func (rc *resetAction) resetTequilapi() error {
   100  	err := auth.
   101  		NewCredentialsManager(rc.cfg.GetString(config.FlagDataDir.Name)).
   102  		SetPassword(rc.cfg.GetString(config.FlagTequilapiPassword.Name))
   103  	if err != nil {
   104  		return fmt.Errorf("error changing Tequialpi password: %w", err)
   105  	}
   106  
   107  	_, _ = fmt.Fprintf(rc.writer, `Tequilapi "%s" user password changed successfully`, config.FlagTequilapiUsername.Value)
   108  	_, _ = fmt.Fprintln(rc.writer)
   109  
   110  	return nil
   111  }