diff --git a/internal/ghinstance/host.go b/internal/ghinstance/host.go index 560cc9c5c..642dd0846 100644 --- a/internal/ghinstance/host.go +++ b/internal/ghinstance/host.go @@ -55,3 +55,10 @@ func RESTPrefix(hostname string) string { } return "https://api.github.com/" } + +func GistPrefix(hostname string) string { + if IsEnterprise(hostname) { + return fmt.Sprintf("https://%s/gist/", hostname) + } + return fmt.Sprintf("https://gist.%s/", hostname) +} diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 313529d20..5e4da1e69 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -20,6 +20,7 @@ type ViewOptions struct { Selector string Raw bool + Web bool } func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { @@ -47,6 +48,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman } cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "do not try and render markdown") + cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "open gist in browser") return cmd } @@ -54,6 +56,18 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman func viewRun(opts *ViewOptions) error { gistID := opts.Selector + if opts.Web { + gistURL := gistID + if !strings.Contains(gistURL, "/") { + hostname := ghinstance.OverridableDefault() + gistURL = ghinstance.GistPrefix(hostname) + gistID + } + if opts.IO.IsStderrTTY() { + fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(gistURL)) + } + return utils.OpenInBrowser(gistURL) + } + u, err := url.Parse(opts.Selector) if err == nil { if strings.HasPrefix(u.Path, "/") {