-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (66 loc) · 2.04 KB
/
main.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
package main
import (
"collector/config.go"
"collector/module"
"collector/utils"
"fmt"
"log"
"os"
"path/filepath"
)
func main() {
path, err := utils.GetMyPath()
if err != nil {
log.Fatalf("Failed to get current path: %v", err)
}
beautyLogger := utils.GetBeautyLogger(filepath.Join(path, "logs", config.SERV_LOG))
beautyLogger.PrintBanner()
if len(os.Args) > 1 {
if isValid := config.ValidateParams(os.Args); !isValid {
beautyLogger.WriteFatal("Invalid parameters", fmt.Errorf("invalid parameters"))
}
action := os.Args[1]
typ := config.Collector(os.Args[2])
host := ""
connectionKey := ""
if len(os.Args) > 4 {
host = os.Args[3]
connectionKey = os.Args[4]
}
collector := module.GetCollectorProcess(typ, host, connectionKey, beautyLogger)
switch action {
case "run":
err := collector.Run()
if err != nil {
beautyLogger.WriteFatal(fmt.Sprintf("Error running %s collector:", typ), err)
}
case "install":
beautyLogger.WriteSimpleMessage(fmt.Sprintf("Installing %s collector", typ))
config.SaveConfig(&config.ServiceTypeConfig{CollectorType: typ})
err := collector.Install()
if err != nil {
beautyLogger.WriteFatal(fmt.Sprintf("Error installing %s collector:", typ), err)
} else {
beautyLogger.WriteSimpleMessage(fmt.Sprintf("%s collector installed successfully", typ))
}
case "uninstall":
beautyLogger.WriteSimpleMessage(fmt.Sprintf("Uninstalling %s collector", typ))
err := collector.Uninstall()
if err != nil {
beautyLogger.WriteFatal(fmt.Sprintf("Error uninstalling %s collector:", typ), err)
} else {
beautyLogger.WriteSimpleMessage(fmt.Sprintf("%s collector uninstalled successfully", typ))
}
}
} else {
cnf, err := config.ReadConfig()
if err != nil {
beautyLogger.WriteFatal("Error reading config", err)
}
collector := module.GetCollectorProcess(config.Collector(cnf.CollectorType), "", "", beautyLogger)
err = collector.Run()
if err != nil {
beautyLogger.WriteFatal(fmt.Sprintf("Error running %s collector:", cnf.CollectorType), err)
}
}
}