humble-bot/cmd/root.go

76 lines
2.1 KiB
Go
Raw Normal View History

/*
* Copyright (c) 2023 Laura Kalb <dev@lauka.net>
* The code of this project is available under the MIT license. See the LICENSE file for more info.
*
*/
package cmd
import (
"codeberg.org/lauralani/humble-bot/constants"
"codeberg.org/lauralani/humble-bot/db"
"codeberg.org/lauralani/humble-bot/log"
"codeberg.org/lauralani/humble-bot/misc"
log2 "log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "humble-bot",
2023-07-11 09:57:29 +02:00
Short: "A simple CLI app getting and posting new Humble Bundles on Mastodon",
Long: `This app regularly checks for new Humble Bundles and posts updates on Mastodon`,
}
// 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() {
cobra.OnInitialize(initConfig, db.Initialize, log.InitializeLogger, misc.CheckConfig)
2023-07-06 07:02:46 +02:00
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $workingdir/config.yaml)")
rootCmd.PersistentFlags().BoolVarP(&log.FlagDebug, "debug", "d", false, "enable debug logging")
2023-07-07 09:32:35 +02:00
rootCmd.CompletionOptions.DisableDefaultCmd = true
2023-07-07 09:32:15 +02:00
rootCmd.Version = constants.AppVersion
// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
workingdir, err := os.Getwd()
cobra.CheckErr(err)
viper.AddConfigPath(workingdir)
viper.SetConfigName("config")
}
viper.SetDefault("mastodon.postinterval", "5m")
viper.SetDefault("mastodon.visibility", "public")
viper.SetDefault("humblebundle.pollinterval", "30m")
if err := viper.ReadInConfig(); err == nil {
log2.Printf("Starting humble-bot %v with config file: %v\n", constants.AppVersion, viper.ConfigFileUsed())
} else {
log2.Printf("Can't find config file: %v\n", viper.ConfigFileUsed())
os.Exit(1)
}
}