forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
207 lines (188 loc) · 4.33 KB
/
commands.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/statping/statping/utils"
"io"
"os"
"os/exec"
)
var rootCmd = &cobra.Command{
Use: "statping",
Version: VERSION,
Short: "A simple Application Status Monitor that is opensource and lightweight.",
Run: func(cmd *cobra.Command, args []string) {
start()
},
}
var updateCmd = &cobra.Command{
Use: "update",
Example: "statping update",
Short: "Update to the latest version",
RunE: func(cmd *cobra.Command, args []string) error {
log.Infoln("Updating Statping to the latest version...")
log.Infoln("curl -o- -L https://statping.com/install.sh | bash")
curl, err := exec.LookPath("curl")
if err != nil {
return err
}
bash, err := exec.LookPath("bash")
if err != nil {
return err
}
ree := bytes.NewBuffer(nil)
c1 := exec.Command(curl, "-o-", "-L", "https://statping.com/install.sh")
c2 := exec.Command(bash)
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
io.Copy(ree, &b2)
log.Infoln(ree.String())
os.Exit(0)
return nil
},
}
var versionCmd = &cobra.Command{
Use: "version",
Example: "statping version",
Short: "Print the version number of Statping",
Run: func(cmd *cobra.Command, args []string) {
if COMMIT != "" {
fmt.Printf("%s (%s)\n", VERSION, COMMIT)
} else {
fmt.Printf("%s\n", VERSION)
}
os.Exit(0)
},
}
var systemctlCmd = &cobra.Command{
Use: "systemctl [install/uninstall]",
Example: "statping systemctl install",
Short: "Install or Uninstall systemctl services",
RunE: func(cmd *cobra.Command, args []string) error {
if args[1] == "install" {
if len(args) < 3 {
return errors.New("requires 'install <working_path> <port>'")
}
}
port := utils.ToInt(args[2])
if port == 0 {
port = 80
}
if err := systemctlCli(args[1], args[0] == "uninstall", port); err != nil {
return err
}
os.Exit(0)
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("requires 'install <working_path>' or 'uninstall' as arguments")
}
return nil
},
}
var assetsCmd = &cobra.Command{
Use: "assets",
Example: "statping assets",
Short: "Dump all assets used locally to be edited",
RunE: func(cmd *cobra.Command, args []string) error {
if err := assetsCli(); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var exportCmd = &cobra.Command{
Use: "export",
Example: "statping export",
Short: "Exports your Statping settings to a 'statping-export.json' file.",
RunE: func(cmd *cobra.Command, args []string) error {
if err := exportCli(args); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var sassCmd = &cobra.Command{
Use: "sass",
Example: "statping sass",
Short: "Compile .scss files into the css directory",
RunE: func(cmd *cobra.Command, args []string) error {
if err := sassCli(); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var envCmd = &cobra.Command{
Use: "env",
Example: "statping env",
Short: "Return the configs that will be ran",
RunE: func(cmd *cobra.Command, args []string) error {
if err := envCli(); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var resetCmd = &cobra.Command{
Use: "reset",
Example: "statping reset",
Short: "Start a fresh copy of Statping",
RunE: func(cmd *cobra.Command, args []string) error {
if err := resetCli(); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var onceCmd = &cobra.Command{
Use: "once",
Example: "statping once",
Short: "Check all services 1 time and then quit",
RunE: func(cmd *cobra.Command, args []string) error {
if err := onceCli(); err != nil {
return err
}
os.Exit(0)
return nil
},
}
var importCmd = &cobra.Command{
Use: "import [.json file]",
Example: "statping import backup.json",
Short: "Imports settings from a previously saved JSON file.",
RunE: func(cmd *cobra.Command, args []string) error {
if err := importCli(args); err != nil {
return err
}
os.Exit(0)
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires input file (.json)")
}
return nil
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
exit(err)
}
}