-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.go
More file actions
112 lines (102 loc) · 2.89 KB
/
daemon.go
File metadata and controls
112 lines (102 loc) · 2.89 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package daemon
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"strings"
slogzap "github.com/samber/slog-zap/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
type ActionFunc func(cmd *cobra.Command, args []string) error
var (
std *Daemon
debug = false
registerConfig any = nil
rootCmd = &cobra.Command{
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
SilenceErrors: true,
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
panic("daemon action not implemented")
},
}
configCmd = &cobra.Command{
Use: "config json|yaml", Aliases: []string{"c"},
Short: "Show Config Template",
RunE: func(cmd *cobra.Command, args []string) error {
var buf []byte
var config any
if registerConfig != nil {
config = registerConfig
} else {
config = viper.AllSettings()
viper.Set("config", nil)
viper.Set("instance", nil)
}
if len(args) > 0 && (args[0] == "yaml" || args[0] == "yml") {
buf, _ = yaml.Marshal(config)
} else {
buf, _ = json.MarshalIndent(config, "", " ")
}
fmt.Println(string(buf))
return nil
},
}
)
func (d *Daemon) RegisterConfig(config any) { registerConfig = config }
func (d *Daemon) SetLogger(zlog *zap.Logger) {
d.logger = zlog.Named("daemon")
d.systemd.logger = d.logger.Named("systemd")
if debug {
viper.SetOptions(viper.WithLogger(
slog.New(slogzap.Option{
Level: slog.LevelDebug,
Logger: d.logger.Named("viper"),
}.NewZapHandler()),
))
viper.Debug()
}
}
func AddCommand(cmds ...*cobra.Command) { rootCmd.AddCommand(cmds...) }
func RootCmd() *cobra.Command { return rootCmd }
func SetLogger(log *zap.Logger) { std.SetLogger(log) }
func RegisterConfig(config any) { std.RegisterConfig(config) }
func SetAction(action ActionFunc) { rootCmd.RunE = action }
func SetDebug() { debug = true }
func EnableRemote(project string, publicKey ...string) error {
return std.EnableRemote(project, publicKey...)
}
func ExecuteE(action ActionFunc) error {
if std.logger == nil || std.systemd.logger == nil {
std.SetLogger(zap.L())
}
return std.ExecuteE(action)
}
func Execute(action ActionFunc) {
if err := ExecuteE(action); err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
}
// New - Create a new daemon
func New(appID, name, desc, version, commit string) *Daemon {
rootCmd.Use = name
rootCmd.Short = desc
rootCmd.Version = appID + " " + version + " " + commit
rootCmd.PersistentFlags().StringP("instance", "i", "default", "Get instance name from systemd template")
rootCmd.PersistentFlags().StringP("config", "c", "", "Set custom config file")
std = &Daemon{
systemd: &Systemd{
Name: strings.ToLower(name),
Description: desc,
Version: version,
AppID: appID,
},
}
std.systemd.Command(rootCmd)
return std
}