ParseDiscussionArg parses a discussion number or URL from a command argument. It returns the discussion number and, if the argument was a URL, a repo override.
(arg string)
| 15 | // ParseDiscussionArg parses a discussion number or URL from a command argument. |
| 16 | // It returns the discussion number and, if the argument was a URL, a repo override. |
| 17 | func ParseDiscussionArg(arg string) (int32, ghrepo.Interface, error) { |
| 18 | if num, err := strconv.ParseInt(arg, 10, 32); err == nil { |
| 19 | return int32(num), nil, nil |
| 20 | } |
| 21 | |
| 22 | if len(arg) > 1 && arg[0] == '#' { |
| 23 | if num, err := strconv.ParseInt(arg[1:], 10, 32); err == nil { |
| 24 | return int32(num), nil, nil |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | u, err := url.Parse(arg) |
| 29 | if err != nil || (u.Scheme != "http" && u.Scheme != "https") { |
| 30 | return 0, nil, fmt.Errorf("invalid discussion argument: %q", arg) |
| 31 | } |
| 32 | |
| 33 | // An HTTP URL is also accepted because we only extract the discussion number, |
| 34 | // repo and host from the URL path; no API calls are made over HTTP. |
| 35 | |
| 36 | m := discussionURLRE.FindStringSubmatch(u.Path) |
| 37 | if m == nil { |
| 38 | return 0, nil, fmt.Errorf("invalid discussion URL: %q", arg) |
| 39 | } |
| 40 | |
| 41 | num, err := strconv.ParseInt(m[3], 10, 32) |
| 42 | if err != nil { |
| 43 | return 0, nil, fmt.Errorf("invalid discussion number in URL: %q", m[3]) |
| 44 | } |
| 45 | |
| 46 | repo := ghrepo.NewWithHost(m[1], m[2], u.Hostname()) |
| 47 | return int32(num), repo, nil |
| 48 | } |
| 49 | |
| 50 | // ParsedDiscussionOrCommentArg holds the result of parsing a comment command argument. |
| 51 | // Depending on the input, different fields are populated: |