1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Package gh is a library for CLI Go applications to help interface with the gh CLI tool,
// and the GitHub API.
//
// Note that the examples in this package assume gh and git are installed. They do not run in
// the Go Playground used by pkg.go.dev.
package gh
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"github.com/cli/safeexec"
)
// Exec invokes a gh command in a subprocess and captures the output and error streams.
func Exec(args ...string) (stdout, stderr bytes.Buffer, err error) {
ghExe, err := Path()
if err != nil {
return
}
err = run(context.Background(), ghExe, nil, nil, &stdout, &stderr, args)
return
}
// ExecContext invokes a gh command in a subprocess and captures the output and error streams.
func ExecContext(ctx context.Context, args ...string) (stdout, stderr bytes.Buffer, err error) {
ghExe, err := Path()
if err != nil {
return
}
err = run(ctx, ghExe, nil, nil, &stdout, &stderr, args)
return
}
// Exec invokes a gh command in a subprocess with its stdin, stdout, and stderr streams connected to
// those of the parent process. This is suitable for running gh commands with interactive prompts.
func ExecInteractive(ctx context.Context, args ...string) error {
ghExe, err := Path()
if err != nil {
return err
}
return run(ctx, ghExe, nil, os.Stdin, os.Stdout, os.Stderr, args)
}
// Path searches for an executable named "gh" in the directories named by the PATH environment variable.
// If the executable is found the result is an absolute path.
func Path() (string, error) {
if ghExe := os.Getenv("GH_PATH"); ghExe != "" {
return ghExe, nil
}
return safeexec.LookPath("gh")
}
func run(ctx context.Context, ghExe string, env []string, stdin io.Reader, stdout, stderr io.Writer, args []string) error {
cmd := exec.CommandContext(ctx, ghExe, args...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
if env != nil {
cmd.Env = env
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("gh execution failed: %w", err)
}
return nil
}
|