-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.go
84 lines (67 loc) · 2.14 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
// Copyright (c) 2022 dhn. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"errors"
"flag"
"io"
"os"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
)
// Options
type Options struct {
SearchString string // Client to find CIDRs for"
Silent bool // Show only results in output
Stdin bool // Stdin specifies whether stdin input was given to the process
Version bool // Version specifies if we should just show version and exit
JSON bool // JSON output
Output io.Writer
}
// ParseOptions parses the command line flags provided by a user
func ParseOptions() *Options {
options := &Options{}
flag.StringVar(&options.SearchString, "s", "", "Organization to find CIDRs for")
flag.BoolVar(&options.Silent, "silent", false, "Show only results in output")
flag.BoolVar(&options.Version, "version", false, "Show version of spk")
flag.BoolVar(&options.JSON, "json", false, "Print results as JSON")
flag.Parse()
options.Output = os.Stdout
options.Stdin = hasStdin()
options.configureOutput()
ShowBanner()
if options.Version {
gologger.Info().Msgf("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 {
gologger.Fatal().Msgf("Program exiting: %s\n", err)
}
return options
}
func hasStdin() bool {
stat, err := os.Stdin.Stat()
if err != nil {
return false
}
isPipedFromChrDev := (stat.Mode() & os.ModeCharDevice) == 0
isPipedFromFIFO := (stat.Mode() & os.ModeNamedPipe) != 0
return isPipedFromChrDev || isPipedFromFIFO
}
// configureOutput configures the output on the screen
func (options *Options) configureOutput() {
if options.Silent {
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
}
}
// validateOptions validates the configuration options passed
func (options *Options) validateOptions() error {
if options.SearchString == "" && !options.Stdin {
return errors.New("no search string provided")
}
return nil
}