-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change vars requirement validation
- Loading branch information
1 parent
edff506
commit 41117f4
Showing
3 changed files
with
169 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright (C) 2021-2023, Kubefirst | ||
This program is licensed under MIT. | ||
See the LICENSE file for more details. | ||
*/ | ||
package k3d | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type FlagConfig struct { | ||
Name string | ||
FlagType string // "string" o "bool" | ||
} | ||
|
||
func getFlag(cmd *cobra.Command, config FlagConfig) (interface{}, error) { | ||
if config.FlagType == "string" { | ||
return cmd.Flags().GetString(config.Name) | ||
} else if config.FlagType == "bool" { | ||
return cmd.Flags().GetBool(config.Name) | ||
} | ||
return nil, fmt.Errorf("unknown flag type: %s", config.FlagType) | ||
} | ||
|
||
func validateFlags(cmd *cobra.Command, flagValues map[string]interface{}, FlagsToValidate []FlagConfig) error { | ||
for _, config := range FlagsToValidate { | ||
value, err := getFlag(cmd, config) | ||
if err != nil { | ||
return err | ||
} | ||
flagValues[config.Name] = value | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package k3d | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func TestGetFlag(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
cmd.Flags().String("testStringFlag", "default", "test string flag") | ||
cmd.Flags().Bool("testBoolFlag", false, "test bool flag") | ||
|
||
tests := []struct { | ||
name string | ||
flagConfig FlagConfig | ||
expected interface{} | ||
expectError bool | ||
errorText string | ||
}{ | ||
{"string flag", FlagConfig{"testStringFlag", "string"}, "default", false, ""}, | ||
{"bool flag", FlagConfig{"testBoolFlag", "bool"}, false, false, ""}, | ||
{"unknown flag type", FlagConfig{"testUnknownFlag", "unknown"}, nil, true, "unknown flag type: unknown"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := getFlag(cmd, tt.flagConfig) | ||
|
||
if tt.expectError { | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} else if err.Error() != tt.errorText { | ||
t.Errorf("Expected error message '%v', got '%v'", tt.errorText, err.Error()) | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if result != tt.expected { | ||
t.Errorf("Expected %v, got %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
flagConfigs []FlagConfig | ||
expected map[string]interface{} | ||
expectError bool | ||
}{ | ||
{ | ||
"valid string and bool flags", | ||
[]FlagConfig{ | ||
{"testStringFlag", "string"}, | ||
{"testBoolFlag", "bool"}, | ||
}, | ||
map[string]interface{}{ | ||
"testStringFlag": "default", | ||
"testBoolFlag": false, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"unknown flag type", | ||
[]FlagConfig{ | ||
{"testUnknownFlag", "unknown"}, | ||
}, | ||
nil, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cmd := &cobra.Command{} | ||
cmd.Flags().String("testStringFlag", "default", "test string flag") | ||
cmd.Flags().Bool("testBoolFlag", false, "test bool flag") | ||
|
||
flagValues := make(map[string]interface{}) | ||
|
||
err := validateFlags(cmd, flagValues, tt.flagConfigs) | ||
|
||
if tt.expectError { | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if !reflect.DeepEqual(flagValues, tt.expected) { | ||
t.Errorf("Expected flag values to be %v, got %v", tt.expected, flagValues) | ||
} | ||
}) | ||
} | ||
} |