-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
164 lines (151 loc) · 4.19 KB
/
cmd.go
File metadata and controls
164 lines (151 loc) · 4.19 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package systemd
import (
"fmt"
"os"
"os/user"
"github.com/spf13/cobra"
)
func (s *Systemd) Command(rootCmd *cobra.Command) {
var persistentPreRunE = func(cmd *cobra.Command, args []string) error {
_user, err := user.Current()
if err != nil {
return err
}
s.isRoot = _user.Gid == "0" || _user.Uid == "0"
return nil
}
var installCmd = &cobra.Command{
GroupID: "systemd",
Use: "install",
Short: "Systemd Install",
Aliases: []string{"i"},
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Install()
},
}
var removeCmd = &cobra.Command{
GroupID: "systemd",
Use: "remove",
Short: "Systemd Remove(Uninstall)",
Aliases: []string{"rm", "uninstall", "uni", "un"},
PersistentPreRunE: persistentPreRunE,
RunE: func(_ *cobra.Command, _ []string) error {
return s.Remove()
},
}
var startCmd = &cobra.Command{
GroupID: "systemd",
Use: "start [tag]...",
Short: "Systemd Start",
Aliases: []string{"run"},
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Start()
},
}
var stopCmd = &cobra.Command{
GroupID: "systemd",
Use: "stop",
Short: "Systemd Stop",
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Stop()
},
}
var enableCmd = &cobra.Command{
GroupID: "systemd",
Use: "enable",
Short: "Systemd Enable",
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Enable()
},
}
var disableCmd = &cobra.Command{
GroupID: "systemd",
Use: "disable",
Short: "Systemd Disable",
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Disable()
},
}
var restartCmd = &cobra.Command{
GroupID: "systemd",
Use: "restart",
Short: "Systemd Restart",
Aliases: []string{"r", "re"},
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Restart()
},
}
var killCmd = &cobra.Command{
GroupID: "systemd",
Use: "kill",
Short: "Systemd Kill",
Aliases: []string{"k"},
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Kill()
},
}
var reloadCmd = &cobra.Command{
GroupID: "systemd",
Use: "reload",
Short: "Systemd Reload",
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
return s.Reload()
},
}
var statusCmd = &cobra.Command{
GroupID: "systemd",
Use: "status",
Short: "Systemd Status",
Aliases: []string{"info", "if"},
PersistentPreRunE: persistentPreRunE,
RunE: func(_ *cobra.Command, _ []string) error {
_, err := s.Status(true)
return err
},
}
var unitCmd = &cobra.Command{
GroupID: "systemd",
Hidden: true,
Use: "unit",
Short: "print systemd unit service file",
PersistentPreRunE: persistentPreRunE,
RunE: func(cmd *cobra.Command, args []string) error {
if t, _ := cmd.Flags().GetBool("template"); t {
execPath, err := os.Executable()
if err != nil {
return err
}
buf, err := CreateUnit(s.Name, s.Description, execPath)
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
}
fn := s.UnitFilePath()
s.logger.Info("filepath = " + fn)
buf, err := os.ReadFile(fn)
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
},
}
// Daemon commands
rootCmd.AddGroup(&cobra.Group{ID: "systemd", Title: "Systemd commands"})
rootCmd.AddCommand(
installCmd, removeCmd, reloadCmd, unitCmd,
startCmd, stopCmd, killCmd, restartCmd, statusCmd,
enableCmd, disableCmd,
)
unitCmd.Flags().BoolP("template", "t", false, "Show template unit service file")
}