Added project files
This commit is contained in:
parent
40615a97f8
commit
be40387f4a
10 changed files with 486 additions and 0 deletions
54
cmd/root.go
Normal file
54
cmd/root.go
Normal 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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue