Skip to content

Commit ae12514

Browse files
refactor: improve help handling (#2241)
1 parent bd48dd2 commit ae12514

File tree

10 files changed

+95
-71
lines changed

10 files changed

+95
-71
lines changed

cmd/tracee-ebpf/flags/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/aquasecurity/tracee/pkg/events/queue"
99
)
1010

11-
func CacheHelp() string {
11+
func cacheHelp() string {
1212
return `Select different cache types for the event pipeline queueing.
1313
Possible options:
1414
cache-type={none,mem} pick the appropriate cache type.

cmd/tracee-ebpf/flags/capabilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const (
1616
AddReqCapsFlag = "add"
1717
)
1818

19-
func CapabilitiesHelp() string {
19+
func capabilitiesHelp() string {
2020
mainHelp := fmt.Sprintf(`Manage the capabilities and capabilities-related operations of tracee-ebpf
2121
Normally, tracee will drop all capabilities not required to its operations.
2222

cmd/tracee-ebpf/flags/capture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
tracee "github.com/aquasecurity/tracee/pkg/ebpf"
1010
)
1111

12-
func CaptureHelp() string {
12+
func captureHelp() string {
1313
return `Capture artifacts that were written, executed or found to be suspicious.
1414
Captured artifacts will appear in the 'output-path' directory.
1515
Possible options:

cmd/tracee-ebpf/flags/containers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/aquasecurity/tracee/pkg/containers/runtime"
1010
)
1111

12-
func ContainersHelp() string {
12+
func containersHelp() string {
1313
return `Select which container runtimes to connect to for container events enrichment.
1414
By default, if no flag is passed, tracee will automatically detect installed runtimes by going through known runtime socket paths.
1515

cmd/tracee-ebpf/flags/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/aquasecurity/tracee/pkg/filters"
99
)
1010

11-
func FilterHelp() string {
11+
func filterHelp() string {
1212
return `Select which events to trace by defining trace expressions that operate on events or process metadata.
1313
Only events that match all trace expressions will be traced (trace flags are ANDed).
1414
The following types of expressions are supported:

cmd/tracee-ebpf/flags/help.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package flags
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
func PrintAndExitIfHelp(ctx *cli.Context) {
11+
keys := []string{
12+
"crs",
13+
"cache",
14+
"capture",
15+
"trace",
16+
"output",
17+
"caps",
18+
}
19+
20+
for _, k := range keys {
21+
stringSlice := ctx.StringSlice(k)
22+
if checkIsHelp(stringSlice) {
23+
fmt.Print(getHelpString(k))
24+
os.Exit(0)
25+
}
26+
}
27+
}
28+
29+
func checkIsHelp(s []string) bool {
30+
if len(s) == 1 && s[0] == "help" {
31+
return true
32+
}
33+
return false
34+
}
35+
36+
func getHelpString(key string) string {
37+
switch key {
38+
case "crs":
39+
return containersHelp()
40+
case "cache":
41+
return cacheHelp()
42+
case "capture":
43+
return captureHelp()
44+
case "trace":
45+
return filterHelp()
46+
case "output":
47+
return outputHelp()
48+
case "caps":
49+
return capabilitiesHelp()
50+
}
51+
return ""
52+
}

cmd/tracee-ebpf/flags/help_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package flags
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_checkIsHelp(t *testing.T) {
10+
testCases := []struct {
11+
testName string
12+
input []string
13+
expected bool
14+
}{
15+
{"no flag", []string{""}, false},
16+
{"help flag", []string{"help"}, true},
17+
{"capture flag", []string{"capture"}, false},
18+
{"output flag", []string{"output"}, false},
19+
{"trace flag", []string{"trace"}, false},
20+
{"multiple flags", []string{"help", "capture"}, false},
21+
}
22+
23+
for _, testcase := range testCases {
24+
t.Run(testcase.testName, func(t *testing.T) {
25+
actual := checkIsHelp(testcase.input)
26+
assert.Equal(t, testcase.expected, actual)
27+
})
28+
}
29+
}

cmd/tracee-ebpf/flags/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
tracee "github.com/aquasecurity/tracee/pkg/ebpf"
1111
)
1212

13-
func OutputHelp() string {
13+
func outputHelp() string {
1414
return `Control how and where output is printed.
1515
Possible options:
1616
[format:]table output events in table format

cmd/tracee-ebpf/main.go

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func main() {
3838
return cli.ShowAppHelp(c)
3939
}
4040

41+
flags.PrintAndExitIfHelp(c)
42+
4143
if c.Bool("list") {
4244
printList()
4345
return nil
@@ -88,23 +90,13 @@ func main() {
8890
ContainersEnrich: enrich,
8991
}
9092

91-
containerRuntimesSlice := c.StringSlice("crs")
92-
if checkCommandIsHelp(containerRuntimesSlice) {
93-
fmt.Print(flags.ContainersHelp())
94-
return nil
95-
}
96-
sockets, err := flags.PrepareContainers(containerRuntimesSlice)
93+
sockets, err := flags.PrepareContainers(c.StringSlice("crs"))
9794
if err != nil {
9895
return err
9996
}
10097
cfg.Sockets = sockets
10198

102-
cacheSlice := c.StringSlice("cache")
103-
if checkCommandIsHelp(cacheSlice) {
104-
fmt.Print(flags.CacheHelp())
105-
return nil
106-
}
107-
cache, err := flags.PrepareCache(cacheSlice)
99+
cache, err := flags.PrepareCache(c.StringSlice("cache"))
108100
if err != nil {
109101
return err
110102
}
@@ -113,23 +105,13 @@ func main() {
113105
logger.Debug("cache", "type", cfg.Cache.String())
114106
}
115107

116-
captureSlice := c.StringSlice("capture")
117-
if checkCommandIsHelp(captureSlice) {
118-
fmt.Print(flags.CaptureHelp())
119-
return nil
120-
}
121-
capture, err := flags.PrepareCapture(captureSlice)
108+
capture, err := flags.PrepareCapture(c.StringSlice("capture"))
122109
if err != nil {
123110
return err
124111
}
125112
cfg.Capture = &capture
126113

127-
traceSlice := c.StringSlice("trace")
128-
if checkCommandIsHelp(traceSlice) {
129-
fmt.Print(flags.FilterHelp())
130-
return nil
131-
}
132-
filter, err := flags.PrepareFilter(traceSlice)
114+
filter, err := flags.PrepareFilter(c.StringSlice("trace"))
133115
if err != nil {
134116
return err
135117
}
@@ -139,25 +121,15 @@ func main() {
139121
(cfg.Filter.NewContFilter.Enabled() && cfg.Filter.NewContFilter.Value()) ||
140122
cfg.Filter.ContIDFilter.Enabled()
141123

142-
outputSlice := c.StringSlice("output")
143-
if checkCommandIsHelp(outputSlice) {
144-
fmt.Print(flags.OutputHelp())
145-
return nil
146-
}
147-
output, printerConfig, err := flags.PrepareOutput(outputSlice)
124+
output, printerConfig, err := flags.PrepareOutput(c.StringSlice("output"))
148125
if err != nil {
149126
return err
150127
}
151128

152129
printerConfig.ContainerMode = containerMode
153130
cfg.Output = &output
154131

155-
capsCfgSlice := c.StringSlice("caps")
156-
if checkCommandIsHelp(capsCfgSlice) {
157-
fmt.Print(flags.CapabilitiesHelp())
158-
return nil
159-
}
160-
capsCfg, err := flags.PrepareCapsConfig(capsCfgSlice)
132+
capsCfg, err := flags.PrepareCapsConfig(c.StringSlice("caps"))
161133
if err != nil {
162134
return err
163135
}
@@ -379,13 +351,6 @@ func main() {
379351
}
380352
}
381353

382-
func checkCommandIsHelp(s []string) bool {
383-
if len(s) == 1 && s[0] == "help" {
384-
return true
385-
}
386-
return false
387-
}
388-
389354
func getFormattedEventParams(eventID events.ID) string {
390355
evtDef, exists := events.Definitions.GetSafe(eventID)
391356
if !exists {

cmd/tracee-ebpf/main_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
func Test_checkCommandIsHelp(t *testing.T) {
12-
testCases := []struct {
13-
testName string
14-
input []string
15-
expected bool
16-
}{
17-
{"no flag", []string{""}, false},
18-
{"help flag", []string{"help"}, true},
19-
{"capture flag", []string{"capture"}, false},
20-
{"output flag", []string{"output"}, false},
21-
{"trace flag", []string{"trace"}, false},
22-
{"multiple flags", []string{"help", "capture"}, false},
23-
}
24-
25-
for _, testcase := range testCases {
26-
t.Run(testcase.testName, func(t *testing.T) {
27-
actual := checkCommandIsHelp(testcase.input)
28-
assert.Equal(t, testcase.expected, actual)
29-
})
30-
}
31-
}
32-
3311
func Test_getFormattedEventParams(t *testing.T) {
3412
testCases := []struct {
3513
input events.ID

0 commit comments

Comments
 (0)