|
| 1 | +package editflags |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + flag "github.com/spf13/pflag" |
| 9 | +) |
| 10 | + |
| 11 | +// RegisterEdit registers flags related to in-place YAML modification. |
| 12 | +func RegisterEdit(cmd *cobra.Command) { |
| 13 | + flags := cmd.Flags() |
| 14 | + |
| 15 | + flags.Int("cpus", 0, "number of CPUs") // Similar to colima's --cpu |
| 16 | + _ = cmd.RegisterFlagCompletionFunc("cpus", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 17 | + return []string{"1", "2", "4", "8"}, cobra.ShellCompDirectiveNoFileComp |
| 18 | + }) |
| 19 | + flags.Float32("memory", 0, "memory in GiB") // colima-compatible |
| 20 | + _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 21 | + return []string{"1", "2", "4", "8", "16", "32"}, cobra.ShellCompDirectiveNoFileComp |
| 22 | + }) |
| 23 | + flags.String("mount-type", "", "mount type") // Similar to colima's --mount-type=(sshfs|9p|virtiofs), but "reverse-sshfs" is Lima is called "sshfs" in colima |
| 24 | + _ = cmd.RegisterFlagCompletionFunc("mount-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 25 | + return []string{"reverse-sshfs", "9p", "virtiofs"}, cobra.ShellCompDirectiveNoFileComp |
| 26 | + }) |
| 27 | + |
| 28 | + flags.String("set", "", "modify the template inplace, using yq syntax") |
| 29 | +} |
| 30 | + |
| 31 | +// RegisterStart registers flags related to in-place YAML modification. |
| 32 | +func RegisterStart(cmd *cobra.Command) { |
| 33 | + RegisterEdit(cmd) |
| 34 | + flags := cmd.Flags() |
| 35 | + flags.String("arch", "", "machine architecture") // colima-compatible |
| 36 | + _ = cmd.RegisterFlagCompletionFunc("arch", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 37 | + return []string{"x86_64", "aarch64", "riscv64"}, cobra.ShellCompDirectiveNoFileComp |
| 38 | + }) |
| 39 | + |
| 40 | + flags.Float32("disk", 0, "disk size in GiB") // colima-compatible |
| 41 | + _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 42 | + return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp |
| 43 | + }) |
| 44 | + |
| 45 | + flags.String("vm-type", "", "virtual machine type") // colima-compatible |
| 46 | + _ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 47 | + return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +// YQExpressions returns YQ expressions. |
| 52 | +func YQExpressions(flags *flag.FlagSet) ([]string, error) { |
| 53 | + type def struct { |
| 54 | + flagName string |
| 55 | + fmt string |
| 56 | + experimental bool |
| 57 | + } |
| 58 | + defs := []def{ |
| 59 | + {"cpus", ".cpus = %s", false}, |
| 60 | + {"memory", ".memory = \"%sGiB\"", false}, |
| 61 | + {"mount-type", ".mountType = %q", false}, |
| 62 | + {"set", "%s", true}, |
| 63 | + {"arch", ".arch = %q", false}, |
| 64 | + {"disk", ".disk= \"%sGiB\"", true}, |
| 65 | + {"vm-type", ".vmType = %q", false}, |
| 66 | + } |
| 67 | + var exprs []string |
| 68 | + for _, def := range defs { |
| 69 | + v := flags.Lookup(def.flagName) |
| 70 | + if v != nil && v.Changed { |
| 71 | + if def.experimental { |
| 72 | + logrus.Warnf("`--%s` is experimental", def.flagName) |
| 73 | + } |
| 74 | + exprs = append(exprs, fmt.Sprintf(def.fmt, v.Value)) |
| 75 | + } |
| 76 | + } |
| 77 | + return exprs, nil |
| 78 | +} |
0 commit comments