moved username validation to cmd

pull/10106/head
Steven Powell 2021-01-25 10:57:52 -07:00
parent 81ac8a1677
commit 5058ef3e71
3 changed files with 5 additions and 65 deletions

View File

@ -66,7 +66,7 @@ var RootCmd = &cobra.Command{
}
}
userName := viper.GetString(config.UserFlag)
if !config.UserNameValid(userName) {
if !validateUsername(userName) {
out.WarningT("User name '{{.username}}' is not valid", out.V{"username": userName})
exit.Message(reason.Usage, "User name must be 60 chars or less.")
}
@ -291,3 +291,7 @@ func addToPath(dir string) {
klog.Infof("Updating PATH: %s", dir)
os.Setenv("PATH", new)
}
func validateUsername(name string) bool {
return len(name) <= 60
}

View File

@ -1,22 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
// UserNameValid checks if the user name is valid.
func UserNameValid(name string) bool {
return len(name) <= 60
}

View File

@ -1,42 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"strings"
"testing"
)
func TestUser(t *testing.T) {
t.Run("Length", func(t *testing.T) {
tests := []struct {
in string
want bool
}{
{strings.Repeat("a", 60), true},
{strings.Repeat("a", 61), false},
}
for _, tt := range tests {
got := UserNameValid(tt.in)
if got != tt.want {
t.Errorf("UserNameValid(%q, length: %d) = %t; want %t", tt.in, len(tt.in), got, tt.want)
}
}
})
}