MCPcopy Create free account
hub / github.com/cli/cli / viewRun

Function viewRun

pkg/cmd/repo/license/view/view.go:75–115  ·  view source on GitHub ↗
(opts *ViewOptions)

Source from the content-addressed store, hash-verified

73}
74
75func 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
117func renderLicense(license *api.License, opts *ViewOptions) error {
118 cs := opts.IO.ColorScheme()

Callers 2

TestViewRunFunction · 0.70
NewCmdViewFunction · 0.70

Calls 11

RepoLicenseFunction · 0.92
DisplayURLFunction · 0.92
renderLicenseFunction · 0.85
StartPagerMethod · 0.80
StopPagerMethod · 0.80
IsStdoutTTYMethod · 0.80
ConfigMethod · 0.65
DefaultHostMethod · 0.65
AuthenticationMethod · 0.65
ErrorfMethod · 0.65
BrowseMethod · 0.65

Tested by 1

TestViewRunFunction · 0.56