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

     1  /*
     2   * Copyright (C) 2017 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 main
    19  
    20  import (
    21  	"os"
    22  	"sync"
    23  
    24  	"github.com/mysteriumnetwork/node/cmd/commands/account"
    25  	command_cli "github.com/mysteriumnetwork/node/cmd/commands/cli"
    26  	command_cfg "github.com/mysteriumnetwork/node/cmd/commands/config"
    27  	"github.com/mysteriumnetwork/node/cmd/commands/connection"
    28  	"github.com/mysteriumnetwork/node/cmd/commands/daemon"
    29  	"github.com/mysteriumnetwork/node/cmd/commands/license"
    30  	"github.com/mysteriumnetwork/node/cmd/commands/reset"
    31  	"github.com/mysteriumnetwork/node/cmd/commands/service"
    32  	"github.com/mysteriumnetwork/node/cmd/commands/version"
    33  	"github.com/mysteriumnetwork/node/config"
    34  	"github.com/mysteriumnetwork/node/logconfig"
    35  	"github.com/mysteriumnetwork/node/metadata"
    36  	"github.com/rs/zerolog"
    37  	"github.com/rs/zerolog/log"
    38  	"github.com/urfave/cli/v2"
    39  )
    40  
    41  var (
    42  	licenseCopyright = metadata.LicenseCopyright(
    43  		"run command 'license --warranty'",
    44  		"run command 'license --conditions'",
    45  	)
    46  	versionSummary    = metadata.VersionAsSummary(licenseCopyright)
    47  	daemonCommand     = daemon.NewCommand()
    48  	versionCommand    = version.NewCommand(versionSummary)
    49  	licenseCommand    = license.NewCommand(licenseCopyright)
    50  	serviceCommand    = service.NewCommand(licenseCommand.Name)
    51  	cliCommand        = command_cli.NewCommand()
    52  	resetCommand      = reset.NewCommand()
    53  	accountCommand    = account.NewCommand()
    54  	connectionCommand = connection.NewCommand()
    55  	configCommand     = command_cfg.NewCommand()
    56  )
    57  
    58  func main() {
    59  	logconfig.Bootstrap()
    60  	app, err := NewCommand()
    61  	if err != nil {
    62  		log.Error().Err(err).Msg("Failed to create command: ")
    63  		os.Exit(1)
    64  	}
    65  
    66  	err = app.Run(os.Args)
    67  	if err != nil {
    68  		log.Error().Err(err).Msg("Failed to execute command: ")
    69  		os.Exit(1)
    70  	}
    71  }
    72  
    73  // NewCommand function creates application master command
    74  func NewCommand() (*cli.App, error) {
    75  	cli.VersionPrinter = func(ctx *cli.Context) {
    76  		versionCommand.Run(ctx)
    77  	}
    78  
    79  	app, err := newApp()
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	app.Usage = "VPN server and client for Mysterium Network https://mysterium.network/"
    85  	app.Authors = []*cli.Author{
    86  		{Name: `The "MysteriumNetwork/node" Authors`, Email: "mysterium-dev@mysterium.network"},
    87  	}
    88  	app.Version = metadata.VersionAsString()
    89  	app.Copyright = licenseCopyright
    90  	app.Before = configureLogging()
    91  
    92  	app.Commands = []*cli.Command{
    93  		versionCommand,
    94  		licenseCommand,
    95  		serviceCommand,
    96  		daemonCommand,
    97  		cliCommand,
    98  		resetCommand,
    99  		accountCommand,
   100  		connectionCommand,
   101  		configCommand,
   102  	}
   103  
   104  	return app, nil
   105  }
   106  
   107  func newApp() (*cli.App, error) {
   108  	app := cli.NewApp()
   109  	return app, config.RegisterFlagsNode(&app.Flags)
   110  }
   111  
   112  // uiCommands is a map which consists of all
   113  // commands are used directly by a user.
   114  var uiCommands = map[string]struct{}{
   115  	command_cli.CommandName: {},
   116  	account.CommandName:     {},
   117  	connection.CommandName:  {},
   118  	command_cfg.CommandName: {},
   119  	reset.CommandName:       {},
   120  }
   121  
   122  // configureLogging returns a func which configures global
   123  // logging settings depending on the command used.
   124  // It only runs once.
   125  func configureLogging() cli.BeforeFunc {
   126  	var once sync.Once
   127  	return func(ctx *cli.Context) error {
   128  		once.Do(func() {
   129  			// Keep default settings if verbose logging is enabled.
   130  			if ctx.Bool(config.FlagVerbose.Name) {
   131  				return
   132  			}
   133  
   134  			cmd := ctx.Args().First()
   135  			if _, ok := uiCommands[cmd]; !ok {
   136  				// If the command is not meant for user
   137  				// interaction, skip.
   138  				return
   139  			}
   140  			logconfig.SetLogLevel(zerolog.PanicLevel)
   141  		})
   142  		return nil
   143  	}
   144  }