support --web for gist view

This commit is contained in:
vilmibm 2020-09-11 20:50:25 -05:00 committed by vilmibm
parent 0d3056d9a7
commit dc345a9f73
2 changed files with 21 additions and 0 deletions

View file

@ -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)
}

View file

@ -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, "/") {