Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.DS_Store
8 changes: 6 additions & 2 deletions cmd/scanenv/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
30 changes: 24 additions & 6 deletions scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"go/ast"
"go/build"
"io"
"os"
"path/filepath"
Expand All @@ -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: "",
},
},
}
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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))
}
10 changes: 5 additions & 5 deletions scan/scan_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scan_test

import (
"io/ioutil"
"io"
"os"
"strings"
"testing"
Expand All @@ -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
Expand All @@ -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)
}
9 changes: 9 additions & 0 deletions scan/test_project/other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build test_tags && other_test

package main

import "github.com/taybart/env"

func init() {
env.Add([]string{"BUILD_TAG"})
}
Loading