mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/devvmhq/devvm/internal/from"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// addFromFlag adds --from, the container image a base starts from.
|
|
func addFromFlag(f *pflag.FlagSet, p *string) {
|
|
f.StringVar(p, "from", "", "the container `IMAGE` base starts from, such as node:24, instead of Ubuntu's cloud image (default: $DEVVM_FROM)")
|
|
}
|
|
|
|
// addAgentsFlag adds --agents, a list of coding agents: a new VM's, or
|
|
// new VMs' default with devvm setup. usage says whose.
|
|
func addAgentsFlag(f *pflag.FlagSet, p *string, usage string) {
|
|
f.StringVar(p, "agents", "", usage)
|
|
}
|
|
|
|
// addLayersFlag adds --layers, a list of layers: a new VM's, or new VMs'
|
|
// default with devvm setup. usage says whose.
|
|
func addLayersFlag(f *pflag.FlagSet, p *string, usage string) {
|
|
f.StringVar(p, "layers", "", usage)
|
|
}
|
|
|
|
// addForceFlag adds -f/--force, which only skips a confirmation.
|
|
func addForceFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVarP(p, "force", "f", false, "don't ask to confirm, which devvm needs without a terminal or with --no-input")
|
|
}
|
|
|
|
// addNoExportFlag adds --no-export, which skips exporting the agents'
|
|
// history before a VM's disk goes (devvm rm, devvm layer add, devvm
|
|
// layer rm, devvm update, devvm clone).
|
|
func addNoExportFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "no-export", false, "don't export the agents' history first")
|
|
}
|
|
|
|
// addNoCloneCheckFlag adds --no-clone-check, which skips checking a
|
|
// --clone VM's clone before its disk goes (devvm rm, devvm clone off).
|
|
func addNoCloneCheckFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "no-clone-check", false, "don't check a --clone VM's clone for unfetched commits and uncommitted changes first, which are lost")
|
|
}
|
|
|
|
// addRemakeFlags adds the flags of a command that makes the folder's VM
|
|
// again (devvm layer add, devvm layer rm, devvm update, devvm clone):
|
|
// --force, --no-export and --clean-home.
|
|
func addRemakeFlags(f *pflag.FlagSet, o *vm.RemakeOptions) {
|
|
addForceFlag(f, &o.Force)
|
|
addNoExportFlag(f, &o.NoExport)
|
|
f.BoolVar(&o.CleanHome, "clean-home", false, "give the VM the image's home folder instead of keeping its own")
|
|
}
|
|
|
|
// addSSHConfigFlag adds --ssh-config, which answers the question whether
|
|
// to add devvm's Include line to ~/.ssh/config.
|
|
func addSSHConfigFlag(f *pflag.FlagSet, p *bool) {
|
|
f.BoolVar(p, "ssh-config", false, "add devvm's Include line to ~/.ssh/config without asking")
|
|
}
|
|
|
|
// folderFlags are --here and --parent: which VM a command on this
|
|
// folder's VM uses when a folder it's inside has one.
|
|
type folderFlags struct{ here, parent bool }
|
|
|
|
// rule is the VM environment's rule for them (vm.Env.Folder).
|
|
func (ff folderFlags) rule() vm.FolderRule {
|
|
switch {
|
|
case ff.here:
|
|
return vm.FolderHere
|
|
case ff.parent:
|
|
return vm.FolderParent
|
|
}
|
|
return vm.FolderAuto
|
|
}
|
|
|
|
// addFolderFlags adds --here and --parent to cmd, a command that acts on
|
|
// this folder's VM, into the run's (a.folder), which its VM environment
|
|
// finds the VM by. They can't go together, nor with -g on a command that
|
|
// has it, added before them. It returns cmd.
|
|
func addFolderFlags(cmd *cobra.Command, a *app) *cobra.Command {
|
|
f := cmd.Flags()
|
|
f.BoolVar(&a.folder.here, "here", false, "use this folder's own VM, never that of a folder it's inside")
|
|
f.BoolVar(&a.folder.parent, "parent", false, "use the VM of the nearest folder this one is inside, even across a git repo of its own")
|
|
names := []string{"here", "parent"}
|
|
if f.Lookup("global") != nil {
|
|
names = append(names, "global")
|
|
}
|
|
exclusive(cmd, names...)
|
|
return cmd
|
|
}
|
|
|
|
// fromRef is --from as given, else its default from the environment.
|
|
func fromRef(flag string) string {
|
|
if flag != "" {
|
|
return flag
|
|
}
|
|
return os.Getenv(from.Env)
|
|
}
|
|
|
|
// noBlanks refuses a flag given an empty value, and an empty argument: no
|
|
// flag or argument takes a blank to mean none or its default. A command
|
|
// that passes its arguments on as they are (limactl) is left alone.
|
|
func noBlanks(cmd *cobra.Command, args []string) error {
|
|
if cmd.DisableFlagParsing {
|
|
return nil
|
|
}
|
|
var err error
|
|
cmd.Flags().Visit(func(f *pflag.Flag) {
|
|
if err == nil && f.Value.Type() == "string" && f.Value.String() == "" {
|
|
err = msg.FlagBlank(f.Name)
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if slices.Contains(args, "") {
|
|
return msg.ArgBlank(cmd.CommandPath())
|
|
}
|
|
return nil
|
|
}
|