-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (102 loc) · 2.75 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
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
package main
import (
"os"
"portchecker/utils"
"portchecker/module"
"fmt"
"flag"
"github.com/alexflint/go-arg"
"portchecker/conf"
"errors"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: \n" +
"\tportchecker apiserver\n" +
"or\n" +
"\tportchecker graphviz\n" +
"or\n" +
"\tportchecker check-agent --mapping_file_url<mappingFileUrl> --analysisid <analysisid>\n" +
"or\n" +
"\tportchecker probe-agent --analysisid <analysisid>\n\nParameters desc:\n")
flag.PrintDefaults()
os.Exit(2)
}
//
//func init() {
// config := conf.NewConfig()
// config.Mode = *flag.String("mode", "apiserver", "portchecker mode in [apiserver|graphviz|check-agent|probe-agent]")
// config.AnalysisId = *flag.Int( "analysisid", 1, "unique anlysis id")
// config.ConfigFilePath = *flag.String( "mapping_file_url", "", "Local path to mapping description file")
// config.Verbose = *flag.Bool("verbose", false, "Log verbose mode")
//}
func main() {
config := conf.NewConfig()
arg.MustParse(&config)
flag.Usage = usage
flag.Parse()
paramErrors := checkParam(config)
if paramErrors != nil {
fmt.Fprintf(os.Stderr, "Invalid parameters %s\n", paramErrors)
os.Exit(1)
}
if config.Verbose {
fmt.Println(fmt.Sprintf("Ask to start in %v mode", config.Mode))
}
switch config.Mode {
case "check-agent":
networkConfig, err := utils.UnmarshallFromFile(config.ConfigFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot load config from given path : %v. Err : %v", config.ConfigFilePath, err)
os.Exit(1)
}
checkResult, err := module.StartCheckAgent(*networkConfig, config)
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred %v", err)
}
err = module.ProcessCheckAgentResult(*networkConfig, *checkResult, config)
case "probe-agent":
module.ProbeAgent(config)
case "apiserver":
module.StartApiServer(config)
case "graphviz":
fmt.Fprintf(os.Stderr, "Not implemented\n")
default:
fmt.Fprintf(os.Stderr, "Invalid mode %s\n", config.Mode)
os.Exit(1)
}
os.Exit(0)
}
func checkParam(config conf.Config) error {
var required []string
switch config.Mode {
case "check-agent":
required = []string{"mapping_file_url", "analysisid"}
case "probe-agent":
required = []string{"analysisid"}
case "apiserver":
required = []string{}
case "graphviz":
required = []string{}
default:
required = []string{}
}
flag.Parse()
seen := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
if len(required) > 0 {
return nil
}
missings := ""
for _, req := range required {
if !seen[req] {
// or possibly use `log.Fatalf` instead of:
msg := fmt.Sprintf("ERROR: missing required -%s argument/flag\n\n", req)
missings += "\n" + msg
}
}
if len(missings) > 0 {
return errors.New(missings)
} else {
return nil
}
}