diff --git a/cmd/database/database.go b/cmd/database/database.go index c386ab7..75e0c4d 100644 --- a/cmd/database/database.go +++ b/cmd/database/database.go @@ -47,6 +47,7 @@ func init() { dbCreateCmd.Flags().StringVarP(&size, "size", "s", "g3.db.small", "the size of the database. You can list available DB sizes by `civo size list -s database`") dbCreateCmd.Flags().StringVarP(&software, "software", "m", "MySQL", "the software to use for the database. One of: MySQL, PostgreSQL. Please make sure you use the correct capitalisation.") dbCreateCmd.Flags().StringVarP(&softwareVersion, "version", "v", "", "the version of the software to use for the database.") + dbCreateCmd.Flags().BoolVarP(&waitDatabase, "wait", "w", false, "a simple flag (e.g. --wait) that will cause the CLI to spin and wait for the database to be ACTIVE") dbUpdateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database") dbUpdateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database") diff --git a/cmd/database/database_create.go b/cmd/database/database_create.go index a008c6b..4e64acb 100644 --- a/cmd/database/database_create.go +++ b/cmd/database/database_create.go @@ -4,7 +4,9 @@ import ( "fmt" "os" "strings" + "time" + "github.com/briandowns/spinner" "github.com/civo/civogo" "github.com/civo/cli/common" "github.com/civo/cli/config" @@ -13,6 +15,8 @@ import ( ) var rulesFirewall string +var waitDatabase bool + var dbCreateCmd = &cobra.Command{ Use: "create", Aliases: []string{"new", "add"}, @@ -168,6 +172,34 @@ var dbCreateCmd = &cobra.Command{ os.Exit(1) } + var executionTime string + + if waitDatabase { + startTime := utility.StartTime() + + stillCreating := true + s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) + s.Writer = os.Stderr + s.Prefix = fmt.Sprintf("Create a database called %s ", db.Name) + s.Start() + + for stillCreating { + databaseCheck, err := client.FindDatabase(db.ID) + if err != nil { + utility.Error("Database %s", err) + os.Exit(1) + } + if databaseCheck.Status == "Ready" { + stillCreating = false + s.Stop() + } else { + time.Sleep(2 * time.Second) + } + } + + executionTime = utility.TrackTime(startTime) + } + ow := utility.NewOutputWriterWithMap(map[string]string{"id": db.ID, "name": db.Name}) switch common.OutputFormat { case "json": @@ -175,7 +207,11 @@ var dbCreateCmd = &cobra.Command{ case "custom": ow.WriteCustomOutput(common.OutputFields) default: - 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) + if executionTime != "" { + fmt.Printf("Database %s (%s) with ID %s using %s with version %s has been created in %s\n", utility.Green(db.Name), db.ID, db.Software, db.SoftwareVersion, executionTime) + } else { + 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) + } } }, }