-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
77 lines (60 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.aporeto.io/oidc-mock/internal/entrypoint"
"go.aporeto.io/oidc-mock/internal/versions"
)
func main() {
if err := oidcMockCmd.Execute(); err != nil {
panic(err)
}
}
var config = &entrypoint.Configuration{}
// oidcMockCmd represents the base command when called without any subcommands
var oidcMockCmd = &cobra.Command{
Use: "oidcmock [options]",
Short: "oidcmock command line interface",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
config.LogLevel = viper.GetString("log-level")
config.LogFormat = viper.GetString("log-format")
config.ServerIP = viper.GetString("server-ip")
config.ServerPort = viper.GetString("server-port")
config.TLS = viper.GetBool("tls")
config.DevelopmentMode = viper.GetBool("dev")
time.Local = time.UTC
return nil
},
RunE: func(ccmd *cobra.Command, args []string) (err error) {
if viper.GetBool("version") {
fmt.Println(versions.GetVersions())
return nil
}
entrypoint.StartServer(config)
return nil
},
}
func init() {
cobra.OnInitialize(initConfig)
// Defaults
oidcMockCmd.Flags().StringVar(&config.LogLevel, "log-level", "info", "Set the log-level between info, debug, trace")
oidcMockCmd.Flags().StringVar(&config.LogFormat, "log-format", "human", "Set the log-format between console, json")
oidcMockCmd.Flags().StringVar(&config.ServerIP, "server-ip", "127.0.0.1", "Set the default server ip")
oidcMockCmd.Flags().StringVar(&config.ServerPort, "server-port", ":6999", "Set the default server port")
oidcMockCmd.Flags().BoolVar(&config.TLS, "tls", false, "Enable if the server supports TLS")
oidcMockCmd.Flags().BoolVar(&config.DevelopmentMode, "dev", false, "Enable development mode")
oidcMockCmd.Flags().StringVar(&config.PrivateKeyPath, "private-key", ".data/oidc.rsa", "Set the default private key")
oidcMockCmd.Flags().StringVar(&config.PublicKeyPath, "public-key", ".data/oidc.rsa.pub", "Set the default public key")
oidcMockCmd.Flags().BoolP("version", "v", false, "Show version.")
if err := viper.BindPFlags(oidcMockCmd.Flags()); err != nil {
panic(err)
}
}
func initConfig() {
viper.SetEnvPrefix("OIDCMOCK")
viper.AutomaticEnv() // read in environment variables that match
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
}