Skip to content

Commit

Permalink
support for purging images list cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 1, 2020
1 parent 280b5d2 commit 565ccb4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cli/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Use the image in creating a VM:
$ vermin create <image>
`,
Run: func(cmd *cobra.Command, args []string) {

i, err := images.Display()
purge, _ := cmd.Flags().GetBool("purge")
i, err := images.Display(purge)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -57,5 +57,5 @@ func init() {

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
//imagesCmd.Flags().BoolP("all", "a", false, "List all VMs")
imagesCmd.Flags().BoolP("purge", "p", false, "Purge images list cache")
}
18 changes: 9 additions & 9 deletions images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Create(image string) error {
}
}

remote, err := listRemoteImages()
remote, err := listRemoteImages(false)
if err != nil {
return err
}
Expand All @@ -39,9 +39,9 @@ func Create(image string) error {
}

if rimage == nil {
display, _ := List()
return errors.New(fmt.Sprintf("invalid image name: '%s'.", image) +
" Valid images are:\n" + strings.Join(display, "\n"))
l, _ := List()
return errors.New(fmt.Sprintf("invalid image name: '%s',", image) +
" valid images are:\n" + strings.Join(l, "\n"))
}

return download(rimage)
Expand All @@ -53,7 +53,7 @@ type image struct {
}

func List() ([]string, error) {
list, err := list()
list, err := list(false)
if err != nil {
return nil, err
}
Expand All @@ -64,9 +64,9 @@ func List() ([]string, error) {
return result, nil
}

func Display() (string, error) {
func Display(purgeCache bool) (string, error) {

list, err := list()
list, err := list(purgeCache)
if err != nil {
return "", err
}
Expand All @@ -82,7 +82,7 @@ func Display() (string, error) {
return result, nil
}

func list() ([]image, error) {
func list(purgeCache bool) ([]image, error) {
var result []image

cached, err := listCachedImages()
Expand All @@ -93,7 +93,7 @@ func list() ([]image, error) {
result = append(result, image{cached[i], true})
}

remote, err := listRemoteImagesNames()
remote, err := listRemoteImagesNames(purgeCache)
if err != nil {
return nil, err
}
Expand Down
35 changes: 22 additions & 13 deletions images/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mhewedy/vermin/db"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

Expand All @@ -16,9 +17,8 @@ type rimage struct {
URL string `csv:"url"`
}

func listRemoteImagesNames() ([]string, error) {

vms, err := listRemoteImages()
func listRemoteImagesNames(purgeCache bool) ([]string, error) {
vms, err := listRemoteImages(purgeCache)
if err != nil {
return nil, err
}
Expand All @@ -32,17 +32,15 @@ func listRemoteImagesNames() ([]string, error) {
return images, nil
}

func listRemoteImages() ([]rimage, error) {
var tmp string
// read images csv from tmp cache
dir, err := ioutil.ReadDir(os.TempDir())
for i := range dir {
if strings.HasPrefix(dir[i].Name(), db.ImageFile) {
tmp = os.TempDir() + "/" + dir[i].Name()
break
func listRemoteImages(purgeCache bool) ([]rimage, error) {
if purgeCache {
file, _ := getCSVTempFilePath()
if len(file) > 0 {
_ = os.Remove(file)
}
}

// read images csv from tmp cache
tmp, _ := getCSVTempFilePath()
// if not found, then download the file
if len(tmp) == 0 {
tmpFile, err := ioutil.TempFile("", db.ImageFile)
Expand All @@ -59,7 +57,7 @@ func listRemoteImages() ([]rimage, error) {

// parse the file as csv
var vms []rimage
err = csvtag.Load(csvtag.Config{
err := csvtag.Load(csvtag.Config{
Path: tmp,
Dest: &vms,
})
Expand All @@ -76,6 +74,17 @@ func listRemoteImages() ([]rimage, error) {
return vms, nil
}

func getCSVTempFilePath() (string, error) {
file, err := filepath.Glob(os.TempDir() + "/" + db.ImageFile + "*")
if err != nil {
return "", err
}
if len(file) == 0 {
return "", nil
}
return file[0], nil
}

// Check name follows <distro>/<version> name
// Check Unique name
func validate(vms []rimage) error {
Expand Down

0 comments on commit 565ccb4

Please sign in to comment.