/
root.go
75 lines (64 loc) · 1.37 KB
/
root.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
package cmd
import (
"fmt"
"net/http"
"net/url"
"path"
"runtime"
"strings"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type RootOptions struct {
Debug bool
}
var (
rootOptions = RootOptions{}
log *logrus.Logger
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
origin, ok := r.Header["Origin"]
if !ok {
return true
}
u, _ := url.Parse(origin[0])
host := r.Host
if strings.HasPrefix(u.Host, host) {
return true
}
return false
},
}
rootCmd = &cobra.Command{
Use: "machine-status",
Short: "machine-status is a simple dashboard for multiple machines with nvidia GPU",
Long: `machine-status is a simple dashboard for multiple machines with nvidia GPU`,
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func init() {
log = logrus.New()
log.SetReportCaller(true)
log.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", path.Base(f.Function)),
fmt.Sprintf(" %s:%d", filename, f.Line)
},
}
rootCmd.PersistentFlags().BoolVar(
&rootOptions.Debug, "debug",
false, "Debug mode")
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}