-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon-default.go
72 lines (64 loc) · 1.71 KB
/
daemon-default.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
//go:build !remote
// +build !remote
package daemon
import (
"log/slog"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/virzz/vlog"
)
type Daemon struct {
logger *slog.Logger
systemd *Systemd
}
func EnableRemoteConfig(project string, publicKey ...string) error {
return std.EnableRemoteConfig(project, publicKey...)
}
func (d *Daemon) EnableRemoteConfig(project string, publicKey ...string) error {
panic("remote config build with remote tag")
}
func (d *Daemon) ExecuteE(action ActionFunc) error {
if std.logger == nil || std.systemd.logger == nil {
std.SetLogger(vlog.Log)
}
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) (err error) {
instance, _ := cmd.Flags().GetString("instance")
config, _ := cmd.Flags().GetString("config")
if config != "" {
viper.SetConfigFile(config)
} else {
viper.SetConfigType("json")
viper.AddConfigPath(".")
viper.SetConfigName("config_" + instance)
}
err = viper.ReadInConfig()
if err != nil {
vlog.Warn("Failed to read in config", "err", err.Error())
}
if registerConfig != nil {
err = viper.Unmarshal(registerConfig, func(dc *mapstructure.DecoderConfig) {
dc.TagName = "json"
})
if err != nil {
vlog.Error("Failed to unmarshal register config", "err", err.Error())
return err
}
}
return nil
}
rootCmd.RunE = action
viper.BindPFlags(rootCmd.PersistentFlags())
viper.BindPFlags(rootCmd.Flags())
viper.SetEnvPrefix(rootCmd.Use)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := rootCmd.Execute(); err != nil {
return err
}
return nil
}
func ExecuteE(action ActionFunc) error {
return std.ExecuteE(action)
}