Skip to content

Commit

Permalink
cleanup(cmd,pkg): moved enum to its own package.
Browse files Browse the repository at this point in the history
Moreover, moved logLevel and logFormat enums under pkg/output.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed May 7, 2024
1 parent cce5b80 commit caae24a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 32 deletions.
17 changes: 17 additions & 0 deletions pkg/enum/doc.go
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions pkg/options/enum.go → pkg/enum/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package options
package enum

import (
"fmt"
Expand All @@ -26,34 +26,34 @@ 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.
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
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/options/enum_test.go → pkg/enum/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand All @@ -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))
})
})

Expand Down
8 changes: 4 additions & 4 deletions pkg/options/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/options/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
}
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/options/logFormat.go → pkg/output/logFormat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -32,22 +34,23 @@ 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),
}
}

// ToPtermFormatter converts the current formatter to pterm.LogFormatter.
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)"))
})
})

Expand Down
10 changes: 6 additions & 4 deletions pkg/options/logLevel.go → pkg/output/logLevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions pkg/options/logLevel_test.go → pkg/output/logLevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)"))
})
})

Expand Down

0 comments on commit caae24a

Please sign in to comment.