| 29 | } |
| 30 | |
| 31 | func NewCmdCreate(f *cmdutil.Factory, runF func(*createOptions) error) *cobra.Command { |
| 32 | opts := &createOptions{ |
| 33 | Browser: f.Browser, |
| 34 | IO: f.IOStreams, |
| 35 | } |
| 36 | |
| 37 | cmd := &cobra.Command{ |
| 38 | Use: "create <keyPrefix> <urlTemplate>", |
| 39 | Short: "Create a new autolink reference", |
| 40 | Long: heredoc.Docf(` |
| 41 | Create a new autolink reference for a repository. |
| 42 | |
| 43 | The %[1]skeyPrefix%[1]s argument specifies the prefix that will generate a link when it is appended by certain characters. |
| 44 | |
| 45 | The %[1]surlTemplate%[1]s argument specifies the target URL that will be generated when the keyPrefix is found, which |
| 46 | must contain %[1]s<num>%[1]s variable for the reference number. |
| 47 | |
| 48 | By default, autolinks are alphanumeric with %[1]s--numeric%[1]s flag used to create a numeric autolink. |
| 49 | |
| 50 | The %[1]s<num>%[1]s variable behavior differs depending on whether the autolink is alphanumeric or numeric: |
| 51 | |
| 52 | - alphanumeric: matches %[1]sA-Z%[1]s (case insensitive), %[1]s0-9%[1]s, and %[1]s-%[1]s |
| 53 | - numeric: matches %[1]s0-9%[1]s |
| 54 | |
| 55 | If the template contains multiple instances of %[1]s<num>%[1]s, only the first will be replaced. |
| 56 | `, "`"), |
| 57 | Example: heredoc.Doc(` |
| 58 | # Create an alphanumeric autolink to example.com for the key prefix "TICKET-". |
| 59 | # Generates https://example.com/TICKET?query=123abc from "TICKET-123abc". |
| 60 | $ gh repo autolink create TICKET- "https://example.com/TICKET?query=<num>" |
| 61 | |
| 62 | # Create a numeric autolink to example.com for the key prefix "STORY-". |
| 63 | # Generates https://example.com/STORY?id=123 from "STORY-123". |
| 64 | $ gh repo autolink create STORY- "https://example.com/STORY?id=<num>" --numeric |
| 65 | `), |
| 66 | Args: cmdutil.ExactArgs(2, "Cannot create autolink: keyPrefix and urlTemplate arguments are both required"), |
| 67 | Aliases: []string{"new"}, |
| 68 | RunE: func(c *cobra.Command, args []string) error { |
| 69 | opts.BaseRepo = f.BaseRepo |
| 70 | |
| 71 | httpClient, err := f.HttpClient() |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | opts.AutolinkClient = &AutolinkCreator{HTTPClient: httpClient} |
| 77 | opts.KeyPrefix = args[0] |
| 78 | opts.URLTemplate = args[1] |
| 79 | |
| 80 | if runF != nil { |
| 81 | return runF(opts) |
| 82 | } |
| 83 | |
| 84 | return createRun(opts) |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | cmd.Flags().BoolVarP(&opts.Numeric, "numeric", "n", false, "Mark autolink as numeric") |