-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an interface for executing Bazel commands
This makes it easier to plug the Target Determinator into larger applications that already have tight control over how Bazel is executed (eg. when using this as a plugin of Aspect's CLI tool).
- Loading branch information
Showing
5 changed files
with
91 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package pkg | ||
|
||
import ( | ||
"io" | ||
"os/exec" | ||
) | ||
|
||
type BazelCmdConfig struct { | ||
// Dir represents the working directory to use for the command. | ||
// If Dir is the empty string, use the calling process's current directory. | ||
Dir string | ||
|
||
// Stdout and Stderr specify the process's standard output and error. | ||
// A nil value redirects the output to /dev/null. | ||
// The behavior is the same as the exec.Command struct. | ||
Stdout io.Writer | ||
Stderr io.Writer | ||
} | ||
|
||
type BazelCmd interface { | ||
Execute(config BazelCmdConfig, args ...string) (int, error) | ||
} | ||
|
||
type DefaultBazelCmd struct { | ||
BazelPath string | ||
} | ||
|
||
// Execute calls bazel with the provided arguments. | ||
// It returns the exit status code or -1 if it errored before the process could start. | ||
func (c DefaultBazelCmd) Execute(config BazelCmdConfig, args ...string) (int, error) { | ||
cmd := exec.Command(c.BazelPath, args...) | ||
cmd.Dir = config.Dir | ||
cmd.Stdout = config.Stdout | ||
cmd.Stderr = config.Stderr | ||
|
||
if err := cmd.Run(); err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
return exitError.ExitCode(), err | ||
} else { | ||
return -1, err | ||
} | ||
} | ||
return 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters