X Tutup
Skip to content

Commit dc345a9

Browse files
vilmibmvilmibm
authored andcommitted
support --web for gist view
1 parent 0d3056d commit dc345a9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

internal/ghinstance/host.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ func RESTPrefix(hostname string) string {
5555
}
5656
return "https://api.github.com/"
5757
}
58+
59+
func GistPrefix(hostname string) string {
60+
if IsEnterprise(hostname) {
61+
return fmt.Sprintf("https://%s/gist/", hostname)
62+
}
63+
return fmt.Sprintf("https://gist.%s/", hostname)
64+
}

pkg/cmd/gist/view/view.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type ViewOptions struct {
2020

2121
Selector string
2222
Raw bool
23+
Web bool
2324
}
2425

2526
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
@@ -47,13 +48,26 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4748
}
4849

4950
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "do not try and render markdown")
51+
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "open gist in browser")
5052

5153
return cmd
5254
}
5355

5456
func viewRun(opts *ViewOptions) error {
5557
gistID := opts.Selector
5658

59+
if opts.Web {
60+
gistURL := gistID
61+
if !strings.Contains(gistURL, "/") {
62+
hostname := ghinstance.OverridableDefault()
63+
gistURL = ghinstance.GistPrefix(hostname) + gistID
64+
}
65+
if opts.IO.IsStderrTTY() {
66+
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(gistURL))
67+
}
68+
return utils.OpenInBrowser(gistURL)
69+
}
70+
5771
u, err := url.Parse(opts.Selector)
5872
if err == nil {
5973
if strings.HasPrefix(u.Path, "/") {

0 commit comments

Comments
 (0)
X Tutup