Skip to content

Commit

Permalink
#35 consider vagrant images is the default
Browse files Browse the repository at this point in the history
add suggestion for images that appears in case of invalid image passed in create cmd
  • Loading branch information
mhewedy committed Dec 9, 2020
1 parent 0bdc10d commit d2a705a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion command/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Images are cached after the first time it is downloaded.
You can find images from Vagrant at: https://app.vagrantup.com/search
example images:
* ubuntu/trusty64
* ubuntu/focal64
* hashicorp/precise64
* generic/centos8
* generic/alpine38
Expand Down
2 changes: 1 addition & 1 deletion images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Display() (string, error) {
if len(list) == 0 {
return `You can find images from Vagrant at: https://app.vagrantup.com/search
example images:
* ubuntu/trusty64
* ubuntu/focal64
* hashicorp/precise64
* generic/centos8
* generic/alpine38
Expand Down
83 changes: 66 additions & 17 deletions images/vagrant/image_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vagrant

import (
"encoding/json"
"errors"
"fmt"
"github.com/mhewedy/vermin/hypervisor"
"github.com/mhewedy/vermin/progress"
Expand All @@ -23,39 +24,35 @@ type version struct {
Providers []provider `json:"providers"`
}

type suggestionResp struct {
Boxes []struct {
Tag string `json:"tag"`
} `json:"boxes"`
}

func GetImageURL(image string) (string, error) {

stop := progress.Show("Getting Image info from Vagrant Cloud", false)
defer stop()

user, imageName, imageVersion := getImageParts(image)

req, err := http.NewRequest("GET", fmt.Sprintf("https://app.vagrantup.com/%s/boxes/%s", user, imageName), nil)
if err != nil {
return "", err
}

req.Header.Set("Accept", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
bytes, err := sendAndReceive(fmt.Sprintf("https://app.vagrantup.com/%s/boxes/%s", user, imageName))
if err != nil {
return "", err
return "", nil
}

var obj vagrantResp
if err := json.Unmarshal(b, &obj); err != nil {
if err := json.Unmarshal(bytes, &obj); err != nil {
return "", err
}

p, err := filterByVersion(obj, user, imageName, imageVersion)
if err != nil {
suggest, _ := getSuggestion(imageName, "* ")
if suggest != nil && len(suggest) > 0 {
return "", errors.New(err.Error() + ", do you mean:\n" + strings.Join(suggest, "\n"))
}
return "", err
}

Expand Down Expand Up @@ -114,3 +111,55 @@ func getImageParts(image string) (user string, imageName string, imageVersion st
}
return user, imageName, imageVersion
}

func getSuggestion(imageName, prepend string) ([]string, error) {

h, err := hypervisor.GetHypervisorName(false)
if err != nil {
return nil, err
}

bytes, err := sendAndReceive(fmt.Sprintf("https://app.vagrantup.com/api/v1/search?sort=downloads&provider=%s&q=%s&limit=2",
h, imageName))
if err != nil {
return nil, nil
}

var obj suggestionResp
if err := json.Unmarshal(bytes, &obj); err != nil {
return nil, err
}

size := len(obj.Boxes)
if size == 0 {
return []string{}, nil
}

result := make([]string, size)
for i, b := range obj.Boxes {
result[i] = prepend + b.Tag
}
return result, nil
}

func sendAndReceive(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

req.Header.Set("Accept", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}

0 comments on commit d2a705a

Please sign in to comment.