Skip to content

Commit

Permalink
Add Default on DB software and version on db create
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Feb 5, 2025
1 parent 1c0e673 commit 22d44a0
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions cmd/database/database_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ var dbCreateCmd = &cobra.Command{
os.Exit(1)
}

// Set default software to MySQL if not specified
if software == "" {
software = "mysql"
}
software = strings.ToLower(software)
softwareIsValid := false
softwareVersionIsValid := false

validSoftwares := map[string][]string{
"mysql": {"mysql"},
Expand All @@ -107,17 +109,13 @@ var dbCreateCmd = &cobra.Command{
}

canonicalSoftwareName := ""
softwareIsValid := false
for swName, aliases := range validSoftwares {
for _, alias := range aliases {
if alias == software {
softwareIsValid = true
canonicalSoftwareName = apiSoftwareNames[swName]
for _, v := range dbVersions[canonicalSoftwareName] {
if v.SoftwareVersion == softwareVersion {
softwareVersionIsValid = true
break
}
}
break
}
}
}
Expand All @@ -127,13 +125,29 @@ var dbCreateCmd = &cobra.Command{
os.Exit(1)
}

if !softwareVersionIsValid {
if softwareVersion == "" {
utility.Error(fmt.Sprintf("No version specified for %s. Please provide a version using --version flag. For example, civo database create db-psql --software psql --version 14", canonicalSoftwareName))
// If version is not specified, get the latest version
if softwareVersion == "" {
versions := dbVersions[canonicalSoftwareName]
if len(versions) > 0 {
// Assuming versions are sorted with latest first
softwareVersion = versions[0].SoftwareVersion
} else {
utility.Error("No versions available for %s", canonicalSoftwareName)
os.Exit(1)
}
} else {
// Verify the specified version is valid
versionValid := false
for _, v := range dbVersions[canonicalSoftwareName] {
if v.SoftwareVersion == softwareVersion {
versionValid = true
break
}
}
if !versionValid {
utility.Error("The provided software version is not valid. Please check the available versions for the specified software.")
os.Exit(1)
}
os.Exit(1)
}

configDB := civogo.CreateDatabaseRequest{
Expand Down Expand Up @@ -161,7 +175,7 @@ var dbCreateCmd = &cobra.Command{
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("Database (%s) with ID %s has been created\n", utility.Green(db.Name), db.ID)
fmt.Printf("Database (%s) with ID %s using %s with version %s has been created\n", utility.Green(db.Name), db.ID, db.Software, db.SoftwareVersion)
}
},
}

0 comments on commit 22d44a0

Please sign in to comment.