-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydrate_varlink.sh
executable file
·249 lines (192 loc) · 5.17 KB
/
hydrate_varlink.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Assuming you're in the project root directory
# 1. Update internal packages
cat << EOF > internal/env/env.go
package env
import (
"fmt"
"os"
"strings"
)
func Deactivate() {
for _, env := range os.Environ() {
if strings.HasPrefix(env, "TF_VAR_") {
parts := strings.SplitN(env, "=", 2)
os.Unsetenv(parts[0])
}
}
fmt.Println("Terraform environment variables have been deactivated.")
}
func SetVars(vars map[string]string) {
for k, v := range vars {
os.Setenv(fmt.Sprintf("TF_VAR_%s", k), v)
}
}
EOF
cat << EOF > internal/tfvars/parser.go
package tfvars
import (
"bufio"
"fmt"
"os"
"strings"
)
func ParseFiles(files []string) (map[string]string, error) {
vars := make(map[string]string)
for _, file := range files {
fileVars, err := parseFile(file)
if err != nil {
return nil, fmt.Errorf("error parsing file %s: %w", file, err)
}
for k, v := range fileVars {
vars[k] = v
}
}
return vars, nil
}
func parseFile(file string) (map[string]string, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
vars := make(map[string]string)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
value = strings.Trim(value, "\"")
vars[key] = value
}
if err := scanner.Err(); err != nil {
return nil, err
}
return vars, nil
}
EOF
# 2. Update cmd/varlink/main.go
# (Copy the full implementation we discussed earlier)
# 3. Update internal/config/config.go
cat << EOF > internal/config/config.go
package config
import (
"flag"
"os"
"strconv"
)
type Config struct {
LevelsAbove int
MaxSearchDepth int
Deactivate bool
DryRun bool
}
func Parse() Config {
cfg := Config{
LevelsAbove: 1,
MaxSearchDepth: 10,
}
flag.IntVar(&cfg.LevelsAbove, "levels", cfg.LevelsAbove, "Levels above 'environments' to search for tfvars")
flag.IntVar(&cfg.MaxSearchDepth, "max-depth", cfg.MaxSearchDepth, "Maximum directory depth to search")
flag.BoolVar(&cfg.Deactivate, "deactivate", false, "Deactivate Terraform environment variables")
flag.BoolVar(&cfg.DryRun, "dry-run", false, "Show what would be done without making changes")
flag.Parse()
if envLevels := os.Getenv("VARLINK_LEVELS_ABOVE"); envLevels != "" {
if l, err := strconv.Atoi(envLevels); err == nil {
cfg.LevelsAbove = l
}
}
if envMaxDepth := os.Getenv("VARLINK_MAX_SEARCH_DEPTH"); envMaxDepth != "" {
if d, err := strconv.Atoi(envMaxDepth); err == nil {
cfg.MaxSearchDepth = d
}
}
return cfg
}
EOF
# 4. Add unit tests (create empty files for now)
touch internal/config/config_test.go
touch internal/env/env_test.go
touch internal/tfvars/parser_test.go
# 5. Update Makefile
cat << EOF >> Makefile
lint:
golangci-lint run
fmt:
go fmt ./...
test-structure:
./create_test_structure.sh
EOF
# 6. Update README.md (you may want to edit this manually with more details)
cat << EOF > README.md
# Varlink
Varlink is a tool for managing Terraform variables and environments. It simplifies the process of setting up Terraform environment variables based on .tfvars files in your project structure.
## Features
- Automatically finds and parses .tfvars files
- Sets Terraform environment variables (TF_VAR_*)
- Supports multiple environments (dev, staging, prod, etc.)
- Allows specifying search depth for .tfvars files
- Provides a dry-run mode to preview actions
- Can deactivate all Terraform-related environment variables
## Usage
\`\`\`
varlink [flags]
Flags:
-levels int
Levels above 'environments' to search for tfvars (default 1)
-max-depth int
Maximum directory depth to search (default 10)
-deactivate
Deactivate Terraform environment variables
-dry-run
Show what would be done without making changes
\`\`\`
## Building
To build the project, run:
\`\`\`
make build
\`\`\`
This will create the \`varlink\` binary in the \`bin\` directory.
## Testing
To run tests, use:
\`\`\`
make test
\`\`\`
To create a test directory structure:
\`\`\`
make test-structure
\`\`\`
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
EOF
# 7. Create .github directory and add a basic workflow
mkdir -p .github/workflows
cat << EOF > .github/workflows/go.yml
name: Go
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
EOF
echo "Additional setup for varlink project completed."