cmd: added error when specifying config path but not found
This commit is contained in:
parent
4bd4dac129
commit
b5d492d6c0
1 changed files with 8 additions and 2 deletions
10
cmd/root.go
10
cmd/root.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
configurator "github.com/OpenCHAMI/configurator/pkg"
|
configurator "github.com/OpenCHAMI/configurator/pkg"
|
||||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,12 +42,13 @@ func Execute() {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yaml", "set the config path")
|
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config path")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set to enable verbose output")
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set to enable verbose output")
|
||||||
rootCmd.PersistentFlags().StringVar(&cacertPath, "cacert", "", "path to CA cert. (defaults to system CAs)")
|
rootCmd.PersistentFlags().StringVar(&cacertPath, "cacert", "", "path to CA cert. (defaults to system CAs)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
// empty from not being set
|
||||||
if configPath != "" {
|
if configPath != "" {
|
||||||
exists, err := util.PathExists(configPath)
|
exists, err := util.PathExists(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -55,9 +57,13 @@ func initConfig() {
|
||||||
} else if exists {
|
} else if exists {
|
||||||
config = configurator.LoadConfig(configPath)
|
config = configurator.LoadConfig(configPath)
|
||||||
} else {
|
} else {
|
||||||
config = configurator.NewConfig()
|
// show error and exit since a path was specified
|
||||||
|
log.Error().Str("path", configPath).Msg("config file not found")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// set to the default value and create a new one
|
||||||
|
configPath = "./config.yaml"
|
||||||
config = configurator.NewConfig()
|
config = configurator.NewConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue