Configure nodes.conf path dynamically from config

Removes the use of init() to initialize the variable.

- Closes: #1596
- See also: #1569

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-12-12 12:11:04 -07:00
parent b0d0e26b4a
commit a80ba8ee01
30 changed files with 78 additions and 203 deletions

View File

@@ -6,13 +6,20 @@ import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node"
libupgrade "github.com/warewulf/warewulf/internal/pkg/upgrade"
"github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/upgrade"
"github.com/warewulf/warewulf/internal/pkg/util"
)
var (
Command = &cobra.Command{
addDefaults bool
replaceOverlays bool
inputPath string
outputPath string
)
func GetCommand() *cobra.Command {
command := &cobra.Command{
DisableFlagsInUseLine: true,
Use: "nodes [OPTIONS]",
Short: "Upgrade an existing nodes.conf",
@@ -20,32 +27,33 @@ var (
supported by the current version.`,
RunE: UpgradeNodesConf,
}
addDefaults bool
replaceOverlays bool
inputPath string
outputPath string
)
func init() {
Command.Flags().BoolVar(&addDefaults, "add-defaults", false, "Configure a default profile and set default node values")
Command.Flags().BoolVar(&replaceOverlays, "replace-overlays", false, "Replace 'wwinit' and 'generic' overlays with their split replacements")
Command.Flags().StringVarP(&inputPath, "input-path", "i", node.ConfigFile, "Path to a legacy nodes.conf")
Command.Flags().StringVarP(&outputPath, "output-path", "o", node.ConfigFile, "Path to write the upgraded nodes.conf to")
if err := Command.MarkFlagRequired("add-defaults"); err != nil {
command.Flags().BoolVar(&addDefaults, "add-defaults", false, "Configure a default profile and set default node values")
command.Flags().BoolVar(&replaceOverlays, "replace-overlays", false, "Replace 'wwinit' and 'generic' overlays with their split replacements")
command.Flags().StringVarP(&inputPath, "input-path", "i", "", "Path to a legacy nodes.conf")
command.Flags().StringVarP(&outputPath, "output-path", "o", "", "Path to write the upgraded nodes.conf to")
if err := command.MarkFlagRequired("add-defaults"); err != nil {
panic(err)
}
if err := Command.MarkFlagRequired("replace-overlays"); err != nil {
if err := command.MarkFlagRequired("replace-overlays"); err != nil {
panic(err)
}
return command
}
func UpgradeNodesConf(cmd *cobra.Command, args []string) error {
inputPath := inputPath
if inputPath == "" {
inputPath = config.Get().Paths.NodesConf()
}
outputPath := outputPath
if outputPath == "" {
outputPath = config.Get().Paths.NodesConf()
}
data, err := os.ReadFile(inputPath)
if err != nil {
return err
}
legacy, err := libupgrade.ParseNodes(data)
legacy, err := upgrade.ParseNodes(data)
if err != nil {
return err
}