github.com/igggame/nebulas-go@v2.1.0+incompatible/cmd/neb/flags.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"github.com/nebulasio/go-nebulas/neblet/pb"
    23  	"github.com/urfave/cli"
    24  )
    25  
    26  var (
    27  	// ConfigFlag config file path
    28  	ConfigFlag = cli.StringFlag{
    29  		Name:        "config, c",
    30  		Usage:       "load configuration from `FILE`",
    31  		Value:       "conf/default/config.conf",
    32  		Destination: &config,
    33  	}
    34  
    35  	// NetworkSeedFlag network seed
    36  	NetworkSeedFlag = cli.StringSliceFlag{
    37  		Name:  "network.seed",
    38  		Usage: "network seed addresses, multi-value support.",
    39  	}
    40  
    41  	// NetworkListenFlag network listen
    42  	NetworkListenFlag = cli.StringSliceFlag{
    43  		Name:  "network.listen",
    44  		Usage: "network listen addresses, multi-value support.",
    45  	}
    46  
    47  	// NetworkKeyPathFlag network key
    48  	NetworkKeyPathFlag = cli.StringFlag{
    49  		Name:  "network.key",
    50  		Usage: "network private key file path",
    51  	}
    52  
    53  	// NetworkFlags config list
    54  	NetworkFlags = []cli.Flag{
    55  		NetworkSeedFlag,
    56  		NetworkListenFlag,
    57  		NetworkKeyPathFlag,
    58  	}
    59  
    60  	// ChainIDFlag chain id
    61  	ChainIDFlag = cli.UintFlag{
    62  		Name:  "chain.id",
    63  		Usage: "chain id",
    64  	}
    65  
    66  	// ChainDataDirFlag chain data dir
    67  	ChainDataDirFlag = cli.StringFlag{
    68  		Name:  "chain.datadir",
    69  		Usage: "chain data storage dirctory",
    70  	}
    71  
    72  	// ChainKeyDirFlag chain key dir
    73  	ChainKeyDirFlag = cli.StringFlag{
    74  		Name:  "chain.keydir",
    75  		Usage: "chain key storage dirctory",
    76  	}
    77  
    78  	// ChainStartMineFlag chain start mine when launch
    79  	ChainStartMineFlag = cli.BoolFlag{
    80  		Name:  "chain.startmine",
    81  		Usage: "chain start mine when launching",
    82  	}
    83  
    84  	// ChainCoinbaseFlag chain coinbase
    85  	ChainCoinbaseFlag = cli.StringFlag{
    86  		Name:  "chain.coinbase",
    87  		Usage: "chain coinbase dirctory",
    88  	}
    89  
    90  	// ChainCipherFlag chain cipher
    91  	ChainCipherFlag = cli.StringSliceFlag{
    92  		Name:  "chain.ciphers",
    93  		Usage: "chain signature ciphers, multi-value support.",
    94  	}
    95  
    96  	// ChainPassphraseFlag chain miner passphrase
    97  	ChainPassphraseFlag = cli.StringFlag{
    98  		Name:  "chain.passphrase",
    99  		Usage: "chain miner's passphrase.",
   100  	}
   101  
   102  	// ChainGasPriceFlag chain transaction pool min gasPrice
   103  	ChainGasPriceFlag = cli.StringFlag{
   104  		Name:  "chain.gasprice",
   105  		Usage: "chain transaction pool's min gasPrice.",
   106  	}
   107  
   108  	// ChainGasLimitFlag chain transaction pool max gasLimit
   109  	ChainGasLimitFlag = cli.StringFlag{
   110  		Name:  "chain.gaslimit",
   111  		Usage: "chain transaction pool's max gasLimit.",
   112  	}
   113  
   114  	// ChainFlags chain config list
   115  	ChainFlags = []cli.Flag{
   116  		ChainIDFlag,
   117  		ChainDataDirFlag,
   118  		ChainKeyDirFlag,
   119  		ChainStartMineFlag,
   120  		ChainCoinbaseFlag,
   121  		ChainCipherFlag,
   122  		ChainPassphraseFlag,
   123  		ChainGasPriceFlag,
   124  		ChainGasLimitFlag,
   125  	}
   126  
   127  	// RPCListenFlag rpc listen
   128  	RPCListenFlag = cli.StringSliceFlag{
   129  		Name:  "rpc.listen",
   130  		Usage: "rpc listen addresses, multi-value support.",
   131  	}
   132  
   133  	// RPCHTTPFlag rpc http listen
   134  	RPCHTTPFlag = cli.StringSliceFlag{
   135  		Name:  "rpc.http",
   136  		Usage: "rpc http listen addresses, multi-value support.",
   137  	}
   138  
   139  	// RPCModuleFlag rpc http module
   140  	RPCModuleFlag = cli.StringSliceFlag{
   141  		Name:  "rpc.module",
   142  		Usage: "rpc support modules, multi-value support.",
   143  	}
   144  
   145  	// RPCFlags rpc config list
   146  	RPCFlags = []cli.Flag{
   147  		RPCListenFlag,
   148  		RPCHTTPFlag,
   149  		RPCModuleFlag,
   150  	}
   151  
   152  	// AppLogLevelFlag app log level
   153  	AppLogLevelFlag = cli.StringFlag{
   154  		Name:  "app.loglevel",
   155  		Usage: "app log level for neb run.",
   156  	}
   157  
   158  	// AppLogFileFlag app log file dir
   159  	AppLogFileFlag = cli.StringFlag{
   160  		Name:  "app.logfile",
   161  		Usage: "app log file folder for neb run.",
   162  	}
   163  
   164  	// AppCrashReportFlag enable app crash report
   165  	AppCrashReportFlag = cli.BoolFlag{
   166  		Name:  "app.crashreport",
   167  		Usage: "app enable crash report.",
   168  	}
   169  
   170  	// AppCrashReportURLFlag app log level
   171  	AppCrashReportURLFlag = cli.StringFlag{
   172  		Name:  "app.reporturl",
   173  		Usage: "app crash report url.",
   174  	}
   175  
   176  	// AppProfileListen pprof http listen
   177  	AppProfileListen = cli.StringFlag{
   178  		Name:  "app.pprof.listen",
   179  		Usage: "pprof net listen address",
   180  		Value: "",
   181  	}
   182  
   183  	// AppCPUProfile stats cpu profile
   184  	AppCPUProfile = cli.StringFlag{
   185  		Name:  "app.pprof.cpuprofile",
   186  		Usage: "pprof write cpu profile `file`",
   187  		Value: "",
   188  	}
   189  
   190  	// AppMemProfile stats memory profile
   191  	AppMemProfile = cli.StringFlag{
   192  		Name:  "app.pprof.memprofile",
   193  		Usage: "pprof write memory profile `file`",
   194  		Value: "",
   195  	}
   196  
   197  	// AppFlags app config list
   198  	AppFlags = []cli.Flag{
   199  		AppLogLevelFlag,
   200  		AppLogFileFlag,
   201  		AppCrashReportFlag,
   202  		AppCrashReportURLFlag,
   203  		AppProfileListen,
   204  		AppCPUProfile,
   205  		AppMemProfile,
   206  	}
   207  
   208  	// StatsEnableFlag stats enable
   209  	StatsEnableFlag = cli.BoolFlag{
   210  		Name:  "stats.enable",
   211  		Usage: "stats enable metrics",
   212  	}
   213  
   214  	// StatsDBHostFlag stats db host
   215  	StatsDBHostFlag = cli.StringFlag{
   216  		Name:  "stats.dbhost",
   217  		Usage: "stats influxdb host",
   218  	}
   219  
   220  	// StatsDBNameFlag stats db name
   221  	StatsDBNameFlag = cli.StringFlag{
   222  		Name:  "stats.dbname",
   223  		Usage: "stats influxdb db name",
   224  	}
   225  
   226  	// StatsDBUserFlag stats db user
   227  	StatsDBUserFlag = cli.StringFlag{
   228  		Name:  "stats.dbuser",
   229  		Usage: "stats influxdb user",
   230  	}
   231  
   232  	// StatsDBPasswordFlag stats db password
   233  	StatsDBPasswordFlag = cli.StringFlag{
   234  		Name:  "stats.dbpassword",
   235  		Usage: "stats influxdb password",
   236  	}
   237  
   238  	// StatsFlags stats config list
   239  	StatsFlags = []cli.Flag{
   240  		StatsEnableFlag,
   241  		StatsDBHostFlag,
   242  		StatsDBNameFlag,
   243  		StatsDBUserFlag,
   244  		StatsDBPasswordFlag,
   245  	}
   246  )
   247  
   248  func networkConfig(ctx *cli.Context, cfg *nebletpb.NetworkConfig) {
   249  	if ctx.GlobalIsSet(NetworkSeedFlag.Name) {
   250  		cfg.Seed = ctx.GlobalStringSlice(NetworkSeedFlag.Name)
   251  	}
   252  	if ctx.GlobalIsSet(NetworkListenFlag.Name) {
   253  		cfg.Listen = ctx.GlobalStringSlice(NetworkListenFlag.Name)
   254  	}
   255  	if ctx.GlobalIsSet(NetworkKeyPathFlag.Name) {
   256  		cfg.PrivateKey = ctx.GlobalString(NetworkKeyPathFlag.Name)
   257  	}
   258  }
   259  
   260  func chainConfig(ctx *cli.Context, cfg *nebletpb.ChainConfig) {
   261  	if ctx.GlobalIsSet(ChainIDFlag.Name) {
   262  		cfg.ChainId = uint32(ctx.GlobalUint(ChainIDFlag.Name))
   263  	}
   264  	if ctx.GlobalIsSet(ChainDataDirFlag.Name) {
   265  		cfg.Datadir = ctx.GlobalString(ChainDataDirFlag.Name)
   266  	}
   267  	if ctx.GlobalIsSet(ChainKeyDirFlag.Name) {
   268  		cfg.Keydir = ctx.GlobalString(ChainKeyDirFlag.Name)
   269  	}
   270  	if ctx.GlobalIsSet(ChainStartMineFlag.Name) {
   271  		cfg.StartMine = ctx.GlobalBool(ChainStartMineFlag.Name)
   272  	}
   273  	if ctx.GlobalIsSet(ChainCoinbaseFlag.Name) {
   274  		cfg.Coinbase = ctx.GlobalString(ChainCoinbaseFlag.Name)
   275  	}
   276  	if ctx.GlobalIsSet(ChainPassphraseFlag.Name) {
   277  		cfg.Passphrase = ctx.GlobalString(ChainPassphraseFlag.Name)
   278  	}
   279  	if ctx.GlobalIsSet(ChainGasPriceFlag.Name) {
   280  		cfg.GasPrice = ctx.GlobalString(ChainGasPriceFlag.Name)
   281  	}
   282  	if ctx.GlobalIsSet(ChainGasLimitFlag.Name) {
   283  		cfg.GasLimit = ctx.GlobalString(ChainGasLimitFlag.Name)
   284  	}
   285  	if ctx.GlobalIsSet(ChainCipherFlag.Name) {
   286  		cfg.SignatureCiphers = ctx.GlobalStringSlice(ChainCipherFlag.Name)
   287  	}
   288  }
   289  
   290  func rpcConfig(ctx *cli.Context, cfg *nebletpb.RPCConfig) {
   291  	if ctx.GlobalIsSet(RPCListenFlag.Name) {
   292  		cfg.RpcListen = ctx.GlobalStringSlice(RPCListenFlag.Name)
   293  	}
   294  	if ctx.GlobalIsSet(RPCHTTPFlag.Name) {
   295  		cfg.HttpListen = ctx.GlobalStringSlice(RPCHTTPFlag.Name)
   296  	}
   297  	if ctx.GlobalIsSet(RPCModuleFlag.Name) {
   298  		cfg.HttpModule = ctx.GlobalStringSlice(RPCModuleFlag.Name)
   299  	}
   300  }
   301  
   302  func appConfig(ctx *cli.Context, cfg *nebletpb.AppConfig) {
   303  	if ctx.GlobalIsSet(AppLogLevelFlag.Name) {
   304  		cfg.LogLevel = ctx.GlobalString(AppLogLevelFlag.Name)
   305  	}
   306  	if ctx.GlobalIsSet(AppLogFileFlag.Name) {
   307  		cfg.LogFile = ctx.GlobalString(AppLogFileFlag.Name)
   308  	}
   309  	if ctx.GlobalIsSet(AppCrashReportFlag.Name) {
   310  		cfg.EnableCrashReport = ctx.GlobalBool(AppCrashReportFlag.Name)
   311  	}
   312  	if ctx.GlobalIsSet(AppCrashReportURLFlag.Name) {
   313  		cfg.CrashReportUrl = ctx.GlobalString(AppCrashReportURLFlag.Name)
   314  	}
   315  
   316  	if cfg.Pprof == nil {
   317  		cfg.Pprof = &nebletpb.PprofConfig{}
   318  	}
   319  	if ctx.GlobalIsSet(AppProfileListen.Name) {
   320  		cfg.Pprof.HttpListen = ctx.GlobalString(AppProfileListen.Name)
   321  	}
   322  	if ctx.GlobalIsSet(AppCPUProfile.Name) {
   323  		cfg.Pprof.Cpuprofile = ctx.GlobalString(AppCPUProfile.Name)
   324  	}
   325  	if ctx.GlobalIsSet(AppMemProfile.Name) {
   326  		cfg.Pprof.Memprofile = ctx.GlobalString(AppMemProfile.Name)
   327  	}
   328  }
   329  
   330  func statsConfig(ctx *cli.Context, cfg *nebletpb.StatsConfig) {
   331  	if ctx.GlobalIsSet(StatsEnableFlag.Name) {
   332  		cfg.EnableMetrics = ctx.GlobalBool(StatsEnableFlag.Name)
   333  	}
   334  	if ctx.GlobalIsSet(StatsDBHostFlag.Name) {
   335  		cfg.Influxdb.Host = ctx.GlobalString(StatsDBHostFlag.Name)
   336  	}
   337  	if ctx.GlobalIsSet(StatsDBNameFlag.Name) {
   338  		cfg.Influxdb.Db = ctx.GlobalString(StatsDBNameFlag.Name)
   339  	}
   340  	if ctx.GlobalIsSet(StatsDBUserFlag.Name) {
   341  		cfg.Influxdb.User = ctx.GlobalString(StatsDBUserFlag.Name)
   342  	}
   343  	if ctx.GlobalIsSet(StatsDBPasswordFlag.Name) {
   344  		cfg.Influxdb.Password = ctx.GlobalString(StatsDBPasswordFlag.Name)
   345  	}
   346  }
   347  
   348  // MergeFlags sets the global flag from a local flag when it's set.
   349  func MergeFlags(action func(ctx *cli.Context) error) func(*cli.Context) error {
   350  	return func(ctx *cli.Context) error {
   351  		for _, name := range ctx.FlagNames() {
   352  			if ctx.IsSet(name) {
   353  				ctx.GlobalSet(name, ctx.String(name))
   354  			}
   355  		}
   356  		return action(ctx)
   357  	}
   358  }