-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
236 lines (194 loc) · 5.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Short: "GitHub markdown link tools",
}
var linkCmd = &cobra.Command{
Use: "link <url>",
Short: "Convert an input URL into a markdown link",
Long: `Convert an input URL into a markdown link.
The input can be one of:
* a GitHub repository URL
* a GitHub issue URL
* a GitHub pull request URL
* a GitHub discussion URL
* a GitHub issue reference (e.g. "cli/cli#123")
* a markdown link containing one of the above URL types
The output is a markdown link to the input URL, with the link text being the
issue/PR/discussion reference as well as its title fetched from the GitHub API.
Include the --simple flag to disable title lookup.
If the input is unrecognized it will be returned as-is.
Examples:
$ gh md link https://github.com/cli/cli/pull/123
[cli/cli#123: Tweak flags language](https://github.com/cli/cli/pull/123)
$ gh md link --simple https://github.com/cli/cli/pull/123
[cli/cli#123](https://github.com/cli/cli/pull/123)
$ gh md link cli/cli#123
[cli/cli#123: Tweak flags language](https://github.com/cli/cli/pull/123)`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
simple, _ := cmd.Flags().GetBool("simple")
out, err := link(args[0], simple)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
printOptionalNewline(cmd, out)
},
}
var refCmd = &cobra.Command{
Use: "ref <url>",
Aliases: []string{"reference"},
Short: "Convert an input URL into an issue reference",
Long: `Convert an input URL into an issue reference.
The input can be one of:
* A GitHub issue URL
* A GitHub pull request URL
* A GitHub discussion URL
* An issue reference ("cli/cli#123")
* A markdown link containing one of the above
Example:
$ gh md ref https://github.com/cli/cli/pull/123
cli/cli#123`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
out, err := ref(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
printOptionalNewline(cmd, out)
},
}
var titleCmd = &cobra.Command{
Use: "title <url>",
Short: "Fetch the title of a GitHub issue, pull request, or discussion",
Long: `Fetch the title of a GitHub issue, pull request, or discussion.
The input can be one of:
* A GitHub issue URL
* A GitHub pull request URL
* A GitHub discussion URL
* An issue reference ("cli/cli#123")
* A markdown link containing one of the above
Example:
$ gh md title cli/cli#123
Tweak flags language`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
sanitize, _ := cmd.Flags().GetBool("sanitize")
out, err := title(args[0], sanitize)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
printOptionalNewline(cmd, out)
},
}
var urlCmd = &cobra.Command{
Use: "url <ref or markdown link>",
Short: "Convert an issue reference or markdown link into a bare GitHub URL",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
out, err := url(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
printOptionalNewline(cmd, out)
},
}
var client api.GQLClient
func init() {
for _, cmd := range []*cobra.Command{linkCmd, refCmd, titleCmd, urlCmd} {
cmd.Flags().BoolP("no-newline", "n", false, "Do not print trailing newline")
}
linkCmd.Flags().Bool("simple", false, "Disable title lookup")
titleCmd.Flags().Bool("sanitize", false, "Sanitize output for use as a file path")
rootCmd.AddCommand(linkCmd, refCmd, titleCmd, urlCmd)
}
func main() {
var err error
opts := &api.ClientOptions{
EnableCache: true,
Timeout: 10 * time.Second,
}
client, err = gh.GQLClient(opts)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func link(input string, simple bool) (string, error) {
reference, matched := match(input)
if !matched {
return input, nil
}
ref := reference.Reference()
if simple {
return fmt.Sprintf("[%s](%s)", ref, reference.URL()), nil
} else {
title, err := reference.Title(client)
if err != nil {
return input, err
}
if strings.Count(title, "::") > 1 {
title = strings.ReplaceAll(title, "::", "-")
}
return fmt.Sprintf("[%s: %s](%s)", ref, title, reference.URL()), nil
}
}
func ref(input string) (string, error) {
reference, matched := match(input)
if !matched {
return input, nil
}
return reference.Reference(), nil
}
func title(input string, sanitize bool) (string, error) {
reference, matched := match(input)
if !matched {
fmt.Fprintf(os.Stderr, "didn't match %s", input)
return input, nil
}
title, err := reference.Title(client)
if err != nil {
return input, err
}
// sanitize for paths, remove : / ? chars
if sanitize {
title = regexp.MustCompile(`:+|/`).ReplaceAllString(title, " - ")
title = strings.ReplaceAll(title, "/", "-")
title = strings.ReplaceAll(title, "?", "")
}
title = regexp.MustCompile(`\s+`).ReplaceAllString(title, " ")
title = strings.ReplaceAll(title, "[", "(")
title = strings.ReplaceAll(title, "]", ")")
if strings.Count(title, "::") > 1 {
title = strings.ReplaceAll(title, "::", "-")
}
return title, nil
}
func url(input string) (string, error) {
reference, matched := match(input)
if !matched {
return input, nil
}
return reference.URL(), nil
}
func printOptionalNewline(cmd *cobra.Command, output string) {
noNewline, _ := cmd.Flags().GetBool("no-newline")
if noNewline {
fmt.Print(output)
} else {
fmt.Println(output)
}
}