multi-motors-controller/src/cmd/root.go

84 lines
2.4 KiB
Go
Raw Normal View History

2024-09-03 11:20:50 +02:00
/*
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
2024-09-03 16:50:05 +02:00
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
2024-09-03 11:20:50 +02:00
Use: "src",
2024-09-03 12:55:55 +02:00
Short: "Multi Motors Controller",
Long: `Root command of Multi Motors Controller`,
// Args: cobra.MinimumNArgs(1),
2024-09-03 11:20:50 +02:00
// Uncomment the following line if your bare application
// has an action associated with it:
2024-09-03 12:55:55 +02:00
// Run: func(cmd *cobra.Command, args []string) { fmt.Println("root called with: ", viper.Get("motors")) },
2024-09-03 11:20:50 +02:00
}
// Execute adds all child commands to the root command and sets flags appropriately.
2024-09-03 16:50:05 +02:00
// This is called by main.main(). It only needs to happen once to the RootCmd.
2024-09-03 11:20:50 +02:00
func Execute() {
2024-09-03 16:50:05 +02:00
err := RootCmd.Execute()
2024-09-03 11:20:50 +02:00
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
2024-09-03 16:50:05 +02:00
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
//RootCmd.MarkFlagRequired("config")
// viper.BindPFlag("config", RootCmd.PersistentFlags().Lookup("config"))
2024-09-03 11:20:50 +02:00
// Cobra also supports local flags, which will only run
// when this action is called directly.
2024-09-03 16:50:05 +02:00
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2024-09-03 11:20:50 +02:00
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
2024-09-03 12:55:55 +02:00
// viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
2024-09-03 11:20:50 +02:00
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".src" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".src")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
2024-09-03 12:55:55 +02:00
} else {
panic(fmt.Errorf("fatal error config file: %w", err))
// if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// // Config file not found; ignore error if desired
// } else {
// // Config file was found but another error was produced
// }
2024-09-03 11:20:50 +02:00
}
}