diff --git a/.gitignore b/.gitignore index 66fd13c..180e910 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +.DS_Store diff --git a/cmd/scanenv/main.go b/cmd/scanenv/main.go index 57c714a..97f4d21 100644 --- a/cmd/scanenv/main.go +++ b/cmd/scanenv/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" @@ -9,10 +10,13 @@ import ( ) func main() { + app := args.App{} app = app.Import(scan.Args) - err := app.Parse() - if err != nil { + if err := app.Parse(); err != nil { + if errors.Is(err, args.ErrUsageRequested) { + return + } panic(err) } diff --git a/go.mod b/go.mod index fab4ce9..b2f6089 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.16 require ( github.com/matryer/is v1.4.0 - github.com/taybart/args v0.0.0-20210718035434-490aa1694337 + github.com/taybart/args v0.0.2 github.com/taybart/log v1.6.2 ) diff --git a/go.sum b/go.sum index 50db5cb..a38b59c 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,7 @@ github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= -github.com/taybart/args v0.0.0-20210718035434-490aa1694337 h1:hn2L5QAtWn4OD0faFFBMWStzVe5BQVW9gh4p/F3Ma7Y= -github.com/taybart/args v0.0.0-20210718035434-490aa1694337/go.mod h1:l8CZDnKNf9iNMfX7UOSq7BbwpmwOLcJ7A6DehAP6A+g= -github.com/taybart/log v1.4.1 h1:W33bTp9iviJxhohNXkU7Og7n63D2zKufP8hn2nO5Jfk= -github.com/taybart/log v1.4.1/go.mod h1:zG3tAVOXRh0zQfyxs0dTqarj1hTKFOUWk/oKeiugmZA= +github.com/taybart/args v0.0.2 h1:I2uGSfUse9lqoFy/1dC/kRuwZ/u83KKSosM3staYmAs= +github.com/taybart/args v0.0.2/go.mod h1:aZ8Gg0AdpDyO3BjBfPEiwkIHu/gV/k1Iz4FDJtytGIw= +github.com/taybart/log v1.5.1/go.mod h1:zG3tAVOXRh0zQfyxs0dTqarj1hTKFOUWk/oKeiugmZA= github.com/taybart/log v1.6.2 h1:loVHUm+sG4Xfz2LtXggSL5o8o58GwXWW4QWeYCyJpaY= github.com/taybart/log v1.6.2/go.mod h1:zG3tAVOXRh0zQfyxs0dTqarj1hTKFOUWk/oKeiugmZA= diff --git a/scan/scan.go b/scan/scan.go index baf6c0b..511bd70 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "go/ast" + "go/build" "io" "os" "path/filepath" @@ -26,25 +27,26 @@ var ( Args: map[string]*args.Arg{ "files": { Short: "f", - Long: "files", Help: "Comma seperated files to check (./main.go,./util.go)", }, "directory": { Short: "d", - Long: "directory", Help: "Scan directory", }, "validate": { Short: "v", - Long: "validate", Help: "File to validate env config against", }, "print": { Short: "p", - Long: "print", Help: "Print contents in env file format, will add file tags above each env", Default: false, }, + "tags": { + Short: "t", + Help: "Use go build tags", + Default: "", + }, }, } ) @@ -73,6 +75,16 @@ func Scan(app args.App) error { return e } if re.Match([]byte(path)) { + // check if build tags apply + if app.Get("tags").IsSet() { + ok, err := checkBuildTags(strings.Split(app.Get("tags").String(), ","), path) + if err != nil { + return err + } + if !ok { + return nil + } + } files = append(files, path) } return nil @@ -139,8 +151,8 @@ func Scan(app args.App) error { } - if app.Get("print").IsSet() { - fmt.Println(v.EnvByFile()) + if app.Bool("print") { + fmt.Println("by file", v.EnvByFile()) return nil } fmt.Println(v.ToEnvFile()) @@ -156,3 +168,9 @@ func isPiped() bool { return info.Mode()&os.ModeCharDevice == 0 } + +func checkBuildTags(tags []string, path string) (bool, error) { + context := build.Default + context.BuildTags = tags + return context.MatchFile(filepath.Dir(path), filepath.Base(path)) +} diff --git a/scan/scan_test.go b/scan/scan_test.go index fc1d1f4..5037ad5 100644 --- a/scan/scan_test.go +++ b/scan/scan_test.go @@ -1,7 +1,7 @@ package scan_test import ( - "io/ioutil" + "io" "os" "strings" "testing" @@ -12,7 +12,7 @@ import ( func TestScan(t *testing.T) { is := is.New(t) - os.Args = []string{"./test", "-f", "./test_project/main.go"} + os.Args = []string{"./test", "-d", "./test_project", "-t", "test_tags,other_test"} err := scan.Args.Parse() is.NoErr(err) // hijack stdout @@ -22,8 +22,8 @@ func TestScan(t *testing.T) { err = scan.Scan(scan.Args) is.NoErr(err) w.Close() - out, _ := ioutil.ReadAll(r) - os.Stdout = rescueStdout + out, _ := io.ReadAll(r) // use hijacked output - is.True(strings.Compare(strings.ReplaceAll(string(out), "\n", ""), `ENV=""PORT="6969"`) == 0) + os.Stdout = rescueStdout + is.True(strings.Compare(strings.ReplaceAll(string(out), "\n", ""), `ENV=""PORT="6969"BUILD_TAG=""`) == 0) } diff --git a/scan/test_project/other.go b/scan/test_project/other.go new file mode 100644 index 0000000..390f90a --- /dev/null +++ b/scan/test_project/other.go @@ -0,0 +1,9 @@ +//go:build test_tags && other_test + +package main + +import "github.com/taybart/env" + +func init() { + env.Add([]string{"BUILD_TAG"}) +}