/* Copyright © 2024 NAME HERE */ package cmd import ( "fmt" "os" "github.com/joho/godotenv" "github.com/spf13/cobra" "github.com/spf13/viper" ) var ( cfgFile string miniflux_host string miniflux_token string miniflux_feed_id int64 ) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "miniflux-archiver", Short: "Archive solution for RSS miniflux", // Run: func(cmd *cobra.Command, args []string) { // fmt.Printf("%v", viper.AllSettings()) // }, } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } func init() { godotenv.Load() 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. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") rootCmd.PersistentFlags().StringVar(&miniflux_host, "miniflux-host", "", "127.0.0.1") rootCmd.PersistentFlags().StringVar(&miniflux_token, "miniflux-token", "", "XXX-XXX-XXX") rootCmd.PersistentFlags().Int64Var(&miniflux_feed_id, "miniflux-feed_id", 0, "1") viper.BindPFlag("miniflux_host", rootCmd.PersistentFlags().Lookup("miniflux-host")) viper.BindPFlag("miniflux_token", rootCmd.PersistentFlags().Lookup("miniflux-token")) viper.BindPFlag("miniflux_feed_id", rootCmd.PersistentFlags().Lookup("miniflux-feed-id")) } // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { // 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 ".miniflux-archiver" (without extension). viper.AddConfigPath(home) viper.AddConfigPath(".") viper.SetConfigType("toml") viper.SetConfigName(".miniflux-archiver") } viper.SetEnvPrefix("MFA") 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()) } requiredParams := [...]string{ "miniflux_host", "miniflux_token", "miniflux_feed_id", } for _, param := range requiredParams { if !viper.IsSet(param) { fmt.Printf( "Error: The value \"%v\" must be set. For more information use the --help command\n", param, ) os.Exit(1) } } }