-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.go
161 lines (133 loc) · 3.56 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
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
package main
import (
"fmt"
"os"
"runtime/debug"
"github.com/RedTeamPentesting/monsoon/cmd/fuzz"
"github.com/RedTeamPentesting/monsoon/cmd/list"
"github.com/RedTeamPentesting/monsoon/cmd/show"
"github.com/spf13/cobra"
)
var version = ""
var cmdRoot = &cobra.Command{
Use: "monsoon COMMAND [options]",
SilenceErrors: true,
SilenceUsage: true,
}
var cmdVersion = &cobra.Command{
Use: "version",
Short: "Print the current version",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("monsoon " + buildVersionString(version))
},
}
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: "To load completions",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
return cmdRoot.GenBashCompletionV2(os.Stdout, true)
case "zsh":
return cmdRoot.GenZshCompletion(os.Stdout)
case "fish":
return cmdRoot.GenFishCompletion(os.Stdout, true)
case "powershell":
return cmdRoot.GenPowerShellCompletionWithDesc(os.Stdout)
default:
return fmt.Errorf("unsupported argument: %q", args[0])
}
},
}
func init() {
// configure cobra help texts
setupHelp(cmdRoot)
fuzz.AddCommand(cmdRoot)
show.AddCommand(cmdRoot)
list.AddCommand(cmdRoot)
cmdRoot.AddCommand(cmdVersion)
cmdRoot.AddCommand(completionCmd)
}
func injectDefaultCommand(args []string) []string {
validCommands := make(map[string]struct{})
for _, cmd := range cmdRoot.Commands() {
validCommands[cmd.Name()] = struct{}{}
}
// check that there's a command in the arguments
for _, arg := range args {
if _, ok := validCommands[arg]; ok {
// valid command found, nothing to do
return args
}
if arg == "-h" || arg == "--help" || arg == "help" {
// request for help found, do not inject default command
return args
}
}
// inject default command as first argument
fmt.Fprintf(os.Stderr, "no command found, assuming 'monsoon fuzz'\n\n")
args = append([]string{"fuzz"}, args...)
return args
}
func main() {
resetTerminal := prepareTerminal()
defer resetTerminal()
os.Args = append(os.Args[:1], injectDefaultCommand(os.Args[1:])...)
cmdRoot.SetArgs(os.Args[1:])
err := cmdRoot.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
resetTerminal()
os.Exit(1)
}
}
func buildVersionString(compileTimeVersion string) string {
fallback := compileTimeVersion
if fallback == "" {
fallback = "unknown version"
}
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return fallback
}
buildSetting := func(key string) (string, bool) {
for _, setting := range buildInfo.Settings {
if setting.Key == key {
return setting.Value, true
}
}
return "", false
}
vcs, ok := buildSetting("vcs")
if !ok {
return fallback
}
commit, _ := buildSetting("vcs.revision")
if !ok {
return version
}
var dirtyTag string
dirty, ok := buildSetting("vcs.modified")
if ok && dirty == "true" && commit != "" {
dirtyTag = " (dirty)"
}
if compileTimeVersion != "" {
versionString := compileTimeVersion
if commit != "" {
versionString += "-" + shortCommit(commit) + dirtyTag
}
return versionString
}
return fmt.Sprintf("built from %s revision %s%s", vcs, commit, dirtyTag)
}
func shortCommit(commit string) string {
if len(commit) < 8 {
return commit
}
return commit[:8]
}