-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon-default.go
More file actions
74 lines (67 loc) · 1.67 KB
/
daemon-default.go
File metadata and controls
74 lines (67 loc) · 1.67 KB
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
//go:build !remote
// +build !remote
package daemon
import (
"slices"
"strings"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type Daemon struct {
logger *zap.Logger
systemd *Systemd
}
func (d *Daemon) EnableRemote(project string, publicKey ...string) error {
panic("remote config build with remote tag")
}
func (d *Daemon) ExecuteE(action ActionFunc) error {
if !slices.ContainsFunc(rootCmd.Commands(),
func(cmd *cobra.Command) bool { return cmd.Use == "config" },
) {
rootCmd.AddCommand(configCmd)
}
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 {
viper.SetConfigType("yaml")
err = viper.ReadInConfig()
if err != nil {
d.logger.Warn("Failed to read in config", zap.Error(err))
}
}
if registerConfig != nil {
err = viper.Unmarshal(registerConfig, func(dc *mapstructure.DecoderConfig) {
dc.TagName = "json"
})
if err != nil {
d.logger.Error("Failed to unmarshal register config", zap.Error(err))
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
}