diff --git a/cmd/database/database.go b/cmd/database/database.go index 89939309..c386ab73 100644 --- a/cmd/database/database.go +++ b/cmd/database/database.go @@ -36,6 +36,7 @@ func init() { DBCmd.AddCommand(dbEngineCmd) DBCmd.AddCommand(dbBackupCmd) DBCmd.AddCommand(dbRestoreCmd) + DBCmd.AddCommand(dbVersionListCmd) dbCredentialCmd.Flags().BoolVarP(&connectionString, "connection-string", "c", false, "show the connection string for the database") diff --git a/cmd/database/database_version.go b/cmd/database/database_version.go new file mode 100644 index 00000000..4a4e352e --- /dev/null +++ b/cmd/database/database_version.go @@ -0,0 +1,52 @@ +package database + +import ( + "github.com/civo/cli/common" + "github.com/civo/cli/config" + "github.com/civo/cli/utility" + "github.com/spf13/cobra" + "os" +) + +var dbVersionListCmd = &cobra.Command{ + Use: "versions", + Aliases: []string{"version"}, + Example: `civo db versions`, + Short: "List all the available database versions", + Run: func(cmd *cobra.Command, args []string) { + utility.EnsureCurrentRegion() + + client, err := config.CivoAPIClient() + if err != nil { + utility.Error("Creating the connection to Civo's API failed with %s", err) + os.Exit(1) + } + + if common.RegionSet != "" { + client.Region = common.RegionSet + } + + dbVersions, err := client.ListDBVersions() + if err != nil { + utility.Error("%s", err) + os.Exit(1) + } + + ow := utility.NewOutputWriter() + + for dbName, versionDetails := range dbVersions { + ow.StartLine() + ow.AppendDataWithLabel("name", dbName, "Name") + ow.AppendDataWithLabel("version", versionDetails[0].SoftwareVersion, "version") + } + + switch common.OutputFormat { + case "json": + ow.WriteMultipleObjectsJSON(common.PrettySet) + case "custom": + ow.WriteCustomOutput(common.OutputFields) + default: + ow.WriteTable() + } + }, +}