Added project files

This commit is contained in:
David J. Allen 2024-03-07 17:00:39 -07:00
parent 40615a97f8
commit be40387f4a
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
10 changed files with 486 additions and 0 deletions

54
cmd/root.go Normal file
View file

@ -0,0 +1,54 @@
package cmd
import (
"fmt"
"os"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
"github.com/spf13/cobra"
)
var (
configPath string
config *configurator.Config
)
var rootCmd = &cobra.Command{
Use: "configurator",
Short: "Tool for building common config files",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
}
func initConfig() {
if configPath != "" {
exists, err := util.PathExists(configPath)
if err != nil {
fmt.Printf("failed to load config")
os.Exit(1)
} else if exists {
config = configurator.LoadConfig(configPath)
} else {
config = configurator.NewConfig()
}
} else {
config = configurator.NewConfig()
}
}