Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor db prompt in infra_confirm.go #4487

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions cli/azd/internal/repository/infra_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,11 @@ func (i *Initializer) infraSpecFromDetect(

dbPrompt:
for {
dbName, err := i.console.Prompt(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Input the name of the app database (%s)", database.Display()),
Help: "Hint: App database name\n\n" +
"Name of the database that the app connects to. " +
"This database will be created after running azd provision or azd up." +
"\nYou may be able to skip this step by hitting enter, in which case the database will not be created.",
})
dbName, err := i.promptDbName(ctx, database)
if err != nil {
return scaffold.InfraSpec{}, err
}

if strings.ContainsAny(dbName, " ") {
i.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "Database name contains whitespace. This might not be allowed by the database server.",
})
confirm, err := i.console.Confirm(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Continue with name '%s'?", dbName),
})
if err != nil {
return scaffold.InfraSpec{}, err
}

if !confirm {
continue dbPrompt
}
} else if !wellFormedDbNameRegex.MatchString(dbName) {
i.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "Database name contains special characters. " +
"This might not be allowed by the database server.",
})
confirm, err := i.console.Confirm(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Continue with name '%s'?", dbName),
})
if err != nil {
return scaffold.InfraSpec{}, err
}

if !confirm {
continue dbPrompt
}
}

switch database {
case appdetect.DbMongo:
spec.DbCosmosMongo = &scaffold.DatabaseCosmosMongo{
Expand Down Expand Up @@ -229,3 +192,51 @@ func (i *Initializer) getPortByPrompt(ctx context.Context, promptMessage string)
}
return port, nil
}

func (i *Initializer) promptDbName(ctx context.Context, database appdetect.DatabaseDep) (string, error) {
for {
dbName, err := i.console.Prompt(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Input the name of the app database (%s)", database.Display()),
Help: "Hint: App database name\n\n" +
"Name of the database that the app connects to. " +
"This database will be created after running azd provision or azd up." +
"\nYou may be able to skip this step by hitting enter, in which case the database will not be created.",
})
if err != nil {
return "", err
}

if strings.ContainsAny(dbName, " ") {
i.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "Database name contains whitespace. This might not be allowed by the database server.",
})
confirm, err := i.console.Confirm(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Continue with name '%s'?", dbName),
})
if err != nil {
return "", err
}

if !confirm {
continue
}
} else if !wellFormedDbNameRegex.MatchString(dbName) {
i.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "Database name contains special characters. " +
"This might not be allowed by the database server.",
})
confirm, err := i.console.Confirm(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Continue with name '%s'?", dbName),
})
if err != nil {
return "", err
}

if !confirm {
continue
}
weikanglim marked this conversation as resolved.
Show resolved Hide resolved
}

return dbName, nil
}
}
4 changes: 4 additions & 0 deletions cli/azd/internal/repository/infra_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func TestInitializer_infraSpecFromDetect(t *testing.T) {
},
},
interactions: []string{
"my app db",
"n",
"my$special$db",
"n",
"myappdb", // fill in db name
},
want: scaffold.InfraSpec{
Expand Down
Loading