Skip to content

Commit

Permalink
add pkg-root flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin Blackman committed Feb 1, 2020
1 parent c8aac04 commit 2890f1b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 23 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The forgotten go tool that executes and caches binaries included in go.mod files

# Convert a JSON object to a Go struct, properly passing in stdin.
echo example.json | gomodrun gojson > example.go

# Specifiy alternative root directory containing a go.mod and tools file.
gomodrun -r ./alternative-tools-dir golangci-lint run
```

## Installation
Expand Down Expand Up @@ -98,11 +101,12 @@ import (
)

func main() {
exitCode, err := gomodrun.Run("golangci-lint", []string{"run"}, gomodrun.Options{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: os.Environ(),
exitCode, err := gomodrun.Run("golangci-lint", []string{"run"}, &gomodrun.Options{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: os.Environ(),
PkgRoot: "",
})
}
```
Expand Down
30 changes: 23 additions & 7 deletions cmd/gomodrun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,36 @@ https://github.com/dustinblackman/gomodrun/commit/%s
The forgotten go tool that executes and caches binaries included in go.mod files.
Usage:
gomodrun cli-name [parameters]
gomodrun [flags] cli-name [parameters]
Example:
gomodrun golangci-lint run
echo example.json | gomodrun gojson > example.go`, version, date, commit)
echo example.json | gomodrun gojson > example.go
gomodrun -r ./alternative-tools-dir golangci-lint run
Flags:
-r, --pkg-root string Specify alternative root directory containing a go.mod and tools file. Defaults to walking up the file tree to locate go.mod.`, version, date, commit)
os.Exit(0)
}

exitCode, err := gomodrun.Run(os.Args[1], os.Args[2:], gomodrun.Options{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: os.Environ(),
cmdPosition := 1
argsPosition := 2
pkgRoot := ""

if os.Args[1] == "-r" || os.Args[1] == "--pkg-root" {
pkgRoot = os.Args[2]
cmdPosition += 2
argsPosition += 2
}

exitCode, err := gomodrun.Run(os.Args[cmdPosition], os.Args[argsPosition:], &gomodrun.Options{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: os.Environ(),
PkgRoot: pkgRoot,
})

if err != nil {
exitWithError(err)
}
Expand Down
23 changes: 15 additions & 8 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (

// Options contains parameters that are passed to `exec.Command` when running the binary.
type Options struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Env []string
Stdin io.Reader // Stdin passed to tool.
Stdout io.Writer // Stdout passed to tool.
Stderr io.Writer // Stderr passed to tool.
Env []string // Array of environment variables passed to tool.
PkgRoot string // Root directory of go.mod with tools
}

// GetPkgRoot gets your projects package root, allowing you to run gomodrun from any sub directory.
Expand All @@ -31,6 +32,7 @@ func GetPkgRoot() (string, error) {
if err != nil {
return "", err
}

for {
if _, err := os.Stat(path.Join(currentDir, "go.mod")); !os.IsNotExist(err) {
absPath, err := filepath.Abs(currentDir)
Expand Down Expand Up @@ -170,10 +172,15 @@ func GetCachedBin(pkgRoot, binName, cmdPath string) (string, error) {
}

// Run executes your binary.
func Run(binName string, args []string, options Options) (int, error) {
pkgRoot, err := GetPkgRoot()
if err != nil {
return -1, err
func Run(binName string, args []string, options *Options) (int, error) {
var err error
pkgRoot := options.PkgRoot

if pkgRoot == "" {
pkgRoot, err = GetPkgRoot()
if err != nil {
return -1, err
}
}

cmdPath, err := GetCommandVersionedPkgPath(pkgRoot, binName)
Expand Down
30 changes: 27 additions & 3 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,45 @@ var _ = Describe("pkg", func() {

Context("Run", func() {
It("should return exit code 0 when binary exits with 0", func() {
exitCode, err := gomodrun.Run("hello-world", []string{}, gomodrun.Options{})
exitCode, err := gomodrun.Run("hello-world", []string{}, &gomodrun.Options{})
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(0))
})

It("should return exit code 1 when the binary exists with 1", func() {
exitCode, err := gomodrun.Run("hello-world", []string{"1"}, gomodrun.Options{})
exitCode, err := gomodrun.Run("hello-world", []string{"1"}, &gomodrun.Options{})
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(1))
})

It("should return an error when the binary does not exist", func() {
exitCode, err := gomodrun.Run("not-real", []string{}, gomodrun.Options{})
exitCode, err := gomodrun.Run("not-real", []string{}, &gomodrun.Options{})
Expect(err).ToNot(BeNil())
Expect(exitCode).To(Equal(-1))
})

Context("Alternative tools directory", func() {
options := &gomodrun.Options{
PkgRoot: path.Join(cwd, "./tests/alternative-tools-dir"),
}

It("should return exit code 0 when binary exits with 0", func() {
exitCode, err := gomodrun.Run("hello-world", []string{}, options)
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(0))
})

It("should return exit code 1 when the binary exists with 1", func() {
exitCode, err := gomodrun.Run("hello-world", []string{"1"}, options)
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(1))
})

It("should return an error when the binary does not exist", func() {
exitCode, err := gomodrun.Run("not-real", []string{}, options)
Expect(err).ToNot(BeNil())
Expect(exitCode).To(Equal(-1))
})
})
})
})
5 changes: 5 additions & 0 deletions tests/alternative-tools-dir/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/dustinblackman/gomodrun-test

go 1.13

require github.com/dustinblackman/go-hello-world-test v0.0.2
2 changes: 2 additions & 0 deletions tests/alternative-tools-dir/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/dustinblackman/go-hello-world-test v0.0.2 h1:DcAbKiyeohJ/c/3m5c7h3tQGKA8q7J9eahZpAjo3ZZs=
github.com/dustinblackman/go-hello-world-test v0.0.2/go.mod h1:wbYSnWUoM4tqbraqvRvVuK6jV7YV4Gv+d/lTltepZyA=
7 changes: 7 additions & 0 deletions tests/alternative-tools-dir/go.tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package gomodrun

import (
_ "github.com/dustinblackman/go-hello-world-test/hello-world"
)

0 comments on commit 2890f1b

Please sign in to comment.