@@ -13,45 +13,19 @@ import (
13
13
"sort"
14
14
"strings"
15
15
16
- "github.com/taybart/args"
17
16
"github.com/taybart/env"
18
17
"github.com/taybart/log"
19
18
)
20
19
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
+ }
53
27
54
- func Scan (app args. App ) error {
28
+ func Scan (config Config ) ( string , error ) {
55
29
files := []string {}
56
30
57
31
switch {
@@ -67,17 +41,16 @@ func Scan(app args.App) error {
67
41
}
68
42
files = strings .Split (string (fns ), "\n " )
69
43
70
- case app . Get ( "directory" ). IsSet () : // directory specified
44
+ case config . Dir != "" : // directory specified
71
45
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 {
74
47
if e != nil {
75
48
return e
76
49
}
77
50
if re .Match ([]byte (path )) {
78
51
// 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 )
81
54
if err != nil {
82
55
return err
83
56
}
@@ -90,13 +63,13 @@ func Scan(app args.App) error {
90
63
return nil
91
64
})
92
65
if err != nil {
93
- return err
66
+ return "" , err
94
67
}
95
68
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 , "," )
98
71
default :
99
- return errors .New ("no files specified" )
72
+ return "" , errors .New ("no files specified" )
100
73
}
101
74
102
75
// Get down to buisness
@@ -108,13 +81,13 @@ func Scan(app args.App) error {
108
81
}
109
82
ast .Inspect (node , v .Visit )
110
83
}
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 )
113
86
foundEnv , optional := v .EnvToMap ()
114
87
115
- envToTest , err := parseEnvFile (app . String ( "validate" ) )
88
+ envToTest , err := parseEnvFile (config . Validate )
116
89
if err != nil {
117
- return err
90
+ return "" , err
118
91
}
119
92
missing := []string {}
120
93
usingDefault := []string {}
@@ -147,16 +120,14 @@ func Scan(app args.App) error {
147
120
log .Warnf ("Using default value for %s=%s\n " , k , strings .Trim (d , `"` ))
148
121
}
149
122
}
150
- return nil
123
+ return "" , nil
151
124
152
125
}
153
126
154
- if app .Bool ("print" ) {
155
- fmt .Println (v .EnvByFile ())
156
- return nil
127
+ if config .PrintFiles {
128
+ return v .EnvByFile (), nil
157
129
}
158
- fmt .Println (v .ToEnvFile ())
159
- return nil
130
+ return v .ToEnvFile (), nil
160
131
}
161
132
162
133
// Check if program has data piped to it
0 commit comments