Skip to content

Commit

Permalink
refactor: change vars requirement validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jairoFernandez committed Oct 22, 2023
1 parent edff506 commit 41117f4
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 49 deletions.
76 changes: 27 additions & 49 deletions cmd/k3d/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,61 +55,39 @@ var (
cancelContext context.CancelFunc
)

func runK3d(cmd *cobra.Command, args []string) error {
ciFlag, err := cmd.Flags().GetBool("ci")
if err != nil {
return err
}

clusterNameFlag, err := cmd.Flags().GetString("cluster-name")
if err != nil {
return err
}

clusterTypeFlag, err := cmd.Flags().GetString("cluster-type")
if err != nil {
return err
}

githubOrgFlag, err := cmd.Flags().GetString("github-org")
if err != nil {
return err
}

githubUserFlag, err := cmd.Flags().GetString("github-user")
if err != nil {
return err
}

gitlabGroupFlag, err := cmd.Flags().GetString("gitlab-group")
if err != nil {
return err
}

gitProviderFlag, err := cmd.Flags().GetString("git-provider")
if err != nil {
return err
}

gitProtocolFlag, err := cmd.Flags().GetString("git-protocol")
if err != nil {
return err
}
var FlagsToValidate = []FlagConfig{
{"ci", "bool"},
{"cluster-name", "string"},
{"cluster-type", "string"},
{"github-org", "string"},
{"github-user", "string"},
{"gitlab-group", "string"},
{"git-provider", "string"},
{"git-protocol", "string"},
{"gitops-template-url", "string"},
{"gitops-template-branch", "string"},
{"use-telemetry", "bool"},
}

gitopsTemplateURLFlag, err := cmd.Flags().GetString("gitops-template-url")
if err != nil {
return err
}
func runK3d(cmd *cobra.Command, args []string) error {

gitopsTemplateBranchFlag, err := cmd.Flags().GetString("gitops-template-branch")
flagValues := make(map[string]interface{})
err := validateFlags(cmd, flagValues, FlagsToValidate)
if err != nil {
return err
}

useTelemetryFlag, err := cmd.Flags().GetBool("use-telemetry")
if err != nil {
return err
}
ciFlag := flagValues["ci"].(bool)
clusterNameFlag := flagValues["cluster-name"].(string)
clusterTypeFlag := flagValues["cluster-type"].(string)
githubUserFlag := flagValues["github-user"].(string)
gitlabGroupFlag := flagValues["gitlab-group"].(string)
githubOrgFlag := flagValues["github-org"].(string)
gitProviderFlag := flagValues["git-provider"].(string)
gitProtocolFlag := flagValues["git-protocol"].(string)
gitopsTemplateURLFlag := flagValues["gitops-template-url"].(string)
gitopsTemplateBranchFlag := flagValues["gitops-template-branch"].(string)
useTelemetryFlag := flagValues["use-telemetry"].(bool)

// If cluster setup is complete, return
clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete")
Expand Down
38 changes: 38 additions & 0 deletions cmd/k3d/utils.go
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
}
104 changes: 104 additions & 0 deletions cmd/k3d/utils_test.go
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)
}
})
}
}

0 comments on commit 41117f4

Please sign in to comment.