diff --git a/cli/images.go b/cli/images.go index b683a15..10614e9 100644 --- a/cli/images.go +++ b/cli/images.go @@ -36,8 +36,8 @@ Use the image in creating a VM: $ vermin create `, 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) @@ -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") } diff --git a/images/images.go b/images/images.go index cb2e228..49e160b 100644 --- a/images/images.go +++ b/images/images.go @@ -23,7 +23,7 @@ func Create(image string) error { } } - remote, err := listRemoteImages() + remote, err := listRemoteImages(false) if err != nil { return err } @@ -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) @@ -53,7 +53,7 @@ type image struct { } func List() ([]string, error) { - list, err := list() + list, err := list(false) if err != nil { return nil, err } @@ -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 } @@ -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() @@ -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 } diff --git a/images/remote.go b/images/remote.go index 3faf921..bbd1d3c 100644 --- a/images/remote.go +++ b/images/remote.go @@ -6,6 +6,7 @@ import ( "github.com/mhewedy/vermin/db" "io/ioutil" "os" + "path/filepath" "strings" ) @@ -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 } @@ -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) @@ -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, }) @@ -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 / name // Check Unique name func validate(vms []rimage) error {