2023-07-03 15:30:14 +02:00
|
|
|
/*
|
|
|
|
* 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/app"
|
2023-07-04 16:56:09 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/constants"
|
2023-07-03 15:30:14 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/db"
|
2023-07-04 16:56:09 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/log"
|
2023-07-07 09:32:01 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/misc"
|
2023-07-04 16:56:09 +02:00
|
|
|
log2 "log"
|
2023-07-03 15:30:14 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "humble-bot",
|
|
|
|
Short: "A simple daemon posting new Humble Bundles on Mastodon",
|
|
|
|
Long: `This app regularly checks for new Humble Bundles and posts updates on Mastodon`,
|
|
|
|
Run: app.RunRoot,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2023-07-07 09:32:01 +02:00
|
|
|
cobra.OnInitialize(initConfig, db.Initialize, log.InitializeLogger, misc.CheckConfig)
|
2023-07-03 15:30:14 +02:00
|
|
|
|
2023-07-06 07:02:46 +02:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $workingdir/config.yaml)")
|
2023-07-04 16:56:09 +02:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&log.FlagDebug, "debug", "d", false, "enable debug logging")
|
2023-07-03 15:30:14 +02:00
|
|
|
|
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
2023-07-04 16:56:09 +02:00
|
|
|
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
viper.SetDefault("mastodon.postinterval", "5m")
|
2023-07-07 09:32:01 +02:00
|
|
|
viper.SetDefault("mastodon.visibility", "public")
|
2023-07-04 16:56:09 +02:00
|
|
|
viper.SetDefault("humblebundle.pollinterval", "30m")
|
2023-07-03 15:30:14 +02:00
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2023-07-04 16:56:09 +02:00
|
|
|
log2.Printf("Starting humble-bot %v with config file: %v\n", constants.AppVersion, viper.ConfigFileUsed())
|
2023-07-03 15:30:14 +02:00
|
|
|
} else {
|
2023-07-04 16:56:09 +02:00
|
|
|
log2.Printf("Can't find config file: %v\n", viper.ConfigFileUsed())
|
|
|
|
os.Exit(1)
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
|
|
|
}
|