forked from BountySecurity/gbounty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Body for parsing config from command-line args
- Loading branch information
Showing
11 changed files
with
1,110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/bountysecurity/gbounty/internal/platform/cli" | ||
) | ||
|
||
func Run() error { | ||
cfg, err := parseCLIArgs() | ||
if err != nil || cfg.ShowHelp { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseCLIArgs() (cli.Config, error) { | ||
cliConfig, err := cli.Parse(os.Args) | ||
if err != nil { | ||
return cli.Config{}, err | ||
} | ||
|
||
if cliConfig.ShowHelp { | ||
return cliConfig, nil | ||
} | ||
|
||
if err := cliConfig.Validate(); err != nil { | ||
return cli.Config{}, err | ||
} | ||
|
||
return cliConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cli | ||
|
||
import ( | ||
"flag" | ||
"io" | ||
"os" | ||
|
||
"github.com/bountysecurity/gbounty/kit/getopt" | ||
) | ||
|
||
const ( | ||
Output = "output" | ||
Debug = "debug" | ||
) | ||
|
||
func Parse(args []string) (Config, error) { | ||
config := Config{} | ||
|
||
fs := getopt.NewFlagSet(args[0], flag.ContinueOnError) | ||
fs.SetOutput(io.Discard) | ||
|
||
// No group | ||
fs.BoolVar("", &config.ShowHelp, "help", false, "Show help") | ||
fs.Alias("h", "help") | ||
|
||
// Output | ||
fs.InitGroup(Output, "OUTPUT OPTIONS:") | ||
fs.StringVar(Output, &config.OutPath, "output", "", "Determines the path where the output file will be stored to\n\tBy default, the output file is formatted as plain text") | ||
fs.Alias("o", "output") | ||
json := fs.Bool(Output, "json", false, "If specified, the output file will be JSON-formatted\n\tBy default, the output file is formatted as plain text") | ||
fs.Alias("j", "json") | ||
markdown := fs.Bool(Output, "markdown", false, "If specified, the output file will be Markdown-formatted\n\tBy default, the output file is formatted as plain text") | ||
fs.Alias("md", "markdown") | ||
|
||
// Debug | ||
fs.InitGroup(Debug, "DEBUG OPTIONS:") | ||
fs.BoolVar(Debug, &config.Verbosity.Warn, "verbose", false, "If specified, the internal logger will write warning and error log messages") | ||
fs.Alias("v", "verbose") | ||
fs.BoolVar(Debug, &config.Verbosity.Info, "verbose-extra", false, "If specified, the internal logger will write info, warning and error log messages") | ||
fs.Alias("vv", "verbose-extra") | ||
fs.BoolVar(Debug, &config.Verbosity.Debug, "verbose-all", false, "If specified, the internal logger will write debug, info, warning and error log messages") | ||
fs.Alias("vvv", "verbose-all") | ||
fs.StringVar(Debug, &config.Verbosity.Output, "verbose-output", "", "If specified, the internal logger will write the log messages to a file") | ||
fs.Alias("vout", "verbose-output") | ||
|
||
fs.SetUsage(` | ||
Usage: | ||
gbounty [flags] | ||
Flags:`) | ||
|
||
if err := fs.Parse(os.Args[1:]); err != nil { | ||
return Config{}, err | ||
} | ||
|
||
if config.ShowHelp { | ||
fs.SetOutput(os.Stdout) | ||
fs.PrintDefaults() | ||
} | ||
|
||
switch { | ||
case *json: | ||
config.OutFormat = "json" | ||
case *markdown: | ||
config.OutFormat = "markdown" | ||
default: | ||
config.OutFormat = "plain" | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cli | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/bountysecurity/gbounty/kit/logger" | ||
) | ||
|
||
const ( | ||
defaultParamsSplit = 10 | ||
defaultParamsMethod = http.MethodGet | ||
defaultParamsEncode = "url" | ||
) | ||
|
||
type Verbosity struct { | ||
Debug bool | ||
Info bool | ||
Warn bool | ||
Output string | ||
} | ||
|
||
func (v Verbosity) Level() logger.Level { | ||
switch { | ||
case v.Debug: | ||
return logger.LevelDebug | ||
case v.Info: | ||
return logger.LevelInfo | ||
case v.Warn: | ||
return logger.LevelWarn | ||
} | ||
|
||
return logger.LevelDisabled | ||
} | ||
|
||
type Config struct { | ||
// ShowHelp determines whether | ||
// the help flag has been provided. | ||
ShowHelp bool | ||
// OutPath specifies the path where | ||
// the scan output will be written to. | ||
OutPath string | ||
// OutFormat specifies the format | ||
// the scan output will be written. | ||
OutFormat string | ||
// Verbosity determines the level of | ||
// verbosity for the internal logger. | ||
Verbosity Verbosity | ||
} | ||
|
||
func (cfg Config) Validate() error { | ||
for _, validation := range []func() error{} { | ||
if err := validation(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.