humble-bot/cmd/root.go
Adora Laura Kalb f575e0cb46
All checks were successful
ci/woodpecker/push/docker-deploy Pipeline was successful
ci/woodpecker/pr/docker-deploy Pipeline was successful
add logging clolr flag and change default values
2024-11-06 11:16:12 +01:00

77 lines
2.2 KiB
Go

/*
* 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 (
log2 "log"
"os"
"codeberg.org/lauralani/humble-bot/constants"
"codeberg.org/lauralani/humble-bot/db"
"codeberg.org/lauralani/humble-bot/log"
"codeberg.org/lauralani/humble-bot/misc"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "humble-bot",
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)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $workingdir/config.yaml)")
rootCmd.PersistentFlags().BoolVarP(&log.FlagDebug, "debug", "d", false, "enable debug logging")
rootCmd.CompletionOptions.DisableDefaultCmd = true
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", "1h")
viper.SetDefault("mastodon.visibility", "private")
viper.SetDefault("humblebundle.pollinterval", "30m")
viper.SetDefault("logging.colored_output", false)
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)
}
}