Skip to content

Commit 026e976

Browse files
stemar94mislav
andauthoredApr 5, 2023
feat: add gh.ExecInteractive (#115)
- add gh.ExecInteractive - add gh.ExecContext - add support for GH_PATH environment variable Co-authored-by: Mislav Marohnić <[email protected]>
1 parent fadea2b commit 026e976

File tree

3 files changed

+80
-27
lines changed

3 files changed

+80
-27
lines changed
 

‎gh.go

+39-15
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package gh
77

88
import (
99
"bytes"
10+
"context"
1011
"errors"
1112
"fmt"
13+
"io"
1214
"net/http"
1315
"os"
1416
"os/exec"
@@ -24,33 +26,55 @@ import (
2426
"github.com/cli/safeexec"
2527
)
2628

27-
// Exec gh command with provided arguments.
28-
func Exec(args ...string) (stdOut, stdErr bytes.Buffer, err error) {
29-
path, err := path()
29+
// Exec invokes a gh command in a subprocess and captures the output and error streams.
30+
func Exec(args ...string) (stdout, stderr bytes.Buffer, err error) {
31+
ghExe, err := ghLookPath()
3032
if err != nil {
31-
err = fmt.Errorf("could not find gh executable in PATH. error: %w", err)
3233
return
3334
}
34-
return run(path, nil, args...)
35+
err = run(context.Background(), ghExe, nil, nil, &stdout, &stderr, args)
36+
return
37+
}
38+
39+
// ExecContext invokes a gh command in a subprocess and captures the output and error streams.
40+
func ExecContext(ctx context.Context, args ...string) (stdout, stderr bytes.Buffer, err error) {
41+
ghExe, err := ghLookPath()
42+
if err != nil {
43+
return
44+
}
45+
err = run(ctx, ghExe, nil, nil, &stdout, &stderr, args)
46+
return
47+
}
48+
49+
// Exec invokes a gh command in a subprocess with its stdin, stdout, and stderr streams connected to
50+
// those of the parent process. This is suitable for running gh commands with interactive prompts.
51+
func ExecInteractive(ctx context.Context, args ...string) error {
52+
ghExe, err := ghLookPath()
53+
if err != nil {
54+
return err
55+
}
56+
return run(ctx, ghExe, nil, os.Stdin, os.Stdout, os.Stderr, args)
3557
}
3658

37-
func path() (string, error) {
59+
func ghLookPath() (string, error) {
60+
if ghExe := os.Getenv("GH_PATH"); ghExe != "" {
61+
return ghExe, nil
62+
}
3863
return safeexec.LookPath("gh")
3964
}
4065

41-
func run(path string, env []string, args ...string) (stdOut, stdErr bytes.Buffer, err error) {
42-
cmd := exec.Command(path, args...)
43-
cmd.Stdout = &stdOut
44-
cmd.Stderr = &stdErr
66+
func run(ctx context.Context, ghExe string, env []string, stdin io.Reader, stdout, stderr io.Writer, args []string) error {
67+
cmd := exec.CommandContext(ctx, ghExe, args...)
68+
cmd.Stdin = stdin
69+
cmd.Stdout = stdout
70+
cmd.Stderr = stderr
4571
if env != nil {
4672
cmd.Env = env
4773
}
48-
err = cmd.Run()
49-
if err != nil {
50-
err = fmt.Errorf("failed to run gh: %s. error: %w", stdErr.String(), err)
51-
return
74+
if err := cmd.Run(); err != nil {
75+
return fmt.Errorf("gh execution failed: %w", err)
5276
}
53-
return
77+
return nil
5478
}
5579

5680
// RESTClient builds a client to send requests to GitHub REST API endpoints.

‎gh_test.go

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package gh
22

33
import (
4+
"bytes"
5+
"context"
46
"fmt"
57
"net/http"
68
"os"
79
"strings"
810
"testing"
11+
"time"
912

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

36+
func TestHelperProcessLongRunning(t *testing.T) {
37+
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
38+
return
39+
}
40+
args := os.Args[3:]
41+
fmt.Fprintf(os.Stdout, "%v", args)
42+
fmt.Fprint(os.Stderr, "going to sleep...")
43+
time.Sleep(10 * time.Second)
44+
fmt.Fprint(os.Stderr, "...going to exit")
45+
os.Exit(0)
46+
}
47+
3348
func TestRun(t *testing.T) {
34-
stdOut, stdErr, err := run(os.Args[0],
35-
[]string{"GH_WANT_HELPER_PROCESS=1"},
36-
"-test.run=TestHelperProcess", "--", "gh", "issue", "list")
49+
var stdout, stderr bytes.Buffer
50+
err := run(context.TODO(), os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, &stdout, &stderr,
51+
[]string{"-test.run=TestHelperProcess", "--", "gh", "issue", "list"})
3752
assert.NoError(t, err)
38-
assert.Equal(t, "[gh issue list]", stdOut.String())
39-
assert.Equal(t, "", stdErr.String())
53+
assert.Equal(t, "[gh issue list]", stdout.String())
54+
assert.Equal(t, "", stderr.String())
4055
}
4156

4257
func TestRunError(t *testing.T) {
43-
stdOut, stdErr, err := run(os.Args[0],
44-
[]string{"GH_WANT_HELPER_PROCESS=1"},
45-
"-test.run=TestHelperProcess", "--", "gh", "issue", "list", "error")
46-
assert.EqualError(t, err, "failed to run gh: process exited with error. error: exit status 1")
47-
assert.Equal(t, "", stdOut.String())
48-
assert.Equal(t, "process exited with error", stdErr.String())
58+
var stdout, stderr bytes.Buffer
59+
err := run(context.TODO(), os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, &stdout, &stderr,
60+
[]string{"-test.run=TestHelperProcess", "--", "gh", "error"})
61+
assert.EqualError(t, err, "gh execution failed: exit status 1")
62+
assert.Equal(t, "", stdout.String())
63+
assert.Equal(t, "process exited with error", stderr.String())
64+
}
65+
66+
func TestRunInteractiveContextCanceled(t *testing.T) {
67+
// pass current time to ensure that deadline has already passed
68+
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
69+
cancel()
70+
err := run(ctx, os.Args[0], []string{"GH_WANT_HELPER_PROCESS=1"}, nil, nil, nil,
71+
[]string{"-test.run=TestHelperProcessLongRunning", "--", "gh", "issue", "list"})
72+
assert.EqualError(t, err, "gh execution failed: context deadline exceeded")
4973
}
5074

5175
func TestRESTClient(t *testing.T) {

‎pkg/auth/auth.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ func TokenForHost(host string) (string, string) {
3737
return token, source
3838
}
3939

40-
if ghExe, err := safeexec.LookPath("gh"); err == nil {
40+
ghExe := os.Getenv("GH_PATH")
41+
if ghExe == "" {
42+
ghExe, _ = safeexec.LookPath("gh")
43+
}
44+
45+
if ghExe != "" {
4146
if token, source := tokenFromGh(ghExe, host); token != "" {
4247
return token, source
4348
}

0 commit comments

Comments
 (0)
Please sign in to comment.