Skip to content

Commit

Permalink
Changes to instance create
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjeffries committed May 20, 2020
1 parent 7e16e37 commit 8290109
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions cmd/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cmd

import (
"fmt"
"github.com/briandowns/spinner"
"os"
"strings"
"time"

"github.com/briandowns/spinner"

"github.com/civo/civogo"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"

Expand Down Expand Up @@ -105,29 +107,29 @@ If you wish to use a custom format, the available fields are:
config.TagsList = tags
}

var executionTime string
startTime := utility.StartTime()

var instance *civogo.Instance
resp, err := client.CreateInstance(config)
if err != nil {
utility.Error("error creating instance %s", err)
os.Exit(1)
}

var executionTime string

if wait == true {
startTime := utility.StartTime()

stillCreating := true
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Prefix = fmt.Sprintf("Creating instance (%s)... ", resp.Hostname)
s.Start()

for stillCreating {
instanceCheck, err := client.FindInstance(resp.ID)
instance, err = client.FindInstance(resp.ID)
if err != nil {
utility.Error("Unable to find the network %s", err)
os.Exit(1)
}
if instanceCheck.Status == "ACTIVE" {
if instance.Status == "ACTIVE" {
stillCreating = false
s.Stop()
} else {
Expand All @@ -136,14 +138,14 @@ If you wish to use a custom format, the available fields are:
}

executionTime = utility.TrackTime(startTime)
}

// we look for the created instance to obtain the data that we need
// like PublicIP
instance, err := client.FindInstance(resp.ID)
if err != nil {
utility.Error("Unable to find the instance %s", err)
os.Exit(1)
} else {
// we look for the created instance to obtain the data that we need
// like PublicIP
instance, err = client.FindInstance(resp.ID)
if err != nil {
utility.Error("Unable to find the instance %s", err)
os.Exit(1)
}
}

if outputFormat == "human" {
Expand Down

2 comments on commit 8290109

@andyjeffries
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I've pulled instance out of the "wait" block and moved this up to an else (so we don't make the call an extra time for this when we've just made it for the "wait" loop), if they didn't wait, is there any point in doing this - they won't have a PublicIP yet anyway (that happens during building and at this moment if they didn't wait it won't have happened)

Not to mention further down you use resp and don't actually use this object from the API call.

@alejandrojnm

@alejandrojnm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, I didn't see it that way but it's much better this way

Please sign in to comment.