setup flags and envvar

This commit is contained in:
Ronmi Ren 2025-01-06 16:53:02 +08:00
commit 46e9545dca
3 changed files with 18 additions and 12 deletions

View file

@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -39,10 +40,6 @@ func init() {
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.forgejo-pages.yaml)")
// 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.
@ -57,14 +54,16 @@ func initConfig() {
// Search config in home directory with name ".forgejo-pages" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigType("toml")
viper.SetConfigName(".forgejo-pages")
}
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())
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("PAGES")
viper.AutomaticEnv() // read in environment variables that match
}