| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | |
| 11 | type VMOpts struct { |
| 12 | Disk, Memory, CPUs int |
| 13 | SSHPort int |
| 14 | Reset bool |
| 15 | ReadOnly bool |
| 16 | Scratch bool |
| 17 | UnsafeWritableGit bool |
| 18 | UnsafeNoPrompts bool |
| 19 | RM bool |
| 20 | } |
| 21 | |
| 22 | func newVMOpts() VMOpts { |
| 23 | return VMOpts{SSHPort: -1} |
| 24 | } |
| 25 | |
| 26 | var positiveRe = regexp.MustCompile(`^[1-9][0-9]*$`) |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | func (o *VMOpts) take(args []string) (int, error) { |
| 32 | if len(args) == 0 { |
| 33 | return 0, nil |
| 34 | } |
| 35 | opt, val, n := args[0], "", 0 |
| 36 | switch opt { |
| 37 | case "--disk", "--memory", "--ram", "--cpus", "--ssh-port": |
| 38 | if len(args) < 2 { |
| 39 | return 0, fmt.Errorf("%s needs a value.", opt) |
| 40 | } |
| 41 | val, n = args[1], 2 |
| 42 | case "--reset": |
| 43 | o.Reset = true |
| 44 | return 1, nil |
| 45 | case "--readonly": |
| 46 | o.ReadOnly = true |
| 47 | return 1, nil |
| 48 | case "--scratch": |
| 49 | o.Scratch = true |
| 50 | return 1, nil |
| 51 | case "--unsafe-writable-git", "--unsafe-writable-git=1": |
| 52 | o.UnsafeWritableGit = true |
| 53 | return 1, nil |
| 54 | case "--unsafe-disable-security-prompts": |
| 55 | o.UnsafeNoPrompts = true |
| 56 | return 1, nil |
| 57 | case "--rm": |
| 58 | o.RM = true |
| 59 | return 1, nil |
| 60 | default: |
| 61 | name, v, ok := strings.Cut(opt, "=") |
| 62 | switch { |
| 63 | case !ok: |
| 64 | return 0, nil |
| 65 | case name == "--disk", name == "--memory", name == "--ram", name == "--cpus", name == "--ssh-port": |
| 66 | opt, val, n = name, v, 1 |
| 67 | default: |
| 68 | return 0, nil |
| 69 | } |
| 70 | } |
| 71 | if opt == "--ram" { |
| 72 | opt = "--memory" |
| 73 | } |
| 74 | if opt == "--ssh-port" { |
| 75 | p, err := strconv.Atoi(val) |
| 76 | if val != "0" && (!positiveRe.MatchString(val) || err != nil || p < 1024 || p > 65535) { |
| 77 | return 0, fmt.Errorf("--ssh-port must be 0 or a port from 1024 to 65535 (got: '%s')", val) |
| 78 | } |
| 79 | o.SSHPort = p |
| 80 | return n, nil |
| 81 | } |
| 82 | v, err := strconv.Atoi(val) |
| 83 | if !positiveRe.MatchString(val) || err != nil { |
| 84 | return 0, fmt.Errorf("%s must be a positive integer (got: '%s')", opt, val) |
| 85 | } |
| 86 | switch opt { |
| 87 | case "--disk": |
| 88 | o.Disk = v |
| 89 | case "--memory": |
| 90 | o.Memory = v |
| 91 | default: |
| 92 | o.CPUs = v |
| 93 | } |
| 94 | return n, nil |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | func (o *VMOpts) takeAll(args []string) (taken, rest []string, err error) { |
| 100 | for { |
| 101 | n, err := o.take(args) |
| 102 | if err != nil { |
| 103 | return nil, nil, err |
| 104 | } |
| 105 | if n == 0 { |
| 106 | return taken, args, nil |
| 107 | } |
| 108 | taken = append(taken, args[:n]...) |
| 109 | args = args[n:] |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | func (o *VMOpts) split(args []string) (cmd []string, tty bool, err error) { |
| 118 | for { |
| 119 | n, err := o.take(args) |
| 120 | if err != nil { |
| 121 | return nil, false, err |
| 122 | } |
| 123 | if n > 0 { |
| 124 | args = args[n:] |
| 125 | continue |
| 126 | } |
| 127 | if len(args) > 0 && args[0] == "--tty" { |
| 128 | tty = true |
| 129 | args = args[1:] |
| 130 | continue |
| 131 | } |
| 132 | if len(args) > 0 && args[0] == "--" { |
| 133 | args = args[1:] |
| 134 | } |
| 135 | return args, tty, nil |
| 136 | } |
| 137 | } |