storj/cmd/storagenode/root.go
Clement Sam 3cf89633e9 cmd/storagenode: refactor main.go
The cmd/storagenode/main.go is a big mess right now with so many
unneeded config structures initialized and shared by several
subcommands.

There are many instances where the config structure of one subcommand
is mistakenly used for another subcommand.

This changes is an attempt to clean up the main.go by moving the
subcommands to a separate `cmd_*.go` files with separate config structures
for each subcommand.

Resolves https://github.com/storj/storj/issues/5756

Change-Id: I85adf2439acba271c023c269739f7fa3c6d49f9d
2023-04-06 12:48:23 +00:00

65 lines
1.7 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"github.com/spf13/cobra"
"go.uber.org/zap"
"storj.io/common/fpath"
"storj.io/private/cfgstruct"
"storj.io/storj/storagenode"
)
// StorageNodeFlags defines storage node configuration.
type StorageNodeFlags struct {
EditConf bool `default:"false" help:"open config in default editor"`
storagenode.Config
Deprecated
}
// Factory contains default values for configuration flags.
type Factory struct {
Defaults cfgstruct.BindOpt
ConfDir string
IdentityDir string
UseColor bool
}
// newRootCmd creates a new root command.
func newRootCmd(setDefaults bool) (*cobra.Command, *Factory) {
cmd := &cobra.Command{
Use: "storagenode",
Short: "Storagenode",
}
factory := &Factory{}
if setDefaults {
defaultConfDir := fpath.ApplicationDir("storj", "storagenode")
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "storagenode")
cfgstruct.SetupFlag(zap.L(), cmd, &factory.ConfDir, "config-dir", defaultConfDir, "main directory for storagenode configuration")
cfgstruct.SetupFlag(zap.L(), cmd, &factory.IdentityDir, "identity-dir", defaultIdentityDir, "main directory for storagenode identity credentials")
cmd.PersistentFlags().BoolVar(&factory.UseColor, "color", false, "use color in user interface")
factory.Defaults = cfgstruct.DefaultsFlag(cmd)
}
cmd.AddCommand(
newConfigCmd(factory),
newSetupCmd(factory),
newDashboardCmd(factory),
newDiagCmd(factory),
newRunCmd(factory),
newNodeInfoCmd(factory),
newIssueAPIKeyCmd(factory),
newGracefulExitInitCmd(factory),
newGracefulExitStatusCmd(factory),
)
return cmd, factory
}