@@ -16,14 +16,13 @@ package cmd
16
16
17
17
import (
18
18
"context"
19
- "os"
20
19
"os/signal"
21
20
"syscall"
22
21
23
- logger "github.com/sirupsen/logrus"
24
22
"github.com/spf13/cobra"
25
23
26
24
"github.com/falcosecurity/falcoctl/pkg/options"
25
+ "github.com/falcosecurity/falcoctl/pkg/output"
27
26
"github.com/falcosecurity/falcoctl/pkg/version"
28
27
)
29
28
@@ -32,14 +31,6 @@ const (
32
31
defaultRepoFile = "sources.yaml"
33
32
)
34
33
35
- func init () {
36
- logger .SetFormatter (& logger.TextFormatter {
37
- ForceColors : true ,
38
- DisableLevelTruncation : false ,
39
- DisableTimestamp : true ,
40
- })
41
- }
42
-
43
34
// New instantiates the root command.
44
35
func New () * cobra.Command {
45
36
opt := options .NewOptions ()
@@ -53,9 +44,6 @@ func New() *cobra.Command {
53
44
// by calling the initialize function.
54
45
opt .Initialize ()
55
46
},
56
- Run : func (c * cobra.Command , args []string ) {
57
- c .Help ()
58
- },
59
47
}
60
48
61
49
// Global flags
@@ -74,42 +62,15 @@ func New() *cobra.Command {
74
62
75
63
// Execute creates the root command and runs it.
76
64
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 )
88
66
89
- ctx , cancel := context . WithCancel ( ctx )
67
+ // If the ctx is marked as done then we reset the signals.
90
68
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 ()
104
71
}()
105
- return ctx
106
- }
107
72
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 ))
115
76
}
0 commit comments