generated from alecodes/base-template
feat: first commit
This commit is contained in:
commit
c8a37ed85c
11 changed files with 628 additions and 0 deletions
78
cmd/root.go
Normal file
78
cmd/root.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
miniflux_host string
|
||||
miniflux_token string
|
||||
)
|
||||
|
||||
// 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() {
|
||||
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")
|
||||
viper.BindPFlag("miniflux_host", rootCmd.PersistentFlags().Lookup("miniflux-host"))
|
||||
viper.BindPFlag("miniflux_token", rootCmd.PersistentFlags().Lookup("miniflux-token"))
|
||||
}
|
||||
|
||||
// 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())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue