Run make fmt

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
Hubertbits 2025-04-12 00:19:15 +02:00 committed by yxxhero
parent a829524fc3
commit e8a4380e66
24 changed files with 440 additions and 445 deletions

View File

@ -73,7 +73,7 @@ func NewApplyCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.StringArrayVar(&applyOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
// Register flags using the registrar
flagRegistrar.RegisterFlags(cmd)
flagRegistrar.RegisterFlags(cmd)
return cmd
}

View File

@ -62,7 +62,7 @@ func NewDiffCmd(globalCfg *config.GlobalImpl) *cobra.Command {
f.StringArrayVar(&diffOptions.SuppressOutputLineRegex, "suppress-output-line-regex", nil, "a list of regex patterns to suppress output lines from the diff output")
// Register flags using the registrar
flagRegistrar.RegisterFlags(cmd)
flagRegistrar.RegisterFlags(cmd)
return cmd
}

View File

@ -1377,7 +1377,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) {
SkipDiffOnInstall: c.SkipDiffOnInstall(),
ReuseValues: c.ReuseValues(),
ResetValues: c.ResetValues(),
IncludeCRDs: c.ShouldIncludeCRDs(),
IncludeCRDs: c.ShouldIncludeCRDs(),
DiffArgs: c.DiffArgs(),
PostRenderer: c.PostRenderer(),
PostRendererArgs: c.PostRendererArgs(),

View File

@ -244,9 +244,9 @@ type TemplateConfigProvider interface {
}
type CRDConfig interface {
SkipCRDs() bool
IncludeCRDs() bool
ShouldIncludeCRDs() bool
SkipCRDs() bool
IncludeCRDs() bool
ShouldIncludeCRDs() bool
}
type DAGConfig interface {

View File

@ -25,7 +25,7 @@ type diffConfig struct {
set []string
validate bool
skipCRDs common.BoolFlag
includeCRDs common.BoolFlag
includeCRDs common.BoolFlag
skipDeps bool
skipRefresh bool
includeTests bool
@ -104,10 +104,10 @@ func (a diffConfig) IncludeCRDs() bool {
}
func (a diffConfig) ShouldIncludeCRDs() bool {
includeCRDsExplicit := a.includeCRDs.WasExplicitlySet() && a.includeCRDs.Value()
skipCRDsExplicit := a.skipCRDs.WasExplicitlySet() && !a.skipCRDs.Value()
includeCRDsExplicit := a.includeCRDs.WasExplicitlySet() && a.includeCRDs.Value()
skipCRDsExplicit := a.skipCRDs.WasExplicitlySet() && !a.skipCRDs.Value()
return includeCRDsExplicit || skipCRDsExplicit
return includeCRDsExplicit || skipCRDsExplicit
}
func (a diffConfig) SkipDeps() bool {

View File

@ -2,42 +2,42 @@ package common
// BoolFlag represents a boolean flag that tracks whether it was explicitly set
type BoolFlag interface {
// Value returns the current boolean value
Value() bool
// Value returns the current boolean value
Value() bool
// WasExplicitlySet returns whether the flag was explicitly set
WasExplicitlySet() bool
// WasExplicitlySet returns whether the flag was explicitly set
WasExplicitlySet() bool
// Set sets the value and marks the flag as explicitly set
Set(value bool)
// Set sets the value and marks the flag as explicitly set
Set(value bool)
}
// boolFlag is the implementation of BoolFlag
type boolFlag struct {
value bool
wasExplicitlySet bool
value bool
wasExplicitlySet bool
}
// NewBoolFlag creates a new BoolFlag with default values
func NewBoolFlag(defaultValue bool) BoolFlag {
return &boolFlag{
value: defaultValue,
wasExplicitlySet: false,
}
return &boolFlag{
value: defaultValue,
wasExplicitlySet: false,
}
}
// Value returns the current boolean value
func (bf *boolFlag) Value() bool {
return bf.value
return bf.value
}
// WasExplicitlySet returns whether the flag was explicitly set
func (bf *boolFlag) WasExplicitlySet() bool {
return bf.wasExplicitlySet
return bf.wasExplicitlySet
}
// Set sets the value and marks the flag as explicitly set
func (bf *boolFlag) Set(value bool) {
bf.value = value
bf.wasExplicitlySet = true
bf.value = value
bf.wasExplicitlySet = true
}

View File

@ -1,94 +1,94 @@
package common
import (
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func TestNewBoolFlag(t *testing.T) {
tests := []struct {
name string
defaultValue bool
expected bool
}{
{
name: "default true",
defaultValue: true,
expected: true,
},
{
name: "default false",
defaultValue: false,
expected: false,
},
}
tests := []struct {
name string
defaultValue bool
expected bool
}{
{
name: "default true",
defaultValue: true,
expected: true,
},
{
name: "default false",
defaultValue: false,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewBoolFlag(tt.defaultValue)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewBoolFlag(tt.defaultValue)
// Check initial state
assert.Equal(t, tt.expected, flag.Value(), "Value should match default")
assert.False(t, flag.WasExplicitlySet(), "New flag should not be marked as explicitly set")
})
}
// Check initial state
assert.Equal(t, tt.expected, flag.Value(), "Value should match default")
assert.False(t, flag.WasExplicitlySet(), "New flag should not be marked as explicitly set")
})
}
}
func TestBoolFlag_Set(t *testing.T) {
tests := []struct {
name string
defaultValue bool
setValue bool
expected bool
}{
{
name: "default false, set true",
defaultValue: false,
setValue: true,
expected: true,
},
{
name: "default true, set false",
defaultValue: true,
setValue: false,
expected: false,
},
}
tests := []struct {
name string
defaultValue bool
setValue bool
expected bool
}{
{
name: "default false, set true",
defaultValue: false,
setValue: true,
expected: true,
},
{
name: "default true, set false",
defaultValue: true,
setValue: false,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewBoolFlag(tt.defaultValue)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewBoolFlag(tt.defaultValue)
// Set the value
flag.Set(tt.setValue)
// Set the value
flag.Set(tt.setValue)
// Check state after setting
assert.Equal(t, tt.expected, flag.Value(), "Value should match set value")
assert.True(t, flag.WasExplicitlySet(), "Flag should be marked as explicitly set")
})
}
// Check state after setting
assert.Equal(t, tt.expected, flag.Value(), "Value should match set value")
assert.True(t, flag.WasExplicitlySet(), "Flag should be marked as explicitly set")
})
}
}
func TestBoolFlag_MultipleSet(t *testing.T) {
flag := NewBoolFlag(false)
flag := NewBoolFlag(false)
// Initial state
assert.False(t, flag.Value())
assert.False(t, flag.WasExplicitlySet())
// Initial state
assert.False(t, flag.Value())
assert.False(t, flag.WasExplicitlySet())
// First set
flag.Set(true)
assert.True(t, flag.Value())
assert.True(t, flag.WasExplicitlySet())
// First set
flag.Set(true)
assert.True(t, flag.Value())
assert.True(t, flag.WasExplicitlySet())
// Second set
flag.Set(false)
assert.False(t, flag.Value())
assert.True(t, flag.WasExplicitlySet(), "Flag should remain explicitly set")
// Second set
flag.Set(false)
assert.False(t, flag.Value())
assert.True(t, flag.WasExplicitlySet(), "Flag should remain explicitly set")
}
func TestBoolFlag_Implementation(t *testing.T) {
// Test that boolFlag properly implements BoolFlag interface
var _ BoolFlag = &boolFlag{}
// Test that boolFlag properly implements BoolFlag interface
var _ BoolFlag = &boolFlag{}
}

View File

@ -1,58 +1,56 @@
package common
// For array/slice flags
type StringArrayFlag interface {
Values() []string
WasExplicitlySet() bool
Add(value string)
Set(values []string)
Values() []string
WasExplicitlySet() bool
Add(value string)
Set(values []string)
}
type stringArrayFlag struct {
values []string
wasExplicitlySet bool
values []string
wasExplicitlySet bool
}
// NewStringArrayFlag creates a new StringArrayFlag with the given default values
// Create a defensive copy of the default values to prevent external modifications
// and to ensure that the original values remain unchanged.
func NewStringArrayFlag(defaultValues []string) StringArrayFlag {
valuesCopy := make([]string, len(defaultValues))
copy(valuesCopy, defaultValues)
valuesCopy := make([]string, len(defaultValues))
copy(valuesCopy, defaultValues)
return &stringArrayFlag{
values: valuesCopy,
wasExplicitlySet: false,
}
return &stringArrayFlag{
values: valuesCopy,
wasExplicitlySet: false,
}
}
// Values returns the values of the flag
// It returns a copy of the values to prevent external modifications
// and to ensure that the original values remain unchanged.
// This is important for flags that may be modified by the user
// or other parts of the program.
func (f *stringArrayFlag) Values() []string {
// Return a copy to prevent external modifications
valuesCopy := make([]string, len(f.values))
copy(valuesCopy, f.values)
return valuesCopy
// Return a copy to prevent external modifications
valuesCopy := make([]string, len(f.values))
copy(valuesCopy, f.values)
return valuesCopy
}
// WasExplicitlySet returns whether the flag was explicitly set
func (f *stringArrayFlag) WasExplicitlySet() bool {
return f.wasExplicitlySet
return f.wasExplicitlySet
}
// Set sets the values and marks the flag as explicitly set
func (f *stringArrayFlag) Set(values []string) {
f.values = values
f.wasExplicitlySet = true
f.values = values
f.wasExplicitlySet = true
}
// Add sets the value and marks the flag as explicitly set
func (f *stringArrayFlag) Add(value string) {
f.values = append(f.values, value)
f.wasExplicitlySet = true
f.wasExplicitlySet = true
}

View File

@ -1,35 +1,35 @@
package common
type StringFlag interface {
Value() string
WasExplicitlySet() bool
Set(value string)
Value() string
WasExplicitlySet() bool
Set(value string)
}
type stringFlag struct {
value string
wasExplicitlySet bool
value string
wasExplicitlySet bool
}
func NewStringFlag(defaultValue string) StringFlag {
return &stringFlag{
value: defaultValue,
wasExplicitlySet: false,
}
return &stringFlag{
value: defaultValue,
wasExplicitlySet: false,
}
}
// Value returns the current boolean value
func (f *stringFlag) Value() string {
return f.value
return f.value
}
// WasExplicitlySet returns whether the flag was explicitly set
func (f *stringFlag) WasExplicitlySet() bool {
return f.wasExplicitlySet
return f.wasExplicitlySet
}
// Set sets the value and marks the flag as explicitly set
func (f *stringFlag) Set(value string) {
f.value = value
f.wasExplicitlySet = true
}
f.value = value
f.wasExplicitlySet = true
}

View File

@ -1,100 +1,100 @@
package common
import (
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
func TestNewStringFlag(t *testing.T) {
tests := []struct {
name string
defaultValue string
expected string
}{
{
name: "empty default",
defaultValue: "",
expected: "",
},
{
name: "non-empty default",
defaultValue: "default",
expected: "default",
},
}
tests := []struct {
name string
defaultValue string
expected string
}{
{
name: "empty default",
defaultValue: "",
expected: "",
},
{
name: "non-empty default",
defaultValue: "default",
expected: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewStringFlag(tt.defaultValue)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewStringFlag(tt.defaultValue)
// Check initial state
assert.Equal(t, tt.expected, flag.Value(), "Value should match default")
assert.False(t, flag.WasExplicitlySet(), "New flag should not be marked as explicitly set")
})
}
// Check initial state
assert.Equal(t, tt.expected, flag.Value(), "Value should match default")
assert.False(t, flag.WasExplicitlySet(), "New flag should not be marked as explicitly set")
})
}
}
func TestStringFlag_Set(t *testing.T) {
tests := []struct {
name string
defaultValue string
setValue string
expected string
}{
{
name: "empty default, set value",
defaultValue: "",
setValue: "new value",
expected: "new value",
},
{
name: "non-empty default, set empty",
defaultValue: "default",
setValue: "",
expected: "",
},
{
name: "non-empty default, set new value",
defaultValue: "default",
setValue: "new value",
expected: "new value",
},
}
tests := []struct {
name string
defaultValue string
setValue string
expected string
}{
{
name: "empty default, set value",
defaultValue: "",
setValue: "new value",
expected: "new value",
},
{
name: "non-empty default, set empty",
defaultValue: "default",
setValue: "",
expected: "",
},
{
name: "non-empty default, set new value",
defaultValue: "default",
setValue: "new value",
expected: "new value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewStringFlag(tt.defaultValue)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := NewStringFlag(tt.defaultValue)
// Set the value
flag.Set(tt.setValue)
// Set the value
flag.Set(tt.setValue)
// Check state after setting
assert.Equal(t, tt.expected, flag.Value(), "Value should match set value")
assert.True(t, flag.WasExplicitlySet(), "Flag should be marked as explicitly set")
})
}
// Check state after setting
assert.Equal(t, tt.expected, flag.Value(), "Value should match set value")
assert.True(t, flag.WasExplicitlySet(), "Flag should be marked as explicitly set")
})
}
}
func TestStringFlag_MultipleSet(t *testing.T) {
flag := NewStringFlag("initial")
flag := NewStringFlag("initial")
// Initial state
assert.Equal(t, "initial", flag.Value())
assert.False(t, flag.WasExplicitlySet())
// Initial state
assert.Equal(t, "initial", flag.Value())
assert.False(t, flag.WasExplicitlySet())
// First set
flag.Set("first")
assert.Equal(t, "first", flag.Value())
assert.True(t, flag.WasExplicitlySet())
// First set
flag.Set("first")
assert.Equal(t, "first", flag.Value())
assert.True(t, flag.WasExplicitlySet())
// Second set
flag.Set("second")
assert.Equal(t, "second", flag.Value())
assert.True(t, flag.WasExplicitlySet(), "Flag should remain explicitly set")
// Second set
flag.Set("second")
assert.Equal(t, "second", flag.Value())
assert.True(t, flag.WasExplicitlySet(), "Flag should remain explicitly set")
}
func TestStringFlag_Implementation(t *testing.T) {
// Test that stringFlag properly implements StringFlag interface
var _ StringFlag = &stringFlag{}
// Test that stringFlag properly implements StringFlag interface
var _ StringFlag = &stringFlag{}
}

View File

@ -160,20 +160,20 @@ func (a *ApplyImpl) NoHooks() bool {
// SkipCRDs returns the skip CRDs.
func (a *ApplyImpl) SkipCRDs() bool {
return a.ApplyOptions.SkipCRDsFlag.Value()
return a.ApplyOptions.SkipCRDsFlag.Value()
}
// IncludeCRDs returns the include CRDs.
func (a *ApplyImpl) IncludeCRDs() bool {
return a.ApplyOptions.IncludeCRDsFlag.Value()
return a.ApplyOptions.IncludeCRDsFlag.Value()
}
// ShouldIncludeCRDs returns true if CRDs should be included.
func (a *ApplyImpl) ShouldIncludeCRDs() bool {
includeCRDsExplicit := a.IncludeCRDsFlag.WasExplicitlySet() && a.IncludeCRDsFlag.Value()
skipCRDsExplicit := a.SkipCRDsFlag.WasExplicitlySet() && !a.SkipCRDsFlag.Value()
includeCRDsExplicit := a.IncludeCRDsFlag.WasExplicitlySet() && a.IncludeCRDsFlag.Value()
skipCRDsExplicit := a.SkipCRDsFlag.WasExplicitlySet() && !a.SkipCRDsFlag.Value()
return includeCRDsExplicit || skipCRDsExplicit
return includeCRDsExplicit || skipCRDsExplicit
}
// SkipCleanup returns the skip cleanup.

View File

@ -157,20 +157,20 @@ func (t *DiffImpl) NoHooks() bool {
// SkipCRDs returns the skip crds
func (t *DiffImpl) SkipCRDs() bool {
return t.DiffOptions.SkipCRDsFlag.Value()
return t.DiffOptions.SkipCRDsFlag.Value()
}
// IncludeCRDs returns the include crds
func (t *DiffImpl) IncludeCRDs() bool {
return t.DiffOptions.IncludeCRDsFlag.Value()
return t.DiffOptions.IncludeCRDsFlag.Value()
}
// ShouldIncludeCRDs returns true if CRDs should be included
func (t *DiffImpl) ShouldIncludeCRDs() bool {
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
return includeCRDsExplicit || skipCRDsExplicit
return includeCRDsExplicit || skipCRDsExplicit
}
// SkipDiffOnInstall returns the skip diff on install

View File

@ -1,65 +1,65 @@
package config
func (o *ApplyOptions) HandleFlag(name string, value interface{}, changed bool) {
if !changed {
return
}
if !changed {
return
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
case "skip-crds":
if boolVal, ok := value.(*bool); ok {
o.SkipCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
case "skip-crds":
if boolVal, ok := value.(*bool); ok {
o.SkipCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
}
func (o *DiffOptions) HandleFlag(name string, value interface{}, changed bool) {
if !changed {
return
}
if !changed {
return
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
}
func (o *SyncOptions) HandleFlag(name string, value interface{}, changed bool) {
if !changed {
return
}
if !changed {
return
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
case "skip-crds":
if boolVal, ok := value.(*bool); ok {
o.SkipCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
case "skip-crds":
if boolVal, ok := value.(*bool); ok {
o.SkipCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
}
func (o *TemplateOptions) HandleFlag(name string, value interface{}, changed bool) {
if !changed {
return
}
if !changed {
return
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
}
switch name {
case "include-crds":
if boolVal, ok := value.(*bool); ok {
o.IncludeCRDsFlag.Set(*boolVal)
}
// Handle other flags...
}
}

View File

@ -115,20 +115,20 @@ func (t *SyncImpl) Values() []string {
// SkipCRDs returns the skip crds
func (t *SyncImpl) SkipCRDs() bool {
return t.SyncOptions.SkipCRDsFlag.Value()
return t.SyncOptions.SkipCRDsFlag.Value()
}
// IncludeCRDs returns the include crds
func (t *SyncImpl) IncludeCRDs() bool {
return t.SyncOptions.IncludeCRDsFlag.Value()
return t.SyncOptions.IncludeCRDsFlag.Value()
}
// ShouldIncludeCRDs returns true if CRDs should be included
func (t *SyncImpl) ShouldIncludeCRDs() bool {
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
return includeCRDsExplicit || skipCRDsExplicit
return includeCRDsExplicit || skipCRDsExplicit
}
// Wait returns the wait

View File

@ -24,7 +24,7 @@ type TemplateOptions struct {
Validate bool
// SkipCRDsFlag is the skip crds flag
// Deprecated: Use IncludeCRDsFlag instead
SkipCRDsFlag common.BoolFlag
SkipCRDsFlag common.BoolFlag
// IncludeCRDsFlag is the include crds flag
IncludeCRDsFlag common.BoolFlag
// SkipTests is the skip tests flag
@ -91,10 +91,10 @@ func (t *TemplateImpl) IncludeCRDs() bool {
// ShouldIncludeCRDs returns whether to include crds
func (t *TemplateImpl) ShouldIncludeCRDs() bool {
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
includeCRDsExplicit := t.IncludeCRDsFlag.WasExplicitlySet() && t.IncludeCRDsFlag.Value()
skipCRDsExplicit := t.SkipCRDsFlag.WasExplicitlySet() && !t.SkipCRDsFlag.Value()
return includeCRDsExplicit || skipCRDsExplicit
return includeCRDsExplicit || skipCRDsExplicit
}
// NoHooks returns the no hooks

View File

@ -4,21 +4,20 @@ import "github.com/spf13/cobra"
// ApplyFlagRegistrar handles flags specific to the apply command
type ApplyFlagRegistrar struct {
*GenericFlagRegistrar
IncludeCRDs bool
SkipCRDs bool
*GenericFlagRegistrar
IncludeCRDs bool
SkipCRDs bool
}
// NewApplyFlagRegistrar creates a new ApplyFlagRegistrar
func NewApplyFlagRegistrar() *ApplyFlagRegistrar {
return &ApplyFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
return &ApplyFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
}
// RegisterFlags registers apply-specific flags
func (r *ApplyFlagRegistrar) RegisterFlags(cmd *cobra.Command) {
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
r.RegisterBoolFlag(cmd, "skip-crds", &r.SkipCRDs, false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present")
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
r.RegisterBoolFlag(cmd, "skip-crds", &r.SkipCRDs, false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present")
}

View File

@ -1,49 +1,49 @@
package flags
import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
)
// FlagRegistrar defines an interface for registering and transferring flags
type FlagRegistrar interface {
RegisterFlags(cmd *cobra.Command)
TransferFlags(cmd *cobra.Command, opts interface{})
RegisterFlags(cmd *cobra.Command)
TransferFlags(cmd *cobra.Command, opts interface{})
}
// FlagHandler is a generic interface for handling flag values
type FlagHandler interface {
// HandleFlag receives a flag name, value, and whether it was changed
HandleFlag(name string, value interface{}, changed bool)
// HandleFlag receives a flag name, value, and whether it was changed
HandleFlag(name string, value interface{}, changed bool)
}
// GenericFlagRegistrar is a base struct for flag registrars
type GenericFlagRegistrar struct {
// Map of flag names to their values
values map[string]interface{}
// Map of flag names to their values
values map[string]interface{}
}
// NewGenericFlagRegistrar creates a new GenericFlagRegistrar
func NewGenericFlagRegistrar() *GenericFlagRegistrar {
return &GenericFlagRegistrar{
values: make(map[string]interface{}),
}
return &GenericFlagRegistrar{
values: make(map[string]interface{}),
}
}
// TransferFlags transfers all registered flags to the options
func (r *GenericFlagRegistrar) TransferFlags(cmd *cobra.Command, opts interface{}) {
if handler, ok := opts.(FlagHandler); ok {
flags := cmd.Flags()
if handler, ok := opts.(FlagHandler); ok {
flags := cmd.Flags()
// Transfer each registered flag
for name, value := range r.values {
changed := flags.Changed(name)
handler.HandleFlag(name, value, changed)
}
}
// Transfer each registered flag
for name, value := range r.values {
changed := flags.Changed(name)
handler.HandleFlag(name, value, changed)
}
}
}
// RegisterBoolFlag registers a boolean flag and stores its reference
func (r *GenericFlagRegistrar) RegisterBoolFlag(cmd *cobra.Command, name string, value *bool, defaultValue bool, usage string) {
cmd.Flags().BoolVar(value, name, defaultValue, usage)
r.values[name] = value
cmd.Flags().BoolVar(value, name, defaultValue, usage)
r.values[name] = value
}

View File

@ -1,140 +1,140 @@
package flags
import (
"testing"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
// MockFlagHandler implements FlagHandler for testing
type MockFlagHandler struct {
handledFlags map[string]interface{}
changedFlags map[string]bool
handledFlags map[string]interface{}
changedFlags map[string]bool
}
func NewMockFlagHandler() *MockFlagHandler {
return &MockFlagHandler{
handledFlags: make(map[string]interface{}),
changedFlags: make(map[string]bool),
}
return &MockFlagHandler{
handledFlags: make(map[string]interface{}),
changedFlags: make(map[string]bool),
}
}
func (h *MockFlagHandler) HandleFlag(name string, value interface{}, changed bool) {
h.handledFlags[name] = value
h.changedFlags[name] = changed
h.handledFlags[name] = value
h.changedFlags[name] = changed
}
func TestNewGenericFlagRegistrar(t *testing.T) {
registrar := NewGenericFlagRegistrar()
assert.NotNil(t, registrar)
assert.NotNil(t, registrar.values)
assert.Len(t, registrar.values, 0)
registrar := NewGenericFlagRegistrar()
assert.NotNil(t, registrar)
assert.NotNil(t, registrar.values)
assert.Len(t, registrar.values, 0)
}
func TestGenericFlagRegistrar_RegisterBoolFlag(t *testing.T) {
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
// Verify the flag was registered
flag := cmd.Flags().Lookup("test-flag")
assert.NotNil(t, flag)
assert.Equal(t, "test-flag", flag.Name)
assert.Equal(t, "false", flag.DefValue)
assert.Equal(t, "Test flag", flag.Usage)
// Verify the flag was registered
flag := cmd.Flags().Lookup("test-flag")
assert.NotNil(t, flag)
assert.Equal(t, "test-flag", flag.Name)
assert.Equal(t, "false", flag.DefValue)
assert.Equal(t, "Test flag", flag.Usage)
// Verify the value was stored in the registrar
value, exists := registrar.values["test-flag"]
assert.True(t, exists)
assert.Equal(t, &testFlag, value)
// Verify the value was stored in the registrar
value, exists := registrar.values["test-flag"]
assert.True(t, exists)
assert.Equal(t, &testFlag, value)
}
func TestGenericFlagRegistrar_TransferFlags_NoChanges(t *testing.T) {
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
// Create a mock handler
handler := NewMockFlagHandler()
// Create a mock handler
handler := NewMockFlagHandler()
// Transfer flags (none changed)
registrar.TransferFlags(cmd, handler)
// Transfer flags (none changed)
registrar.TransferFlags(cmd, handler)
// Verify the handler was called with the right parameters
assert.Equal(t, &testFlag, handler.handledFlags["test-flag"])
assert.False(t, handler.changedFlags["test-flag"])
// Verify the handler was called with the right parameters
assert.Equal(t, &testFlag, handler.handledFlags["test-flag"])
assert.False(t, handler.changedFlags["test-flag"])
}
func TestGenericFlagRegistrar_TransferFlags_WithChanges(t *testing.T) {
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
// Simulate flag being set on command line
err := cmd.Flags().Set("test-flag", "true")
assert.NoError(t, err)
testFlag = true // Value would be updated by cobra
// Simulate flag being set on command line
err := cmd.Flags().Set("test-flag", "true")
assert.NoError(t, err)
testFlag = true // Value would be updated by cobra
// Create a mock handler
handler := NewMockFlagHandler()
// Create a mock handler
handler := NewMockFlagHandler()
// Transfer flags (with changes)
registrar.TransferFlags(cmd, handler)
// Transfer flags (with changes)
registrar.TransferFlags(cmd, handler)
// Verify the handler was called with the right parameters
assert.Equal(t, &testFlag, handler.handledFlags["test-flag"])
assert.True(t, handler.changedFlags["test-flag"])
assert.True(t, *handler.handledFlags["test-flag"].(*bool))
// Verify the handler was called with the right parameters
assert.Equal(t, &testFlag, handler.handledFlags["test-flag"])
assert.True(t, handler.changedFlags["test-flag"])
assert.True(t, *handler.handledFlags["test-flag"].(*bool))
}
func TestGenericFlagRegistrar_TransferFlags_NonHandler(t *testing.T) {
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
var testFlag bool
registrar.RegisterBoolFlag(cmd, "test-flag", &testFlag, false, "Test flag")
// Use a non-handler type
nonHandler := struct{}{}
// Use a non-handler type
nonHandler := struct{}{}
// This should not panic
registrar.TransferFlags(cmd, nonHandler)
// This should not panic
registrar.TransferFlags(cmd, nonHandler)
}
func TestGenericFlagRegistrar_MultipleFlags(t *testing.T) {
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
registrar := NewGenericFlagRegistrar()
cmd := &cobra.Command{Use: "test"}
var boolFlag bool
var boolFlag2 bool
var boolFlag bool
var boolFlag2 bool
registrar.RegisterBoolFlag(cmd, "bool-flag", &boolFlag, false, "Boolean flag")
registrar.RegisterBoolFlag(cmd, "bool-flag2", &boolFlag2, true, "Another boolean flag")
registrar.RegisterBoolFlag(cmd, "bool-flag", &boolFlag, false, "Boolean flag")
registrar.RegisterBoolFlag(cmd, "bool-flag2", &boolFlag2, true, "Another boolean flag")
// Set one flag
err := cmd.Flags().Set("bool-flag", "true")
assert.NoError(t, err)
boolFlag = true // Value would be updated by cobra
// Set one flag
err := cmd.Flags().Set("bool-flag", "true")
assert.NoError(t, err)
boolFlag = true // Value would be updated by cobra
// Create a mock handler
handler := NewMockFlagHandler()
// Create a mock handler
handler := NewMockFlagHandler()
// Transfer flags
registrar.TransferFlags(cmd, handler)
// Transfer flags
registrar.TransferFlags(cmd, handler)
// Verify both flags were handled correctly
assert.Equal(t, &boolFlag, handler.handledFlags["bool-flag"])
assert.True(t, handler.changedFlags["bool-flag"])
assert.True(t, *handler.handledFlags["bool-flag"].(*bool))
// Verify both flags were handled correctly
assert.Equal(t, &boolFlag, handler.handledFlags["bool-flag"])
assert.True(t, handler.changedFlags["bool-flag"])
assert.True(t, *handler.handledFlags["bool-flag"].(*bool))
assert.Equal(t, &boolFlag2, handler.handledFlags["bool-flag2"])
assert.False(t, handler.changedFlags["bool-flag2"])
assert.True(t, *handler.handledFlags["bool-flag2"].(*bool)) // Default is true
assert.Equal(t, &boolFlag2, handler.handledFlags["bool-flag2"])
assert.False(t, handler.changedFlags["bool-flag2"])
assert.True(t, *handler.handledFlags["bool-flag2"].(*bool)) // Default is true
}

View File

@ -4,19 +4,19 @@ import "github.com/spf13/cobra"
// DiffFlagRegistrar handles flags specific to the diff command
type DiffFlagRegistrar struct {
*GenericFlagRegistrar
IncludeCRDs bool
*GenericFlagRegistrar
IncludeCRDs bool
}
// NewDiffFlagRegistrar creates a new DiffFlagRegistrar
func NewDiffFlagRegistrar() *DiffFlagRegistrar {
return &DiffFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
return &DiffFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
}
// RegisterFlags registers diff-specific flags
func (r *DiffFlagRegistrar) RegisterFlags(cmd *cobra.Command) {
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
// Diff doesn't have skip-crds
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
// Diff doesn't have skip-crds
}

View File

@ -1,32 +1,32 @@
package flags
import (
"github.com/helmfile/helmfile/pkg/common"
"github.com/helmfile/helmfile/pkg/common"
)
// BoolFlagInitializer ensures a BoolFlag is initialized with a default value if nil
func EnsureBoolFlag(flag *common.BoolFlag, defaultValue bool) {
if *flag == nil {
*flag = common.NewBoolFlag(defaultValue)
}
if *flag == nil {
*flag = common.NewBoolFlag(defaultValue)
}
}
// StringFlagInitializer ensures a StringFlag is initialized with a default value if nil
func EnsureStringFlag(flag *common.StringFlag, defaultValue string) {
if *flag == nil {
*flag = common.NewStringFlag(defaultValue)
}
if *flag == nil {
*flag = common.NewStringFlag(defaultValue)
}
}
// StringArrayFlagInitializer ensures a StringArrayFlag is initialized with default values if nil
func EnsureStringArrayFlag(flag *common.StringArrayFlag, defaultValues []string) {
if *flag == nil {
*flag = common.NewStringArrayFlag(defaultValues)
}
if *flag == nil {
*flag = common.NewStringArrayFlag(defaultValues)
}
}
// InitializeOptions initializes all nil flag fields in an options struct
func InitializeOptions(options interface{}) {
// This could be expanded to use reflection to automatically find and initialize
// all flag fields in any options struct
}
// This could be expanded to use reflection to automatically find and initialize
// all flag fields in any options struct
}

View File

@ -4,21 +4,20 @@ import "github.com/spf13/cobra"
// SyncFlagRegistrar handles flags specific to the sync command
type SyncFlagRegistrar struct {
*GenericFlagRegistrar
IncludeCRDs bool
SkipCRDs bool
*GenericFlagRegistrar
IncludeCRDs bool
SkipCRDs bool
}
// NewSyncFlagRegistrar creates a new SyncFlagRegistrar
func NewSyncFlagRegistrar() *SyncFlagRegistrar {
return &SyncFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
return &SyncFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
}
// RegisterFlags registers sync-specific flags
func (r *SyncFlagRegistrar) RegisterFlags(cmd *cobra.Command) {
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
r.RegisterBoolFlag(cmd, "skip-crds", &r.SkipCRDs, false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present")
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
r.RegisterBoolFlag(cmd, "skip-crds", &r.SkipCRDs, false, "if set, no CRDs will be installed on sync. By default, CRDs are installed if not already present")
}

View File

@ -4,19 +4,18 @@ import "github.com/spf13/cobra"
// TemplateFlagRegistrar handles flags specific to the template command
type TemplateFlagRegistrar struct {
*GenericFlagRegistrar
IncludeCRDs bool
*GenericFlagRegistrar
IncludeCRDs bool
}
// NewTemplateFlagRegistrar creates a new TemplateFlagRegistrar
func NewTemplateFlagRegistrar() *TemplateFlagRegistrar {
return &TemplateFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
return &TemplateFlagRegistrar{
GenericFlagRegistrar: NewGenericFlagRegistrar(),
}
}
// RegisterFlags registers template-specific flags
func (r *TemplateFlagRegistrar) RegisterFlags(cmd *cobra.Command) {
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
r.RegisterBoolFlag(cmd, "include-crds", &r.IncludeCRDs, false, "include CRDs in the diffing")
}

View File

@ -2036,7 +2036,7 @@ type DiffOpts struct {
// If this is true, Color has no effect.
NoColor bool
Set []string
IncludeCRDs bool
IncludeCRDs bool
SkipCleanup bool
SkipDiffOnInstall bool
DiffArgs string

View File

@ -389,26 +389,26 @@ func execHelm(t *testing.T, args ...string) string {
func waitForRegistry(t *testing.T, address string, timeout time.Duration) error {
t.Helper()
client := http.Client{
Timeout: 1 * time.Second,
}
client := http.Client{
Timeout: 1 * time.Second,
}
url := fmt.Sprintf("http://%s/v2/", address)
deadline := time.Now().Add(timeout)
url := fmt.Sprintf("http://%s/v2/", address)
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
resp, err := client.Get(url)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized {
// Registry is up - a 200 OK or 401 Unauthorized both indicate the registry is running
return nil
}
}
for time.Now().Before(deadline) {
resp, err := client.Get(url)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized {
// Registry is up - a 200 OK or 401 Unauthorized both indicate the registry is running
return nil
}
}
// Wait a bit before trying again
time.Sleep(500 * time.Millisecond)
}
// Wait a bit before trying again
time.Sleep(500 * time.Millisecond)
}
return fmt.Errorf("timed out waiting for registry at %s after %s", address, timeout)
}
return fmt.Errorf("timed out waiting for registry at %s after %s", address, timeout)
}