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

Aliases for DB software #413

Merged
merged 5 commits into from
May 17, 2024
Merged
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
45 changes: 26 additions & 19 deletions cmd/database/database_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"fmt"
"os"
"strings"

"github.com/civo/civogo"
"github.com/civo/cli/common"
Expand Down Expand Up @@ -87,45 +88,51 @@ var dbCreateCmd = &cobra.Command{

dbVersions, err := client.ListDBVersions()
if err != nil {
utility.Error("Unable to list database versions %s", err)
utility.Error("Failed to fetch database versions: %s", err)
os.Exit(1)
}

if software == "" {
software = "MySQL"
}
software = strings.ToLower(software)
softwareIsValid := false
softwareVersionIsValid := false

if softwareVersion == "" && software == "MySQL" {
softwareVersion = "8.0"
validSoftwares := map[string][]string{
"mysql": {"mysql"},
"postgresql": {"postgresql", "psql"},
}

if software == "PostgreSQL" && softwareVersion == "" {
softwareVersion = "14"
apiSoftwareNames := map[string]string{
"mysql": "MySQL",
"postgresql": "PostgreSQL",
}

softwareIsValid := false
softwareVersionIsValid := false
if software != "" {
for swName, version := range dbVersions {
if swName == software {
canonicalSoftwareName := ""
for swName, aliases := range validSoftwares {
for _, alias := range aliases {
if alias == software {
softwareIsValid = true
for i, v := range version {
if v.SoftwareVersion == version[i].SoftwareVersion {
canonicalSoftwareName = apiSoftwareNames[swName]
for _, v := range dbVersions[canonicalSoftwareName] {
if v.SoftwareVersion == softwareVersion {
softwareVersionIsValid = true
break
}
}
}

}
}

if !softwareIsValid {
utility.Error("The provided software name is not valid. Make sure you use correct capitalization (e.g. MySQL, PostgreSQL)")
utility.Error("The provided software name is not valid. valid options are mysql, psql or postgresql")
os.Exit(1)
}

if !softwareVersionIsValid {
utility.Error("The provided software version is not valid")
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))
} else {
utility.Error("The provided software version is not valid. Please check the available versions for the specified software.")
}
os.Exit(1)
}

Expand All @@ -136,7 +143,7 @@ var dbCreateCmd = &cobra.Command{
Nodes: nodes,
FirewallID: firewallID,
FirewallRules: rulesFirewall,
Software: software,
Software: canonicalSoftwareName,
SoftwareVersion: softwareVersion,
Region: client.Region,
}
Expand Down
Loading