-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
100 lines (82 loc) · 2.51 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
package finder
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/simpleFinder/settings.yaml")
)
type Options struct {
SettingsFile string
Project string
BaseFolder string
Hosts bool
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(fmt.Sprintf("simpleFinder %s - Get findings from input files for a specific project", VERSION))
flagSet.CreateGroup("input", "Input",
flagSet.StringVarP(&options.Project, "project", "p", "", "project name for metadata addition"),
flagSet.BoolVarP(&options.Hosts, "hosts", "h", false, "identify interesting hosts"),
)
flagSet.CreateGroup("config", "Config",
flagSet.StringVar(&options.SettingsFile, "config", defaultSettingsLocation, "settings (Yaml) file location"),
)
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"),
)
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
}