| 73 | } |
| 74 | |
| 75 | func viewRun(opts *ViewOptions) error { |
| 76 | if opts.License == "" { |
| 77 | return errors.New("no license provided") |
| 78 | } |
| 79 | |
| 80 | client, err := opts.HTTPClient() |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | cfg, err := opts.Config() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | if err := opts.IO.StartPager(); err != nil { |
| 91 | fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err) |
| 92 | } |
| 93 | defer opts.IO.StopPager() |
| 94 | |
| 95 | hostname, _ := cfg.Authentication().DefaultHost() |
| 96 | license, err := api.RepoLicense(client, hostname, opts.License) |
| 97 | if err != nil { |
| 98 | if httpErr, ok := errors.AsType[api.HTTPError](err); ok { |
| 99 | if httpErr.StatusCode == 404 { |
| 100 | return fmt.Errorf("'%s' is not a valid license name or SPDX ID.\n\nRun `gh repo license list` to see available commonly used licenses. For even more licenses, visit %s", opts.License, text.DisplayURL("https://choosealicense.com/appendix")) |
| 101 | } |
| 102 | } |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | if opts.Web { |
| 107 | url := fmt.Sprintf("https://choosealicense.com/licenses/%s", license.Key) |
| 108 | if opts.IO.IsStdoutTTY() { |
| 109 | fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(url)) |
| 110 | } |
| 111 | return opts.Browser.Browse(url) |
| 112 | } |
| 113 | |
| 114 | return renderLicense(license, opts) |
| 115 | } |
| 116 | |
| 117 | func renderLicense(license *api.License, opts *ViewOptions) error { |
| 118 | cs := opts.IO.ColorScheme() |