Skip to content

Commit 7b6d52b

Browse files
committed
fix: move args to cmd
1 parent 5b5dc42 commit 7b6d52b

File tree

3 files changed

+70
-72
lines changed

3 files changed

+70
-72
lines changed

cmd/scanenv/main.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,56 @@ import (
99
"github.com/taybart/env/scan"
1010
)
1111

12-
func main() {
12+
var (
13+
app = args.App{
14+
Name: "scanenv",
15+
Version: "v0.0.1",
16+
Author: "Taylor Bartlett <[email protected]>",
17+
About: "check for defined env vars in a project or file",
18+
Args: map[string]*args.Arg{
19+
"files": {
20+
Short: "f",
21+
Help: "Comma seperated files to check (./main.go,./util.go)",
22+
},
23+
"directory": {
24+
Short: "d",
25+
Help: "Scan directory",
26+
},
27+
"validate": {
28+
Short: "v",
29+
Help: "File to validate env config against",
30+
},
31+
"print": {
32+
Short: "p",
33+
Help: "Print contents in env file format, will add file tags above each env",
34+
Default: false,
35+
},
36+
"tags": {
37+
Short: "t",
38+
Help: "Use go build tags",
39+
Default: "",
40+
},
41+
},
42+
}
43+
)
1344

14-
app := args.App{}
15-
app = app.Import(scan.Args)
45+
func main() {
1646
if err := app.Parse(); err != nil {
1747
if errors.Is(err, args.ErrUsageRequested) {
1848
return
1949
}
2050
panic(err)
2151
}
2252

23-
if err := scan.Scan(app); err != nil {
53+
var config scan.Config
54+
if err := app.Marshal(&config); err != nil {
55+
panic(err)
56+
}
57+
58+
res, err := scan.Scan(config)
59+
if err != nil {
2460
fmt.Println(err)
2561
os.Exit(1)
2662
}
63+
fmt.Println(res)
2764
}

scan/scan.go

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,19 @@ import (
1313
"sort"
1414
"strings"
1515

16-
"github.com/taybart/args"
1716
"github.com/taybart/env"
1817
"github.com/taybart/log"
1918
)
2019

21-
var (
22-
Args = args.App{
23-
Name: "scanenv",
24-
Version: "v0.0.1",
25-
Author: "Taylor Bartlett <[email protected]>",
26-
About: "check for defined env vars in a project or file",
27-
Args: map[string]*args.Arg{
28-
"files": {
29-
Short: "f",
30-
Help: "Comma seperated files to check (./main.go,./util.go)",
31-
},
32-
"directory": {
33-
Short: "d",
34-
Help: "Scan directory",
35-
},
36-
"validate": {
37-
Short: "v",
38-
Help: "File to validate env config against",
39-
},
40-
"print": {
41-
Short: "p",
42-
Help: "Print contents in env file format, will add file tags above each env",
43-
Default: false,
44-
},
45-
"tags": {
46-
Short: "t",
47-
Help: "Use go build tags",
48-
Default: "",
49-
},
50-
},
51-
}
52-
)
20+
type Config struct {
21+
Dir string `arg:"directory"`
22+
Files string `arg:"files"`
23+
Tags string `arg:"tags"`
24+
PrintFiles bool `arg:"print"`
25+
Validate string `arg:"validate"`
26+
}
5327

54-
func Scan(app args.App) error {
28+
func Scan(config Config) (string, error) {
5529
files := []string{}
5630

5731
switch {
@@ -67,17 +41,16 @@ func Scan(app args.App) error {
6741
}
6842
files = strings.Split(string(fns), "\n")
6943

70-
case app.Get("directory").IsSet(): // directory specified
44+
case config.Dir != "": // directory specified
7145
re := regexp.MustCompile(`[[:alnum:]\/\._\-]+.go$`)
72-
dir := app.String("directory")
73-
err := filepath.Walk(dir, func(path string, _ os.FileInfo, e error) error {
46+
err := filepath.Walk(config.Dir, func(path string, _ os.FileInfo, e error) error {
7447
if e != nil {
7548
return e
7649
}
7750
if re.Match([]byte(path)) {
7851
// check if build tags apply
79-
if app.Get("tags").IsSet() {
80-
ok, err := checkBuildTags(strings.Split(app.Get("tags").String(), ","), path)
52+
if config.Tags != "" {
53+
ok, err := checkBuildTags(strings.Split(config.Tags, ","), path)
8154
if err != nil {
8255
return err
8356
}
@@ -90,13 +63,13 @@ func Scan(app args.App) error {
9063
return nil
9164
})
9265
if err != nil {
93-
return err
66+
return "", err
9467
}
9568

96-
case app.Get("files").IsSet(): // csv of files
97-
files = strings.Split(app.String("files"), ",")
69+
case config.Files != "": // csv of files
70+
files = strings.Split(config.Files, ",")
9871
default:
99-
return errors.New("no files specified")
72+
return "", errors.New("no files specified")
10073
}
10174

10275
// Get down to buisness
@@ -108,13 +81,13 @@ func Scan(app args.App) error {
10881
}
10982
ast.Inspect(node, v.Visit)
11083
}
111-
if app.Get("validate").IsSet() {
112-
log.Debug("Should Validate", app.String("validate"))
84+
if config.Validate != "" {
85+
log.Debug("Should Validate", config.Validate)
11386
foundEnv, optional := v.EnvToMap()
11487

115-
envToTest, err := parseEnvFile(app.String("validate"))
88+
envToTest, err := parseEnvFile(config.Validate)
11689
if err != nil {
117-
return err
90+
return "", err
11891
}
11992
missing := []string{}
12093
usingDefault := []string{}
@@ -147,16 +120,14 @@ func Scan(app args.App) error {
147120
log.Warnf("Using default value for %s=%s\n", k, strings.Trim(d, `"`))
148121
}
149122
}
150-
return nil
123+
return "", nil
151124

152125
}
153126

154-
if app.Bool("print") {
155-
fmt.Println(v.EnvByFile())
156-
return nil
127+
if config.PrintFiles {
128+
return v.EnvByFile(), nil
157129
}
158-
fmt.Println(v.ToEnvFile())
159-
return nil
130+
return v.ToEnvFile(), nil
160131
}
161132

162133
// Check if program has data piped to it

scan/scan_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package scan_test
22

33
import (
4-
"io"
5-
"os"
64
"strings"
75
"testing"
86

@@ -12,18 +10,10 @@ import (
1210

1311
func TestScan(t *testing.T) {
1412
is := is.New(t)
15-
os.Args = []string{"./test", "-d", "./test_project", "-t", "test_tags,other_test"}
16-
err := scan.Args.Parse()
13+
res, err := scan.Scan(scan.Config{
14+
Dir: "./test_project/",
15+
Tags: "test_tags,other_test",
16+
})
1717
is.NoErr(err)
18-
// hijack stdout
19-
rescueStdout := os.Stdout
20-
r, w, _ := os.Pipe()
21-
os.Stdout = w
22-
err = scan.Scan(scan.Args)
23-
is.NoErr(err)
24-
w.Close()
25-
out, _ := io.ReadAll(r)
26-
// use hijacked output
27-
os.Stdout = rescueStdout
28-
is.True(strings.Compare(strings.ReplaceAll(string(out), "\n", ""), `ENV=""PORT="6969"BUILD_TAG=""`) == 0)
18+
is.True(strings.Compare(strings.ReplaceAll(res, "\n", ""), `ENV=""PORT="6969"BUILD_TAG=""`) == 0)
2919
}

0 commit comments

Comments
 (0)