diff --git a/pkg/enum/doc.go b/pkg/enum/doc.go new file mode 100644 index 00000000..9110525c --- /dev/null +++ b/pkg/enum/doc.go @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (C) 2024 The Falco Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package enum implements the generic logic to manage enum values for Cobra. +package enum diff --git a/pkg/options/enum.go b/pkg/enum/enum.go similarity index 88% rename from pkg/options/enum.go rename to pkg/enum/enum.go index 0b8290b3..5779f3c8 100644 --- a/pkg/options/enum.go +++ b/pkg/enum/enum.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package enum import ( "fmt" @@ -26,21 +26,21 @@ import ( // can have a limited set of values. type Enum struct { allowed []string - value string + Value string } // NewEnum returns an enum struct. The firs argument is a set of values allowed for the flag. -// The second argument is the default value of the flag. +// The second argument is the default Value of the flag. func NewEnum(allowed []string, d string) *Enum { return &Enum{ allowed: allowed, - value: d, + Value: d, } } -// String returns the value. +// String returns the Value. func (e *Enum) String() string { - return e.value + return e.Value } // Allowed returns the list of allowed values enclosed in parenthesis. @@ -48,12 +48,12 @@ func (e *Enum) Allowed() string { return fmt.Sprintf("(%s)", strings.Join(e.allowed, ", ")) } -// Set the value for the flag. +// Set the Value for the flag. func (e *Enum) Set(p string) error { if !slices.Contains(e.allowed, p) { return fmt.Errorf("invalid argument %q, please provide one of (%s)", p, strings.Join(e.allowed, ", ")) } - e.value = p + e.Value = p return nil } diff --git a/pkg/options/enum_test.go b/pkg/enum/enum_test.go similarity index 92% rename from pkg/options/enum_test.go rename to pkg/enum/enum_test.go index f36efe3e..c05d5d24 100644 --- a/pkg/options/enum_test.go +++ b/pkg/enum/enum_test.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package enum import ( . "github.com/onsi/ginkgo/v2" @@ -41,7 +41,7 @@ var _ = Describe("Enum", func() { }) It("should set the default values", func() { - Expect(enum.value).Should(Equal(defValue)) + Expect(enum.Value).Should(Equal(defValue)) }) It("should set the allowed values", func() { @@ -64,9 +64,9 @@ var _ = Describe("Enum", func() { val = newVal }) - It("Should set the correct val", func() { + It("Should set the correct value", func() { Expect(err).ShouldNot(HaveOccurred()) - Expect(enum.value).Should(Equal(newVal)) + Expect(enum.Value).Should(Equal(newVal)) }) }) diff --git a/pkg/options/common.go b/pkg/options/common.go index df0c5c6e..b90b4a8f 100644 --- a/pkg/options/common.go +++ b/pkg/options/common.go @@ -46,15 +46,15 @@ type Common struct { // IndexCache caches the entries for the configured indexes. IndexCache *cache.Cache - logLevel *LogLevel - logFormat *LogFormat + logLevel *output.LogLevel + logFormat *output.LogFormat } // NewOptions returns a new Common struct. func NewOptions() *Common { return &Common{ - logLevel: NewLogLevel(), - logFormat: NewLogFormat(), + logLevel: output.NewLogLevel(), + logFormat: output.NewLogFormat(), } } diff --git a/pkg/options/driver.go b/pkg/options/driver.go index 66b3c853..41ce0f61 100644 --- a/pkg/options/driver.go +++ b/pkg/options/driver.go @@ -27,11 +27,12 @@ import ( "github.com/falcosecurity/falcoctl/internal/config" driverdistro "github.com/falcosecurity/falcoctl/pkg/driver/distro" drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type" + "github.com/falcosecurity/falcoctl/pkg/enum" ) // DriverTypes data structure for driver types. type DriverTypes struct { - *Enum + *enum.Enum } // NewDriverTypes returns a new Enum configured for the driver types. @@ -41,7 +42,7 @@ func NewDriverTypes() *DriverTypes { return &DriverTypes{ // Default value is not used. // This enum is only used to print allowed values. - Enum: NewEnum(types, drivertype.TypeModernBpf), + Enum: enum.NewEnum(types, drivertype.TypeModernBpf), } } diff --git a/pkg/options/logFormat.go b/pkg/output/logFormat.go similarity index 90% rename from pkg/options/logFormat.go rename to pkg/output/logFormat.go index a8e8b951..76423a69 100644 --- a/pkg/options/logFormat.go +++ b/pkg/output/logFormat.go @@ -13,10 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package output import ( "github.com/pterm/pterm" + + "github.com/falcosecurity/falcoctl/pkg/enum" ) const ( @@ -32,13 +34,13 @@ var logFormats = []string{LogFormatColor, LogFormatText, LogFormatJSON} // LogFormat data structure for log-format flag. type LogFormat struct { - *Enum + *enum.Enum } // NewLogFormat returns a new Enum configured for the log formats flag. func NewLogFormat() *LogFormat { return &LogFormat{ - Enum: NewEnum(logFormats, LogFormatColor), + Enum: enum.NewEnum(logFormats, LogFormatColor), } } @@ -46,8 +48,9 @@ func NewLogFormat() *LogFormat { func (lg *LogFormat) ToPtermFormatter() pterm.LogFormatter { var formatter pterm.LogFormatter - switch lg.value { + switch lg.Value { case LogFormatColor: + pterm.EnableColor() formatter = pterm.LogFormatterColorful case LogFormatText: pterm.DisableColor() diff --git a/pkg/options/logFormat_test.go b/pkg/output/logFormat_test.go similarity index 93% rename from pkg/options/logFormat_test.go rename to pkg/output/logFormat_test.go index fed1a6dd..9a5f2ad3 100644 --- a/pkg/options/logFormat_test.go +++ b/pkg/output/logFormat_test.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package output import ( "github.com/gookit/color" @@ -33,8 +33,8 @@ var _ = Describe("LogFormat", func() { Context("NewLogFormat Func", func() { It("should return a new logFormatter", func() { Expect(logFormatter).ShouldNot(BeNil()) - Expect(logFormatter.value).Should(Equal(LogFormatColor)) - Expect(logFormatter.allowed).Should(Equal(logFormats)) + Expect(logFormatter.Value).Should(Equal(LogFormatColor)) + Expect(logFormatter.Allowed()).Should(Equal("(color, text, json)")) }) }) diff --git a/pkg/options/logLevel.go b/pkg/output/logLevel.go similarity index 91% rename from pkg/options/logLevel.go rename to pkg/output/logLevel.go index b574696a..62ccc3a2 100644 --- a/pkg/options/logLevel.go +++ b/pkg/output/logLevel.go @@ -13,10 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package output import ( "github.com/pterm/pterm" + + "github.com/falcosecurity/falcoctl/pkg/enum" ) const ( @@ -34,20 +36,20 @@ var logLevels = []string{LogLevelInfo, LogLevelWarn, LogLevelDebug, LogLevelTrac // LogLevel data structure for log-level flag. type LogLevel struct { - *Enum + *enum.Enum } // NewLogLevel returns a new Enum configured for the log level flag. func NewLogLevel() *LogLevel { return &LogLevel{ - Enum: NewEnum(logLevels, LogLevelInfo), + Enum: enum.NewEnum(logLevels, LogLevelInfo), } } // ToPtermLogLevel converts the current log level to pterm.LogLevel. func (ll *LogLevel) ToPtermLogLevel() pterm.LogLevel { var level pterm.LogLevel - switch ll.value { + switch ll.Value { case LogLevelInfo: level = pterm.LogLevelInfo case LogLevelWarn: diff --git a/pkg/options/logLevel_test.go b/pkg/output/logLevel_test.go similarity index 93% rename from pkg/options/logLevel_test.go rename to pkg/output/logLevel_test.go index 5f7b579b..a49e4c4b 100644 --- a/pkg/options/logLevel_test.go +++ b/pkg/output/logLevel_test.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package output import ( . "github.com/onsi/ginkgo/v2" @@ -32,8 +32,8 @@ var _ = Describe("LogLevel", func() { Context("NewLogLevel Func", func() { It("should return a new LogLevel", func() { Expect(logLevel).ShouldNot(BeNil()) - Expect(logLevel.value).Should(Equal(LogLevelInfo)) - Expect(logLevel.allowed).Should(Equal(logLevels)) + Expect(logLevel.Value).Should(Equal(LogLevelInfo)) + Expect(logLevel.Allowed()).Should(Equal("(info, warn, debug, trace)")) }) })