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

Function runCopilot

pkg/cmd/copilot/copilot.go:134–195  ·  view source on GitHub ↗
(opts *CopilotOptions)

Source from the content-addressed store, hash-verified

132}
133
134func runCopilot(opts *CopilotOptions) error {
135 if opts.Remove {
136 if err := removeCopilot(copilotInstallDir()); err != nil {
137 return err
138 }
139
140 if opts.IO.IsStdoutTTY() {
141 fmt.Fprintln(opts.IO.ErrOut, "Copilot CLI removed successfully")
142 }
143 return nil
144 }
145
146 copilotPath := findCopilotBinaryFunc()
147 foundInPath := copilotPath != ""
148 if !foundInPath {
149 if opts.IO.CanPrompt() {
150 confirmed, err := opts.Prompter.Confirm("GitHub Copilot CLI is not installed. Would you like to install it?", true)
151 if err != nil {
152 return err
153 }
154 if !confirmed {
155 fmt.Fprintf(opts.IO.ErrOut, "%s Copilot CLI was not installed", opts.IO.ColorScheme().WarningIcon())
156 return cmdutil.SilentError
157 }
158 } else if !ci.IsCI() {
159 fmt.Fprintf(opts.IO.ErrOut, "%s Copilot CLI not installed", opts.IO.ColorScheme().WarningIcon())
160 return cmdutil.SilentError
161 }
162
163 httpClient, err := opts.HttpClient()
164 if err != nil {
165 return err
166 }
167
168 copilotPath, err = downloadCopilot(httpClient, opts.IO, copilotInstallDir(), copilotBinaryPath())
169 if err != nil {
170 return err
171 }
172 }
173
174 externalCmd := exec.Command(copilotPath, opts.CopilotArgs...)
175 externalCmd.Stdin = opts.IO.In
176 externalCmd.Stdout = opts.IO.Out
177 externalCmd.Stderr = opts.IO.ErrOut
178 externalCmd.Env = append(os.Environ(), "COPILOT_GH=true")
179
180 if err := runExternalCmdFunc(externalCmd); err != nil {
181 if exitErr, ok := err.(*exec.ExitError); ok {
182 // We terminate with os.Exit here, preserving the exit code from Copilot CLI,
183 // and also preventing stdio writes by callers up the stack.
184 os.Exit(exitErr.ExitCode())
185 }
186 if foundInPath {
187 // We found a `copilot` binary but exec failed, possibly due to
188 // unusual characters in the path (see https://github.com/cli/cli/issues/13106).
189 // Suggest running copilot directly as a workaround.
190 return fmt.Errorf("%w\nFailed to run '%s', try running `copilot` directly without `gh`.", err, copilotPath)
191 }

Callers 2

NewCmdCopilotFunction · 0.85

Calls 13

IsCIFunction · 0.92
removeCopilotFunction · 0.85
copilotInstallDirFunction · 0.85
downloadCopilotFunction · 0.85
copilotBinaryPathFunction · 0.85
IsStdoutTTYMethod · 0.80
CanPromptMethod · 0.80
WarningIconMethod · 0.80
ColorSchemeMethod · 0.80
CommandMethod · 0.80
ConfirmMethod · 0.65
ExitCodeMethod · 0.65

Tested by 1