Skip to content

Commit f8eb694

Browse files
alacukupoiana
authored andcommitted
new(pkg/options): add the options packages
The options packags handles all the commons flags and configuration. Having the common options managed in one place we avoid duplicated logics in each command that uses the same flags. Signed-off-by: Aldo Lacuku <[email protected]>
1 parent e037a42 commit f8eb694

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

pkg/options/doc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Falco Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package options implements the generic logic to manage the common options
16+
// shared by all the falcoctl commands.
17+
package options

pkg/options/options.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2022 The Falco Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package options
16+
17+
import (
18+
"io"
19+
20+
"github.com/spf13/pflag"
21+
22+
"github.com/falcosecurity/falcoctl/pkg/output"
23+
)
24+
25+
// Used to store the verbose flag, and then passed to the printer.
26+
var verbose bool
27+
28+
// ConfigOptions provides the common flags, options, and printers for all the
29+
// commands. All the fields provided by the ConfigOptions will be initialized before
30+
// the commands are executed through the Initialize func.
31+
type ConfigOptions struct {
32+
// Printer used by all commands to output messages.
33+
Printer *output.Printer
34+
// printerScope contains the data of the optional scope of a prefix.
35+
// It used to add a prefix to the output of a printer.
36+
printerScope string
37+
// writer is used to write the output of the printer.
38+
writer io.Writer
39+
}
40+
41+
// NewOptions returns a new ConfigOptions struct.
42+
func NewOptions() *ConfigOptions {
43+
return &ConfigOptions{}
44+
}
45+
46+
// Configs type of the configs accepted by the Initialize function.
47+
type Configs func(options *ConfigOptions)
48+
49+
// WithPrinterScope sets the scope for the printer.
50+
func WithPrinterScope(scope string) Configs {
51+
return func(options *ConfigOptions) {
52+
options.printerScope = scope
53+
}
54+
}
55+
56+
// WithWriter sets the writer for the printer.
57+
func WithWriter(writer io.Writer) Configs {
58+
return func(options *ConfigOptions) {
59+
options.writer = writer
60+
}
61+
}
62+
63+
// Initialize initializes the options based on the configs. Subsequent calls will overwrite the
64+
// previous configurations based on the new configs passed to the functions.
65+
func (o *ConfigOptions) Initialize(cfgs ...Configs) {
66+
for _, cfg := range cfgs {
67+
cfg(o)
68+
}
69+
70+
// create the printer. The value of verbose is a flag value.
71+
o.Printer = output.NewPrinter(o.printerScope, verbose, o.writer)
72+
}
73+
74+
// AddFlags registers the common flags.
75+
func (o *ConfigOptions) AddFlags(flags *pflag.FlagSet) {
76+
flags.BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logs (default false)")
77+
}

0 commit comments

Comments
 (0)