Skip to content

Commit

Permalink
cli: add prune command (#882)
Browse files Browse the repository at this point in the history
* cli: add prune command

* vm: include socat as dependency
  • Loading branch information
abiosoft authored Nov 17, 2023
1 parent 603f37e commit a9df8ba
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
61 changes: 61 additions & 0 deletions cmd/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var pruneCmdArgs struct {
force bool
all bool
}

// pruneCmd represents the prune command
var pruneCmd = &cobra.Command{
Use: "prune",
Short: "prune cached downloaded assets",
Long: `Prune cached downloaded assets`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
colimaCacheDir := config.CacheDir()
limaCacheDir := filepath.Join(filepath.Dir(colimaCacheDir), "lima")
if !pruneCmdArgs.force {
msg := "'" + colimaCacheDir + "' will be emptied, are you sure"
if pruneCmdArgs.all {
msg = "'" + colimaCacheDir + "' and '" + limaCacheDir + "' will be emptied, are you sure"
}
if y := cli.Prompt(msg); !y {
return nil
}
}
logrus.Info("Pruning ", strconv.Quote(config.CacheDir()))
if err := os.RemoveAll(config.CacheDir()); err != nil {
return fmt.Errorf("error during prune: %w", err)
}

if pruneCmdArgs.all {
cmd := limautil.Limactl("prune")
if err := cmd.Run(); err != nil {
return fmt.Errorf("error during Lima prune: %w", err)
}
}

return nil
},
}

func init() {
root.Cmd().AddCommand(pruneCmd)

pruneCmd.Flags().BoolVarP(&pruneCmdArgs.force, "force", "f", false, "do not prompt for yes/no")
pruneCmd.Flags().BoolVarP(&pruneCmdArgs.all, "all", "a", false, "include Lima assets")
}
4 changes: 2 additions & 2 deletions environment/vm/lima/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

var dependencyPackages = []string{
// docker
"docker.io",
// docker and k8s
"docker.io", "socat",
// utilities
"htop", "vim", "inetutils-ping", "dnsutils",
}
Expand Down
2 changes: 1 addition & 1 deletion environment/vm/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func New(host environment.HostActions) environment.VM {
const (
envLimaInstance = "LIMA_INSTANCE"
lima = "lima"
limactl = limautil.Limactl
limactl = limautil.LimactlCommand
)

func (l limaVM) limaConfFile() string {
Expand Down
15 changes: 8 additions & 7 deletions environment/vm/lima/limautil/limautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
// EnvLimaHome is the environment variable for the Lima directory.
const EnvLimaHome = "LIMA_HOME"

// Limactl is the limactl command.
const Limactl = "limactl"
// LimactlCommand is the limactl command.
const LimactlCommand = "limactl"

func limactl(args ...string) *exec.Cmd {
cmd := cli.Command(Limactl, args...)
// Limactl prepares a limactl command.
func Limactl(args ...string) *exec.Cmd {
cmd := cli.Command(LimactlCommand, args...)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, EnvLimaHome+"="+LimaHome())
return cmd
Expand Down Expand Up @@ -98,7 +99,7 @@ const (
func getInstance(profileID string) (InstanceInfo, error) {
var i InstanceInfo
var buf bytes.Buffer
cmd := limactl("list", profileID, "--json")
cmd := Limactl("list", profileID, "--json")
cmd.Stderr = nil
cmd.Stdout = &buf

Expand All @@ -125,7 +126,7 @@ func Instances(ids ...string) ([]InstanceInfo, error) {
args := append([]string{"list", "--json"}, limaIDs...)

var buf bytes.Buffer
cmd := limactl(args...)
cmd := Limactl(args...)
cmd.Stderr = nil
cmd.Stdout = &buf

Expand Down Expand Up @@ -171,7 +172,7 @@ func Instances(ids ...string) ([]InstanceInfo, error) {
func getIPAddress(profileID, interfaceName string) string {
var buf bytes.Buffer
// TODO: this should be less hacky
cmd := limactl("shell", profileID, "sh", "-c",
cmd := Limactl("shell", profileID, "sh", "-c",
`ip -4 addr show `+interfaceName+` | grep inet | awk -F' ' '{print $2 }' | cut -d/ -f1`)
cmd.Stderr = nil
cmd.Stdout = &buf
Expand Down

0 comments on commit a9df8ba

Please sign in to comment.