-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
104 lines (86 loc) · 2.79 KB
/
options.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
package parser
import (
"fmt"
"github.com/pkg/errors"
"github.com/projectdiscovery/goflags"
folderutil "github.com/projectdiscovery/utils/folder"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
)
var (
defaultSettingsLocation = filepath.Join(folderutil.HomeDirOrDefault("."), ".config/nmapParser/settings.yaml")
)
type Options struct {
SettingsFile string
Project string
ServiceJSON bool
HTTPDomains bool
All bool
BaseFolder string
Silent bool
Version bool
NoColor bool
Verbose bool
}
// ParseOptions parses the command line flags provided by a user
func ParseOptions() *Options {
options := &Options{}
var err error
flagSet := goflags.NewFlagSet()
flagSet.SetDescription(`get simple findings from the obtained information for the specified project`)
flagSet.CreateGroup("input", "Input",
flagSet.StringVarP(&options.Project, "project", "p", "", "project name for metadata addition"),
flagSet.BoolVarP(&options.ServiceJSON, "serviceJSON", "sj", false, "creates a service JSON file in the project folder"),
flagSet.BoolVarP(&options.HTTPDomains, "http_domains", "hd", false, "create additional combinations from ports for HTTP scanning"),
flagSet.BoolVarP(&options.All, "all", "a", true, "create all combinations from ports and hosts"),
)
flagSet.CreateGroup("debug", "Debug",
flagSet.BoolVar(&options.Silent, "silent", false, "show only results in output"),
flagSet.BoolVar(&options.Version, "version", false, "show version of the project"),
flagSet.BoolVar(&options.Verbose, "v", false, "show verbose output"),
flagSet.BoolVarP(&options.NoColor, "no-color", "nc", false, "disable colors in output"),
)
flagSet.CreateGroup("config", "Config",
flagSet.StringVar(&options.SettingsFile, "config", defaultSettingsLocation, "settings (Yaml) file location"),
)
if err := flagSet.Parse(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
options.configureOutput()
if options.Version {
fmt.Printf("Current Version: %s\n", VERSION)
os.Exit(0)
}
// Validate the options passed by the user and if any
// invalid options have been used, exit.
err = options.validateOptions()
if err != nil {
log.Fatalf("Program exiting: %v\n", err)
}
return options
}
func (options *Options) configureOutput() {
if options.Verbose {
log.SetLevel(logrus.TraceLevel)
}
if options.NoColor {
log.SetFormatter(&logrus.TextFormatter{
PadLevelText: true,
ForceColors: false,
DisableTimestamp: true,
})
}
if options.Silent {
log.SetLevel(logrus.PanicLevel)
}
}
// validateOptions validates the configuration options passed
func (options *Options) validateOptions() error {
// Both verbose and silent flags were used
if options.Verbose && options.Silent {
return errors.New("both verbose and silent mode specified")
}
return nil
}