Limit tries of username entries and display message (most likely when Docker container run without -it flag, see #3)

This commit is contained in:
Marc Ole Bulling
2021-05-03 23:13:30 +02:00
parent 7a9ffee8c7
commit ca58161e19
2 changed files with 8 additions and 4 deletions

View File

@@ -145,7 +145,7 @@ func generateDefaultConfig() {
serverSettings = Configuration{
SaltAdmin: saltAdmin,
}
username := askForUsername()
username := askForUsername(1)
password := askForPassword()
port := askForPort()
url := askForUrl(port)
@@ -199,7 +199,11 @@ func Save() {
}
// Asks for username or loads it from env and returns input as string if valid
func askForUsername() string {
func askForUsername(try int) string {
if try > 5 {
fmt.Println("Too many invalid entries! If you are running the setup with Docker, make sure to start the container with the -it flag.")
os.Exit(1)
}
fmt.Print("Username: ")
envUsername := Environment.AdminName
if envUsername != "" {
@@ -211,7 +215,7 @@ func askForUsername() string {
return username
}
fmt.Println("Username needs to be at least 4 characters long")
return askForUsername()
return askForUsername(try + 1)
}
// Asks for password or loads it from env and returns input as string if valid

View File

@@ -95,7 +95,7 @@ func TestUpgradeDb(t *testing.T) {
func TestAskForUsername(t *testing.T) {
original := testconfiguration.StartMockInputStdin("admin")
output := askForUsername()
output := askForUsername(1)
testconfiguration.StopMockInputStdin(original)
test.IsEqualString(t, output, "admin")
}