| 34 | } |
| 35 | |
| 36 | func parseSetArgs(args []string) (setOptions, error) { |
| 37 | var opts setOptions |
| 38 | fs := flag.NewFlagSet("set", flag.ContinueOnError) |
| 39 | fs.SetOutput(io.Discard) |
| 40 | fs.IntVar(&opts.index, "index", 0, "which response to change, starting at 1") |
| 41 | fs.BoolVar(&opts.appendNew, "append", false, "add a new response after the existing ones") |
| 42 | fs.IntVar(&opts.status, "status", 0, "HTTP status code") |
| 43 | fs.Var((*stringList)(&opts.headers), "header", "'Name: value' to set, or 'Name:' to remove") |
| 44 | fs.StringVar(&opts.body, "body", "", "JSON body that replaces the current one") |
| 45 | fs.StringVar(&opts.bodyFile, "body-file", "", "file holding the JSON body") |
| 46 | fs.StringVar(&opts.rawFile, "raw-file", "", "file holding the exact response bytes") |
| 47 | fs.StringVar(&opts.zipDir, "zip-dir", "", "directory to serve as a ZIP response") |
| 48 | fs.StringVar(&opts.at, "at", "", "dotted path inside the body, such as data.repository.issue.title") |
| 49 | fs.StringVar(&opts.value, "json", "", "JSON value to put at --at") |
| 50 | fs.BoolVar(&opts.offSchema, "off-schema", false, "save the response even if it doesn't match GitHub's schema") |
| 51 | |
| 52 | name, err := parseWithName(fs, args) |
| 53 | if err != nil { |
| 54 | return setOptions{}, err |
| 55 | } |
| 56 | opts.name = name |
| 57 | specified := map[string]bool{} |
| 58 | fs.Visit(func(f *flag.Flag) { |
| 59 | specified[f.Name] = true |
| 60 | switch f.Name { |
| 61 | case "body": |
| 62 | opts.bodySet = true |
| 63 | case "json": |
| 64 | opts.valueSet = true |
| 65 | } |
| 66 | }) |
| 67 | |
| 68 | if opts.name == "" { |
| 69 | return setOptions{}, newUsageError("missing request name, such as \"GET /repos/octo/hello\" or \"graphql IssueByNumber\"") |
| 70 | } |
| 71 | if err := validateName(opts.name); err != nil { |
| 72 | return setOptions{}, usageError{err} |
| 73 | } |
| 74 | switch { |
| 75 | case specified["raw-file"] && opts.rawFile == "": |
| 76 | return setOptions{}, newUsageError("--raw-file needs a file path") |
| 77 | case specified["zip-dir"] && opts.zipDir == "": |
| 78 | return setOptions{}, newUsageError("--zip-dir needs a directory path") |
| 79 | case opts.bodySet && opts.bodyFile != "": |
| 80 | return setOptions{}, newUsageError("use either --body or --body-file, not both") |
| 81 | case (opts.rawFile != "" || opts.zipDir != "") && (opts.bodySet || opts.bodyFile != "" || opts.at != "" || opts.valueSet): |
| 82 | return setOptions{}, newUsageError("--raw-file and --zip-dir cannot be combined with JSON body flags") |
| 83 | case opts.rawFile != "" && opts.zipDir != "": |
| 84 | return setOptions{}, newUsageError("use either --raw-file or --zip-dir, not both") |
| 85 | case opts.at != "" && !opts.valueSet: |
| 86 | return setOptions{}, newUsageError("--at needs --json") |
| 87 | case opts.valueSet && opts.at == "": |
| 88 | return setOptions{}, newUsageError("--json needs --at") |
| 89 | case opts.appendNew && opts.index != 0: |
| 90 | return setOptions{}, newUsageError("use either --append or --index, not both") |
| 91 | case opts.index < 0: |
| 92 | return setOptions{}, newUsageError("--index starts at 1") |
| 93 | case opts.status != 0 && (opts.status < 100 || opts.status > 599): |