Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove --help, fix help command for flags #3302

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/Makefile.tracee-container
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ help:
@echo ""
@echo " $$ make -f builder/Makefile.tracee-container build-tracee"
@echo ""
@echo " $$ make -f builder/Makefile.tracee-container run-tracee ARG=\"--help\""
@echo " $$ make -f builder/Makefile.tracee-container run-tracee ARG=\"help\""
@echo ""
@echo " > This will run tracee using provided arguments."
@echo ""
Expand Down
19 changes: 0 additions & 19 deletions cmd/tracee/cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,6 @@ import (
func init() {
rootCmd.AddCommand(analyze)

hfFallback := rootCmd.HelpFunc()
// Override default help function to support help for flags.
// Since for commands the usage is tracee help <command>, for flags
// the usage is tracee --help <flag>.
// e.g. tracee analyze --help rego
// tracee analyze -h rego
analyze.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if len(args) > 2 && (args[1] == "--help" || args[1] == "-h") {
flagHelp := flags.GetHelpString(args[2], true)
if flagHelp != "" {
fmt.Fprintf(os.Stdout, "%s\n", flagHelp)
os.Exit(0)
}
}

// If flag help was not found, fallback to default help function
hfFallback(cmd, args)
})

// flags

// events
Expand Down
80 changes: 80 additions & 0 deletions cmd/tracee/cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/aquasecurity/tracee/pkg/cmd/flags"
"github.com/aquasecurity/tracee/pkg/logger"
)

func init() {
// override the default help command
rootCmd.SetHelpCommand(helpCmd)
// use custom usage template
rootCmd.SetUsageTemplate(customUsageTemplate)
}

var customUsageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}

Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}

Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}

{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}

Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use:
"{{.CommandPath}} [command] --help" for more information about a command.
"{{.CommandPath}} help [command|flag]" for more information about a command or flag.{{end}}
`

var helpCmd = &cobra.Command{
Use: "help [command|flag]",
Short: "Help about any command or flag",
Hidden: false,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
// check if the argument is a flag
if flagHelp := flags.GetHelpString(args[0], true); flagHelp != "" {
fmt.Fprintf(os.Stdout, "%s\n", flagHelp)
return
}

// check if the argument is a command
for _, cmd := range rootCmd.Commands() {
if cmd.Name() == args[0] {
if err := cmd.Help(); err != nil {
logger.Errorw("failed to print help for command", "command", cmd.Name(), "error", err)
os.Exit(1)
}
return
}
}
}

// use the default help function
cmd.Root().HelpFunc()(cmd.Root(), args)
},
}
20 changes: 0 additions & 20 deletions cmd/tracee/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/spf13/viper"

cmdcobra "github.com/aquasecurity/tracee/pkg/cmd/cobra"
"github.com/aquasecurity/tracee/pkg/cmd/flags"
"github.com/aquasecurity/tracee/pkg/cmd/flags/server"
"github.com/aquasecurity/tracee/pkg/cmd/initialize"
"github.com/aquasecurity/tracee/pkg/errfmt"
Expand Down Expand Up @@ -56,25 +55,6 @@ func initCmd() error {

cobra.OnInitialize(initConfig)

hfFallback := rootCmd.HelpFunc()
// Override default help function to support help for flags.
// Since for commands the usage is tracee help <command>, for flags
// the usage is tracee --help <flag>.
// e.g. tracee --help filter
// tracee -h filter
rootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if len(args) > 1 && (args[0] == "--help" || args[0] == "-h") {
flagHelp := flags.GetHelpString(args[1], true)
if flagHelp != "" {
fmt.Fprintf(os.Stdout, "%s\n", flagHelp)
os.Exit(0)
}
}

// If flag help was not found, fallback to default help function
hfFallback(cmd, args)
})

// Filter/Policy flags

// filter is not bound to viper
Expand Down
2 changes: 1 addition & 1 deletion docs/contributing/building/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ the "run" targets:
!!! note
Tracee arguments are passed through the `ARG` variable:
```console
make -f builder/Makefile.tracee-container run-tracee ARG="--help"
make -f builder/Makefile.tracee-container run-tracee ARG="help"
```
2 changes: 1 addition & 1 deletion docs/docs/deep-dive/caching-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tracee has an events caching (in-memory) mechanism. In order to check latest
caching options you may execute:

```console
./dist/tracee --help cache
./dist/tracee help cache
```

!!! Read Important
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/deep-dive/dropping-capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ does is through different "execution protection rings":
You may see all available capabilities in the running environment by running:

```console
--help capabilities
help capabilities
```

command line flag.
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/filters/filtering.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Tracing Event Filtering

```console
sudo ./dist/tracee --help filter
sudo ./dist/tracee help filter
sudo ./dist/tracee --filter xxx
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/forensics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tracee has a unique feature that lets you capture interesting artifacts from
running applications, using the `--capture` flag.

