4883: Advice when user tries to set profile name
parent
333c293976
commit
c93908dd0a
|
@ -47,6 +47,12 @@ var ProfileCmd = &cobra.Command{
|
|||
profile := args[0]
|
||||
if profile == "default" {
|
||||
profile = "minikube"
|
||||
} else {
|
||||
// not validating when it is default profile
|
||||
errProfile, ok := ValidateProfile(profile)
|
||||
if !ok && errProfile != nil {
|
||||
out.FailureT(errProfile.Msg)
|
||||
}
|
||||
}
|
||||
err := Set(pkgConfig.MachineProfile, profile)
|
||||
if err != nil {
|
||||
|
|
|
@ -228,9 +228,19 @@ func EnableOrDisableStorageClasses(name, val string) error {
|
|||
return EnableOrDisableAddon(name, val)
|
||||
}
|
||||
|
||||
//ValidateProfile checks if the profile user is trying to switch exists, else throws error
|
||||
func ValidateProfile(profile string) error {
|
||||
var profileFound bool
|
||||
// ErrValidateProfile Error to validate profile
|
||||
type ErrValidateProfile struct {
|
||||
Name string
|
||||
Msg string
|
||||
}
|
||||
|
||||
func (e ErrValidateProfile) Error() string {
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
// ValidateProfile checks if the profile user is trying to switch exists, else throws error
|
||||
func ValidateProfile(profile string) (*ErrValidateProfile, bool) {
|
||||
|
||||
validProfiles, invalidProfiles, err := pkgConfig.ListProfiles()
|
||||
if err != nil {
|
||||
exit.WithError("Not able to retrieve profile list", err)
|
||||
|
@ -239,11 +249,12 @@ func ValidateProfile(profile string) error {
|
|||
// handling invalid profiles
|
||||
for _, invalidProf := range invalidProfiles {
|
||||
if profile == invalidProf.Name {
|
||||
return errors.New("You are trying to switch to invalid profile")
|
||||
return &ErrValidateProfile{Name: profile, Msg: fmt.Sprintf("%q is an invalid profile", profile)}, false
|
||||
}
|
||||
}
|
||||
|
||||
// valid profiles if found, setting profileFound to true
|
||||
profileFound := false
|
||||
// valid profiles if found, setting profileFound to trueexpectedMsg
|
||||
for _, prof := range validProfiles {
|
||||
if prof.Name == profile {
|
||||
profileFound = true
|
||||
|
@ -251,7 +262,7 @@ func ValidateProfile(profile string) error {
|
|||
}
|
||||
}
|
||||
if !profileFound {
|
||||
return errors.New("Profile you are trying to switch is not found")
|
||||
return &ErrValidateProfile{Name: profile, Msg: fmt.Sprintf("profile %q not found", profile)}, false
|
||||
}
|
||||
return nil
|
||||
return nil, true
|
||||
}
|
||||
|
|
|
@ -119,18 +119,19 @@ func TestValidateProfile(t *testing.T) {
|
|||
expectErr string
|
||||
}{
|
||||
{
|
||||
profileName: "not_so_common",
|
||||
expectErr: "Profile you are trying to switch is not found",
|
||||
profileName: "82374328742_2974224498",
|
||||
},
|
||||
{
|
||||
profileName: "minikube",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
profileNam := test.profileName
|
||||
expectedMsg := test.expectErr
|
||||
expectedMsg := fmt.Sprintf("profile %q not found", test.profileName)
|
||||
|
||||
err := ValidateProfile(profileNam)
|
||||
fmt.Println(err.Error())
|
||||
if err.Error() != expectedMsg {
|
||||
err, ok := ValidateProfile(profileNam)
|
||||
if !ok && err.Error() != expectedMsg {
|
||||
t.Errorf("Didnt receive expected message")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue