Skip to content

Commit 68cd2c5

Browse files
alacukupoiana
authored andcommitted
refactor(cmd/root): drop custom signal handling for the root cmd
Signed-off-by: Aldo Lacuku <[email protected]>
1 parent c3023d2 commit 68cd2c5

File tree

4 files changed

+8
-50
lines changed

4 files changed

+8
-50
lines changed

cmd/root.go

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ package cmd
1616

1717
import (
1818
"context"
19-
"os"
2019
"os/signal"
2120
"syscall"
2221

23-
logger "github.com/sirupsen/logrus"
2422
"github.com/spf13/cobra"
2523

2624
"github.com/falcosecurity/falcoctl/pkg/options"
25+
"github.com/falcosecurity/falcoctl/pkg/output"
2726
"github.com/falcosecurity/falcoctl/pkg/version"
2827
)
2928

@@ -32,14 +31,6 @@ const (
3231
defaultRepoFile = "sources.yaml"
3332
)
3433

35-
func init() {
36-
logger.SetFormatter(&logger.TextFormatter{
37-
ForceColors: true,
38-
DisableLevelTruncation: false,
39-
DisableTimestamp: true,
40-
})
41-
}
42-
4334
// New instantiates the root command.
4435
func New() *cobra.Command {
4536
opt := options.NewOptions()
@@ -53,9 +44,6 @@ func New() *cobra.Command {
5344
// by calling the initialize function.
5445
opt.Initialize()
5546
},
56-
Run: func(c *cobra.Command, args []string) {
57-
c.Help()
58-
},
5947
}
6048

6149
// Global flags
@@ -74,42 +62,15 @@ func New() *cobra.Command {
7462

7563
// Execute creates the root command and runs it.
7664
func Execute() {
77-
ctx := WithSignals(context.Background())
78-
if err := New().ExecuteContext(ctx); err != nil {
79-
logger.WithError(err).Fatal("error executing falcoctl")
80-
}
81-
}
82-
83-
// WithSignals returns a copy of ctx with a new Done channel.
84-
// The returned context's Done channel is closed when a SIGKILL or SIGTERM signal is received.
85-
func WithSignals(ctx context.Context) context.Context {
86-
sigCh := make(chan os.Signal, 1)
87-
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
65+
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
8866

89-
ctx, cancel := context.WithCancel(ctx)
67+
// If the ctx is marked as done then we reset the signals.
9068
go func() {
91-
defer cancel()
92-
select {
93-
case <-ctx.Done():
94-
return
95-
case s := <-sigCh:
96-
switch s {
97-
case os.Interrupt:
98-
logger.Infof("received SIGINT")
99-
case syscall.SIGTERM:
100-
logger.Infof("received SIGTERM")
101-
}
102-
return
103-
}
69+
<-ctx.Done()
70+
stop()
10471
}()
105-
return ctx
106-
}
10772

108-
// initLogger configures the logger
109-
func initLogger(logLevel string) {
110-
lvl, err := logger.ParseLevel(logLevel)
111-
if err != nil {
112-
logger.Fatal(err)
113-
}
114-
logger.SetLevel(lvl)
73+
// we do not log the error here since we expect that each subcommand
74+
// handles the errors by itself.
75+
output.ExitOnErr(New().ExecuteContext(ctx))
11576
}

cmd/testdata/help.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
The control tool for running Falco in Kubernetes
22

33
Usage:
4-
falcoctl [flags]
54
falcoctl [command]
65

76
Available Commands:

cmd/testdata/noargsnoflags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
The control tool for running Falco in Kubernetes
22

33
Usage:
4-
falcoctl [flags]
54
falcoctl [command]
65

76
Available Commands:

cmd/testdata/wrongflag.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Error: unknown flag: --wrong
22
Usage:
3-
falcoctl [flags]
43
falcoctl [command]
54

65
Available Commands:

0 commit comments

Comments
 (0)