storj/cmd/storagenode/cmd_config.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

53 lines
1.1 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
"storj.io/common/fpath"
"storj.io/private/cfgstruct"
"storj.io/private/process"
)
func newConfigCmd(f *Factory) *cobra.Command {
var cfg setupCfg
cmd := &cobra.Command{
Use: "config",
Short: "Edit config files",
RunE: func(cmd *cobra.Command, args []string) error {
setupDir, err := filepath.Abs(f.ConfDir)
if err != nil {
return err
}
cfg.SetupDir = setupDir
return cmdConfig(cmd, &cfg)
},
Annotations: map[string]string{"type": "setup"},
}
process.Bind(cmd, &cfg, f.Defaults, cfgstruct.ConfDir(f.ConfDir), cfgstruct.IdentityDir(f.IdentityDir), cfgstruct.SetupMode())
return cmd
}
func cmdConfig(cmd *cobra.Command, cfg *setupCfg) (err error) {
setupDir, err := filepath.Abs(cfg.SetupDir)
if err != nil {
return err
}
// run setup if we can't access the config file
conf := filepath.Join(setupDir, "config.yaml")
if _, err := os.Stat(conf); err != nil {
return cmdSetup(cmd, cfg)
}
return fpath.EditFile(conf)
}