cli / go-gh

A Go module for interacting with gh and the GitHub API from the command line.
https://pkg.go.dev/github.com/cli/go-gh/v2
MIT License
322 stars 45 forks source link

Expose `ghLookupPath` or a better way to run `gh` #121

Closed josebalius closed 11 months ago

josebalius commented 12 months ago

Been using go-gh for a few things and liking it so far!

I have a use case where I want to run gh as a long running command and I want to take over the writer for stdout and stderr. With the current APIs, I can't do this.

See example of how I am doing it right now by directly invoking gh: https://github.com/josebalius/gh-csfs/blob/main/internal/csfs/ssh.go#L37-L56

This solution works perfectly, but I am hardcoding gh, so it would be nice to either expose the lookup path function or a way to inject writers.

josebalius commented 12 months ago

Something like this would work:

func ExecInteractiveWithStreams(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, args ...string) error {
    ghExe, err := ghLookPath()
    if err != nil {
        return err
    }
    return run(ctx, ghExe, nil, stdin, stdout, stderr, args)
}

Happy to open up a PR

samcoe commented 12 months ago

@josebalius This seems like a workflow that we should be able to support. I would rather expose a top level Path() function that returns the local path to gh rather than adding another Exec command. There are already three of them and adding another one feels like it would create confusion.

Please feel free to open up a PR adding this functionality, should be as simple as renaming ghLookPath() to Path() and adding a Godoc comment.

josebalius commented 11 months ago

I was about to open: https://github.com/cli/go-gh/compare/trunk...josebalius:go-gh:jg/export-lookpath?expand=1 but noticed you had a PR already ❤️ - nice!