Skip to content

Commit

Permalink
Implement some code review suggestion
Browse files Browse the repository at this point in the history
* Rename CLI flags

  * --no-digest-only
    Only prune images without a digest specified ("fallback" images usually)
  * --unreferenced-only
    Only prune downloads not referenced in any VM or template

* Replace sha256.sha256() call with `digest` module functions

Signed-off-by: Yury Bushmelev <jay4mail@gmail.com>
  • Loading branch information
jay7x committed Mar 20, 2023
1 parent 7db52e7 commit fa8ceee
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"crypto/sha256"
"fmt"
"os"
"path/filepath"

"github.com/lima-vm/lima/pkg/downloader"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/templatestore"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -23,17 +22,17 @@ func newPruneCommand() *cobra.Command {
ValidArgsFunction: cobra.NoFileCompletions,
}

pruneCommand.Flags().BoolP("fallback", "F", false, "Only prune images without a digest specified (fallback images usually)")
pruneCommand.Flags().BoolP("unreferenced", "U", false, "Only prune downloads not referenced in any VM or template")
pruneCommand.Flags().Bool("no-digest-only", false, "Only prune images without a digest specified (\"fallback\" images usually)")
pruneCommand.Flags().Bool("unreferenced-only", false, "Only prune downloads not referenced in any VM or template")
return pruneCommand
}

func pruneAction(cmd *cobra.Command, args []string) error {
pruneFallback, err := cmd.Flags().GetBool("fallback")
pruneFallback, err := cmd.Flags().GetBool("no-digest-only")
if err != nil {
return err
}
pruneUnreferenced, err := cmd.Flags().GetBool("unreferenced")
pruneUnreferenced, err := cmd.Flags().GetBool("unreferenced-only")
if err != nil {
return err
}
Expand Down Expand Up @@ -90,10 +89,6 @@ func pruneAction(cmd *cobra.Command, args []string) error {
return os.RemoveAll(cacheDir)
}

func makeDigest(s string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
}

// Collect all downloads referenced in VM definitions and templates
func getReferencedDownloads() (map[string]limayaml.File, error) {
digests := make(map[string]limayaml.File)
Expand All @@ -110,8 +105,9 @@ func getReferencedDownloads() (map[string]limayaml.File, error) {

logrus.Debug("Referenced downloads:")
for _, f := range append(vmRefs, tmplRefs...) {
logrus.Debugf("+ %q => %q", makeDigest(f.Location), f)
digests[makeDigest(f.Location)] = f
d := digest.SHA256.FromString(f.Location).Encoded()
logrus.Debugf("+ %q => %q", d, f)
digests[d] = f
}
logrus.Debug(".")
return digests, nil
Expand Down

0 comments on commit fa8ceee

Please sign in to comment.