From 6582861a71a24781c14addf0bc3116f980a8455b Mon Sep 17 00:00:00 2001 From: Taylor Bartlett Date: Wed, 2 Apr 2025 13:19:46 -0600 Subject: [PATCH 1/4] feat: add -t or --tags to scanner for build tags --- .gitignore | 1 + cmd/scanenv/main.go | 4 ++-- scan/scan.go | 22 ++++++++++++++++++++++ scan/scan_test.go | 10 +++++----- scan/test_project/other.go | 9 +++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 scan/test_project/other.go 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..b0b21fa 100644 --- a/cmd/scanenv/main.go +++ b/cmd/scanenv/main.go @@ -9,10 +9,10 @@ import ( ) func main() { + app := args.App{} app = app.Import(scan.Args) - err := app.Parse() - if err != nil { + if err := app.Parse(); err != nil { panic(err) } diff --git a/scan/scan.go b/scan/scan.go index baf6c0b..995eaab 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "go/ast" + "go/build" "io" "os" "path/filepath" @@ -45,6 +46,11 @@ var ( 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 +79,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 @@ -156,3 +172,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"}) +} From 3927b362ae0052594ccdf0141160b6c561755161 Mon Sep 17 00:00:00 2001 From: Taylor Bartlett Date: Wed, 2 Apr 2025 13:25:09 -0600 Subject: [PATCH 2/4] fix: bump args version --- cmd/scanenv/main.go | 4 ++++ go.mod | 2 +- go.sum | 7 +++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/scanenv/main.go b/cmd/scanenv/main.go index b0b21fa..97f4d21 100644 --- a/cmd/scanenv/main.go +++ b/cmd/scanenv/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" @@ -13,6 +14,9 @@ func main() { app := args.App{} app = app.Import(scan.Args) 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= From c1177eb4e9e00371d65f1d8c4c94cc67a96dc90e Mon Sep 17 00:00:00 2001 From: Taylor Bartlett Date: Wed, 2 Apr 2025 13:26:27 -0600 Subject: [PATCH 3/4] fix: remove old args option for args package --- scan/scan.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scan/scan.go b/scan/scan.go index 995eaab..3e06901 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -27,22 +27,18 @@ 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, }, From a97be2b0d1fdecde07e920b47209b505185504a5 Mon Sep 17 00:00:00 2001 From: Taylor Bartlett Date: Wed, 2 Apr 2025 13:29:09 -0600 Subject: [PATCH 4/4] fix: print flag check --- scan/scan.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scan/scan.go b/scan/scan.go index 3e06901..511bd70 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -151,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())