```console
sudo ./dist/tracee --help capture
sudo ./dist/tracee help capture
sudo ./dist/tracee --capture xxx
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/integrating/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ through the following URLs:
**tracee** can be scraped through `:3366/metrics`

> Metrics addresses can be changed through **tracee** command line
> arguments `metrics` and `listen-addr`, check `--help` for more information.
> arguments `metrics` and `listen-addr`, check `help` for more information.

!!! Tip
Check [this tutorial] for more information as well.
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/outputs/output-formats.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tracing Output Formats

The `--output` flag controls where and how Tracee will output events, by specifying `--output <format>:<destination>`. You can use the `--output` flag multiple times to output events in multiple ways. To see all output options you can run `tracee --help output`.
The `--output` flag controls where and how Tracee will output events, by specifying `--output <format>:<destination>`. You can use the `--output` flag multiple times to output events in multiple ways. To see all output options you can run `tracee help output`.

The following output formats are supported:

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/outputs/output-options.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tracing Output Options

Tracee supports different output options for customizing the way events are printed. For a complete list of available options, run `tracee --help output`.
Tracee supports different output options for customizing the way events are printed. For a complete list of available options, run `tracee help output`.

Available options:

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/flags/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func PrepareCapture(captureSlice []string, newBinary bool) (config.CaptureConfig
}
} else {
if newBinary {
return config.CaptureConfig{}, errfmt.Errorf("invalid capture option specified, use '--help capture' for more info")
return config.CaptureConfig{}, errfmt.Errorf("invalid capture option specified, use 'help capture' for more info")
}

return config.CaptureConfig{}, errfmt.Errorf("invalid capture option specified, use '--capture help' for more info")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/flags/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func InvalidEventExcludeError(event string) error {

func InvalidFilterOptionError(expr string, newBinary bool) error {
if newBinary {
return fmt.Errorf("invalid filter option specified (%s), use '--help filter' for more info", expr)
return fmt.Errorf("invalid filter option specified (%s), use 'help filter' for more info", expr)
}

return fmt.Errorf("invalid filter option specified (%s), use '--filter help' for more info", expr)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/flags/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func invalidLogOption(err error, opt string, newBinary bool) error {
}

if newBinary {
return errfmt.Errorf("invalid log option: %s, %s, use '--help log' for more info", opt, err)
return errfmt.Errorf("invalid log option: %s, %s, use 'help log' for more info", opt, err)
}

return errfmt.Errorf("invalid log option: %s, %s, use '--log help' for more info", opt, err)
Expand All @@ -63,7 +63,7 @@ func invalidLogOptionValue(err error, opt string, newBinary bool) error {
}

if newBinary {
return errfmt.Errorf("invalid log option value: %s, %s, use '--help log' for more info", opt, err)
return errfmt.Errorf("invalid log option value: %s, %s, use 'help log' for more info", opt, err)
}

return errfmt.Errorf("invalid log option value: %s, %s, use '--log help' for more info", opt, err)
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/flags/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func PrepareOutput(outputSlice []string, newBinary bool) (PrepareOutputResult, e
case "none":
if len(outputParts) > 1 {
if newBinary {
return outConfig, errors.New("none output does not support path. Use '--help output' for more info")
return outConfig, errors.New("none output does not support path. Use 'help output' for more info")
}

return outConfig, errors.New("none output does not support path. Use '--output help' for more info")
Expand Down Expand Up @@ -115,7 +115,7 @@ func PrepareOutput(outputSlice []string, newBinary bool) (PrepareOutputResult, e
}
default:
if newBinary {
return outConfig, fmt.Errorf("invalid output flag: %s, use '--help output' for more info", outputParts[0])
return outConfig, fmt.Errorf("invalid output flag: %s, use 'help output' for more info", outputParts[0])
}

return outConfig, fmt.Errorf("invalid output flag: %s, use '--output help' for more info", outputParts[0])
Expand Down Expand Up @@ -158,7 +158,7 @@ func setOption(cfg *config.OutputConfig, option string, newBinary bool) error {
cfg.EventsSorting = true
default:
if newBinary {
return errfmt.Errorf("invalid output option: %s, use '--help output' for more info", option)
return errfmt.Errorf("invalid output option: %s, use 'help output' for more info", option)
}

return errfmt.Errorf("invalid output option: %s, use '--output help' for more info", option)
Expand Down Expand Up @@ -209,15 +209,15 @@ func parseFormat(outputParts []string, printerMap map[string]string, newBinary b
for _, outPath := range strings.Split(outputParts[1], ",") {
if outPath == "" {
if newBinary {
return errfmt.Errorf("format flag can't be empty, use '--help output' for more info")
return errfmt.Errorf("format flag can't be empty, use 'help output' for more info")
}

return errfmt.Errorf("format flag can't be empty, use '--output help' for more info")
}

if _, ok := printerMap[outPath]; ok {
if newBinary {
return errfmt.Errorf("cannot use the same path for multiple outputs: %s, use '--help output' for more info", outPath)
return errfmt.Errorf("cannot use the same path for multiple outputs: %s, use 'help output' for more info", outPath)
}

return errfmt.Errorf("cannot use the same path for multiple outputs: %s, use '--output help' for more info", outPath)
Expand All @@ -232,7 +232,7 @@ func parseFormat(outputParts []string, printerMap map[string]string, newBinary b
func parseOption(outputParts []string, traceeConfig *config.OutputConfig, newBinary bool) error {
if len(outputParts) == 1 || outputParts[1] == "" {
if newBinary {
return errfmt.Errorf("option flag can't be empty, use '--help output' for more info")
return errfmt.Errorf("option flag can't be empty, use 'help output' for more info")
}

return errfmt.Errorf("option flag can't be empty, use '--output help' for more info")
Expand Down Expand Up @@ -272,7 +272,7 @@ func createFile(path string) (*os.File, error) {
func validateURL(outputParts []string, flag string, newBinary bool) error {
if len(outputParts) == 1 || outputParts[1] == "" {
if newBinary {
return errfmt.Errorf("%s flag can't be empty, use '--help output' for more info", flag)
return errfmt.Errorf("%s flag can't be empty, use 'help output' for more info", flag)
}

return errfmt.Errorf("%s flag can't be empty, use '--output help' for more info", flag)
Expand All @@ -282,7 +282,7 @@ func validateURL(outputParts []string, flag string, newBinary bool) error {

if err != nil {
if newBinary {
return errfmt.Errorf("invalid uri for %s output %q. Use '--help output' for more info", flag, outputParts[1])
return errfmt.Errorf("invalid uri for %s output %q. Use 'help output' for more info", flag, outputParts[1])
}

return errfmt.Errorf("invalid uri for %s output %q. Use '--output help' for more info", flag, outputParts[1])
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/flags/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func PrepareRego(regoSlice []string) (rego.Config, error) {
case "aio":
c.AIO = true
default:
return rego.Config{}, errfmt.Errorf("invalid rego option specified, use '--help rego' for more info")
return rego.Config{}, errfmt.Errorf("invalid rego option specified, use 'help rego' for more info")
}
}

Expand Down