setup flags and envvar
This commit is contained in:
parent
cd148b9fa6
commit
46e9545dca
3 changed files with 18 additions and 12 deletions
13
cmd/root.go
13
cmd/root.go
|
|
@ -6,6 +6,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
@ -39,10 +40,6 @@ func init() {
|
||||||
// will be global for your application.
|
// will be global for your application.
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.forgejo-pages.yaml)")
|
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.
|
// 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).
|
// Search config in home directory with name ".forgejo-pages" (without extension).
|
||||||
viper.AddConfigPath(home)
|
viper.AddConfigPath(home)
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("toml")
|
||||||
viper.SetConfigName(".forgejo-pages")
|
viper.SetConfigName(".forgejo-pages")
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
viper.SetEnvPrefix("PAGES")
|
||||||
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
cmd/serve.go
15
cmd/serve.go
|
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
"git.ronmi.tw/ronmi/forgejo-pages/lib"
|
"git.ronmi.tw/ronmi/forgejo-pages/lib"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveCmd represents the serve command
|
// serveCmd represents the serve command
|
||||||
|
|
@ -22,12 +23,17 @@ var serveCmd = &cobra.Command{
|
||||||
Short: "Start the static page server.",
|
Short: "Start the static page server.",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// check flags
|
// check flags
|
||||||
bind, _ := cmd.Flags().GetString("bind")
|
bind := viper.GetString("bind")
|
||||||
server, _ := cmd.Flags().GetString("server")
|
server := viper.GetString("server")
|
||||||
token, _ := cmd.Flags().GetString("token")
|
token := viper.GetString("token")
|
||||||
branch, _ := cmd.Flags().GetString("branch")
|
branch := viper.GetString("branch")
|
||||||
if bind == "" || server == "" || token == "" || branch == "" {
|
if bind == "" || server == "" || token == "" || branch == "" {
|
||||||
fmt.Println("bind, server token and branch are required")
|
fmt.Println("bind, server token and branch are required")
|
||||||
|
fmt.Println("dumping flags:")
|
||||||
|
fmt.Println(" bind: ", bind)
|
||||||
|
fmt.Println(" server: ", server)
|
||||||
|
fmt.Println(" token: ", token)
|
||||||
|
fmt.Println(" branch: ", branch)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
serverUrl, err := url.Parse(server)
|
serverUrl, err := url.Parse(server)
|
||||||
|
|
@ -75,4 +81,5 @@ func init() {
|
||||||
f.StringP("server", "s", "", "Forgejo server address")
|
f.StringP("server", "s", "", "Forgejo server address")
|
||||||
f.StringP("token", "k", "", "Forgejo api token")
|
f.StringP("token", "k", "", "Forgejo api token")
|
||||||
f.StringP("branch", "b", "static-pages", "branch to use")
|
f.StringP("branch", "b", "static-pages", "branch to use")
|
||||||
|
viper.BindPFlags(f)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -4,6 +4,7 @@ go 1.21.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,7 +20,6 @@ require (
|
||||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||||
github.com/spf13/afero v1.11.0 // indirect
|
github.com/spf13/afero v1.11.0 // indirect
|
||||||
github.com/spf13/cast v1.6.0 // indirect
|
github.com/spf13/cast v1.6.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
|
||||||
github.com/subosito/gotenv v1.6.0 // indirect
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
go.uber.org/multierr v1.9.0 // indirect
|
go.uber.org/multierr v1.9.0 // indirect
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue