Skip to content

Conversation

@gabstv
Copy link

@gabstv gabstv commented Jun 19, 2019

Execute commands on a different path than the working directory:

import ( "github.com/bitfield/script" ) func main(){ // proj version based on last git tag v, _ := script.ExecAt("/Users/me/some-project", "git describe --tags --dirty").String() println("some-project version: " + v) }
@gabstv gabstv changed the title ExecAt Add ExecAt Jun 19, 2019
@bitfield
Copy link
Owner

Great PR, thanks!

It occurred to me looking at this that maybe this is a better API:

script.Echo("/Users/me/some-project").ExecAt("git describe --tags --dirty")

This way, you could run the same command on multiple directories at once, and those directories could come from a pipe, or from command-line arguments. What do you think?

args := strings.Fields(s)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = p.Reader
cmd.Dir = dir
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have both Exec and ExecAt use the same basic machinery? It would be tiresome to have to fix a bug in multiple places, for example.

}
want := "Usage"
p := ExecAt(wdir, "go")
if p.Error() == nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're really just testing Exec here, aren't we—this is a straight copy of TestExec, but with the working directory parameter added. Since we already know Exec works, from its own test, and since ExecAt should use Exec (see previous comment), this test only needs to test that the command runs in the specified working directory (and also to test the behaviour in various edge cases, such as 'directory doesn't exist', 'permission denied', and so on).

@bitfield
Copy link
Owner

bitfield commented Jul 2, 2019

Hey @gabstv—are you still interested in working on this? If not, that's fine, but would you mind if I picked up your PR and finished off the changes requested here?

@bitfield
Copy link
Owner

Closing as no response from OP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants