| 162 | } |
| 163 | |
| 164 | func parseGitArgs(args []string) (gitOptions, error) { |
| 165 | var opts gitOptions |
| 166 | fs := flag.NewFlagSet("git", flag.ContinueOnError) |
| 167 | fs.SetOutput(io.Discard) |
| 168 | fs.StringVar(&opts.in, "in", "", "directory inside the Git workspace") |
| 169 | fs.StringVar(&opts.run, "run", "", "inspect a retained run, by ID or latest") |
| 170 | fs.BoolVar(&opts.before, "before", false, "inspect that run's starting state instead") |
| 171 | fs.IntVar(&opts.build, "build", 1, "one-based build number in the retained run") |
| 172 | fs.StringVar(&opts.stdin, "stdin", "", "standard input for git") |
| 173 | fs.DurationVar(&opts.timeout, "timeout", defaultTimeout, "how long git may take") |
| 174 | if err := fs.Parse(args); err != nil { |
| 175 | return gitOptions{}, usageError{err} |
| 176 | } |
| 177 | var hasBuild, hasRun bool |
| 178 | fs.Visit(func(f *flag.Flag) { |
| 179 | hasBuild = hasBuild || f.Name == "build" |
| 180 | hasRun = hasRun || f.Name == "run" |
| 181 | }) |
| 182 | opts.args = fs.Args() |
| 183 | switch { |
| 184 | case len(opts.args) == 0: |
| 185 | return gitOptions{}, newUsageError("missing git arguments; put them after --") |
| 186 | case opts.in != "" && !filepath.IsLocal(opts.in): |
| 187 | return gitOptions{}, newUsageError("--in must be a relative path inside the Git workspace") |
| 188 | case opts.build < 1: |
| 189 | return gitOptions{}, newUsageError("--build must be positive") |
| 190 | case hasRun && opts.run == "": |
| 191 | return gitOptions{}, newUsageError("--run cannot be empty") |
| 192 | case opts.run == "" && hasBuild: |
| 193 | return gitOptions{}, newUsageError("--build requires --run") |
| 194 | case opts.run == "" && opts.before: |
| 195 | return gitOptions{}, newUsageError("--before requires --run") |
| 196 | case opts.before && hasBuild: |
| 197 | return gitOptions{}, newUsageError("--before and --build cannot be used together") |
| 198 | case opts.timeout <= 0: |
| 199 | return gitOptions{}, newUsageError("--timeout must be positive") |
| 200 | } |
| 201 | return opts, nil |
| 202 | } |
| 203 | |
| 204 | func (s *state) gitWorld(opts gitOptions) (string, error) { |
| 205 | if opts.run == "" { |