Skip to content

Commit

Permalink
Merge pull request #12 from gocardless/nlopes/add-get-latest-image
Browse files Browse the repository at this point in the history
Better shell automation support
  • Loading branch information
Harry Maclean authored Sep 11, 2017
2 parents 6f88efc + 3550b57 commit d0e6019
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
39 changes: 32 additions & 7 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ func main() {
Name: "create",
Usage: "create a new instance",
Action: func(c *cli.Context) error {
id := c.Args().First()
if id == "" {
fmt.Println("error: must supply an image id")
return nil
var image models.Image

if c.NArg() == 0 {
image, err = client.GetLatestImage()
} else {
image, err = client.GetImage(c.Args().First())
}

image, err := client.GetImage(id)
if err != nil {
fmt.Printf("error: %s\n", err)
return err
Expand All @@ -138,7 +139,7 @@ func main() {
return err
}

fmt.Println("Created")
fmt.Printf("Created with image ID: %d\n", image.ID)
fmt.Println(InstanceToString(instance))
return nil
},
Expand Down Expand Up @@ -237,8 +238,28 @@ func main() {
return err
}

fmt.Printf("export PGHOST=%s PGPORT=%d PGUSER=postgres PGPASSWORD=''\n", CONFIG.Domain, instance.Port)
showExportCommand(CONFIG, instance)
return nil
},
},
{
Name: "new",
Aliases: []string{},
Usage: "show the environment variables to a newly created instance",
Action: func(c *cli.Context) error {
image, err := client.GetLatestImage()
if err != nil {
fmt.Printf("error: %s\n", err)
return err
}

instance, err := client.CreateInstance(image)
if err != nil {
fmt.Printf("error: %s\n", err)
return err
}

showExportCommand(CONFIG, instance)
return nil
},
},
Expand All @@ -247,6 +268,10 @@ func main() {
app.Run(os.Args)
}

func showExportCommand(config Config, instance models.Instance) {
fmt.Printf("export PGHOST=%s PGPORT=%d PGUSER=postgres PGPASSWORD=''\n", config.Domain, instance.Port)
}

func ImageToString(i models.Image) string {
return fmt.Sprintf("%d [ %s - READY: %5t ]", i.ID, i.BackedUpAt.Format(time.RFC3339), i.Ready)
}
Expand Down
26 changes: 26 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -69,6 +71,30 @@ func (c Client) delete(url string) (*http.Response, error) {
return http.DefaultClient.Do(req)
}

func (c Client) GetLatestImage() (models.Image, error) {
var image models.Image
images, err := c.ListImages()

if err != nil {
fmt.Printf("error: %s\n", err)
return image, err
}

// Make sure they are all sorted by UpdatedAt
// The most up to date should be the first of the slice
sort.Slice(images, func(i, j int) bool {
return images[i].UpdatedAt.After(images[j].UpdatedAt)
})

for _, image := range images {
if image.Ready {
return image, nil
}
}

return image, errors.New("no images available")
}

func (c Client) GetImage(id string) (models.Image, error) {
var image models.Image
resp, err := c.get(c.URL + "/images/" + id)
Expand Down

0 comments on commit d0e6019

Please sign in to comment.