Skip to content

Commit

Permalink
feat: add gh.ExecInteractive (#115)
Browse files Browse the repository at this point in the history
- add gh.ExecInteractive
- add gh.ExecContext
- add support for GH_PATH environment variable

Co-authored-by: Mislav Marohnić <git@mislav.net>
  • Loading branch information
stemar94 and mislav committed Apr 5, 2023
1 parent fadea2b commit 026e976
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 deletions.
54 changes: 39 additions & 15 deletions gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package gh

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
Expand All @@ -24,33 +26,55 @@ import (
"github.com/cli/safeexec"
)

// Exec gh command with provided arguments.
func Exec(args ...string) (stdOut, stdErr bytes.Buffer, err error) {
path, err := path()
// 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 := ghLookPath()
if err != nil {
err = fmt.Errorf("could not find gh executable in PATH. error: %w", err)
return
}
return run(path, nil, args...)
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 := ghLookPath()
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 := ghLookPath()
if err != nil {
return err
}
return run(ctx, ghExe, nil, os.Stdin, os.Stdout, os.Stderr, args)
}

func path() (string, error) {
func ghLookPath() (string, error) {
if ghExe := os.Getenv("GH_PATH"); ghExe != "" {
return ghExe, nil
}
return safeexec.LookPath("gh")
}

func run(path string, env []string, args ...string) (stdOut, stdErr bytes.Buffer, err error) {
cmd := exec.Command(path, args...)
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
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
}
err = cmd.Run()
if err != nil {
err = fmt.Errorf("failed to run gh: %s. error: %w", stdErr.String(), err)
return
if err := cmd.Run(); err != nil {
return fmt.Errorf("gh execution failed: %w", err)
}
return
return nil
}

// RESTClient builds a client to send requests to GitHub REST API endpoints.
Expand Down
46 changes: 35 additions & 11 deletions gh_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package gh

import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"

"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/config"
Expand All @@ -30,22 +33,43 @@ func TestHelperProcess(t *testing.T) {
os.Exit(0)
}

func TestHelperProcessLongRunning(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
args := os.Args[3:]
fmt.Fprintf(os.Stdout, "%v", args)
fmt.Fprint(os.Stderr, "going to sleep...")
time.Sleep(10 * time.Second)
fmt.Fprint(os.Stderr, "...going to exit")
os.Exit(0)
}

func TestRun(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list")
var stdout, stderr bytes.Buffer
err := run(context.TODO(), os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, &stdout, &stderr,
[]string{"-test.run=TestHelperProcess", "--", "gh", "issue", "list"})
assert.NoError(t, err)
assert.Equal(t, "[gh issue list]", stdOut.String())
assert.Equal(t, "", stdErr.String())
assert.Equal(t, "[gh issue list]", stdout.String())
assert.Equal(t, "", stderr.String())
}

func TestRunError(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list", "error")
assert.EqualError(t, err, "failed to run gh: process exited with error. error: exit status 1")
assert.Equal(t, "", stdOut.String())
assert.Equal(t, "process exited with error", stdErr.String())
var stdout, stderr bytes.Buffer
err := run(context.TODO(), os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, &stdout, &stderr,
[]string{"-test.run=TestHelperProcess", "--", "gh", "error"})
assert.EqualError(t, err, "gh execution failed: exit status 1")
assert.Equal(t, "", stdout.String())
assert.Equal(t, "process exited with error", stderr.String())
}

func TestRunInteractiveContextCanceled(t *testing.T) {
// pass current time to ensure that deadline has already passed
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
cancel()
err := run(ctx, os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, nil, nil,
[]string{"-test.run=TestHelperProcessLongRunning", "--", "gh", "issue", "list"})
assert.EqualError(t, err, "gh execution failed: context deadline exceeded")
}

func TestRESTClient(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func TokenForHost(host string) (string, string) {
return token, source
}

if ghExe, err := safeexec.LookPath("gh"); err == nil {
ghExe := os.Getenv("GH_PATH")
if ghExe == "" {
ghExe, _ = safeexec.LookPath("gh")
}

if ghExe != "" {
if token, source := tokenFromGh(ghExe, host); token != "" {
return token, source
}
Expand Down

0 comments on commit 026e976

Please sign in to comment.