forked from eliquious/kappa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kappa.go
98 lines (80 loc) · 2.44 KB
/
kappa.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package commands
import (
"fmt"
"io"
log "github.com/mgutz/logxi/v1"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// KappaCmd is the subsilent root command.
var KappaCmd = &cobra.Command{
Use: "kappa",
Short: "Kappa is a NoSQL database centered around replicated logs and views.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cmd.Help())
},
}
// Pointer to KappaCmd used in initialization
var kappaCmd *cobra.Command
// Execute is the main entry point into the server.
func Execute() {
AddCommands()
KappaCmd.Execute()
}
// AddCommands add all of the subcommands to the main entry point.
func AddCommands() {
KappaCmd.AddCommand(ServerCmd)
KappaCmd.AddCommand(InitCACmd)
KappaCmd.AddCommand(NewCertCmd)
}
// Command line args
var (
ConfigPath string
)
func init() {
KappaCmd.PersistentFlags().StringVarP(&ConfigPath, "config", "c", "", "Configuration file")
kappaCmd = KappaCmd
// for Bash auto-complete
validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
annotation := make(map[string][]string)
annotation[cobra.BashCompFilenameExt] = validConfigFilenames
KappaCmd.PersistentFlags().Lookup("config").Annotations = annotation
}
// InitializeMainConfig sets up the config options for the kappa command
func InitializeMainConfig(logger log.Logger) error {
viper.SetConfigFile("config")
viper.AddConfigPath(ConfigPath)
// Read configuration file
logger.Info("Reading configuration file")
err := viper.ReadInConfig()
if err != nil {
logger.Warn("Unable to locate configuration file.")
}
if kappaCmd.PersistentFlags().Lookup("config").Changed {
logger.Info("", "ConfigPath", ConfigPath)
viper.Set("ConfigPath", ConfigPath)
}
return nil
}
// InitializeConfig reads the configuration file and sets up the application settings via Viper.
func InitializeConfig(writer io.Writer) error {
logger := log.NewLogger(writer, "config")
if err := InitializeMainConfig(logger); err != nil {
logger.Warn("Failed to initialize kappa command line flags")
return err
}
if err := InitializeServerConfig(logger); err != nil {
logger.Warn("Failed to initialize server command line flags")
return err
}
if err := InitializeCertAuthConfig(logger); err != nil {
logger.Warn("Failed to initialize init-ca command line flags")
return err
}
if err := InitializeNewCertConfig(logger); err != nil {
logger.Warn("Failed to initialize new-cert command line flags")
return err
}
return nil
}