Skip to content

Add some unit tests and refactor to support tests #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
695 changes: 106 additions & 589 deletions cmd/config/config.go

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions cmd/config/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package config

// Copyright (C) 2021-2025 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause

import (
"perfspect/internal/target"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// flagDefinition is a struct that defines a command line flag.
type flagDefinition struct {
pflag *pflag.Flag
intSetFunc func(int, target.Target, string) error
floatSetFunc func(float64, target.Target, string) error
stringSetFunc func(string, target.Target, string) error
validationFunc func(cmd *cobra.Command) bool
validationDescription string
}

// GetName returns the name of the flag.
func (f *flagDefinition) GetName() string {
return f.pflag.Name
}

// GetType returns the type of the flag.
func (f *flagDefinition) GetType() string {
return f.pflag.Value.Type()
}

// GetValueAsString returns the value of the flag as a string.
func (f *flagDefinition) GetValueAsString() string {
return f.pflag.Value.String()
}

// newIntFlag creates a new integer flag and adds it to the command.
func newIntFlag(cmd *cobra.Command, name string, defaultValue int, setFunc func(int, target.Target, string) error, help string, validationDescription string, validationFunc func(cmd *cobra.Command) bool) flagDefinition {
cmd.Flags().Int(name, defaultValue, help)
pFlag := cmd.Flags().Lookup(name)
return flagDefinition{
pflag: pFlag,
intSetFunc: setFunc,
validationFunc: validationFunc,
validationDescription: validationDescription,
}
}

// newInt64Flag creates a new int64 flag and adds it to the command.
func newFloat64Flag(cmd *cobra.Command, name string, defaultValue float64, setFunc func(float64, target.Target, string) error, help string, validationDescription string, validationFunc func(cmd *cobra.Command) bool) flagDefinition {
cmd.Flags().Float64(name, defaultValue, help)
pFlag := cmd.Flags().Lookup(name)
return flagDefinition{
pflag: pFlag,
floatSetFunc: setFunc,
validationFunc: validationFunc,
validationDescription: validationDescription,
}
}

// newStringFlag creates a new string flag and adds it to the command.
func newStringFlag(cmd *cobra.Command, name string, defaultValue string, setFunc func(string, target.Target, string) error, help string, validationDescription string, validationFunc func(cmd *cobra.Command) bool) flagDefinition {
cmd.Flags().String(name, defaultValue, help)
pFlag := cmd.Flags().Lookup(name)
return flagDefinition{
pflag: pFlag,
stringSetFunc: setFunc,
validationFunc: validationFunc,
validationDescription: validationDescription,
}
}
Loading