-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8d63118
Showing
14 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/bin/ | ||
*.exe | ||
*.test | ||
*.out | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Your Name | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.PHONY: build test clean | ||
|
||
build: | ||
go build -o bin/varlink cmd/varlink/main.go | ||
|
||
test: | ||
go test ./... | ||
|
||
clean: | ||
rm -f bin/varlink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 | ||
``` | ||
## Assumed directory structure | ||
|
||
├── environments | ||
│ ├── dev | ||
│ │ ├── dev.tfvars | ||
│ │ ├── service1 | ||
│ │ │ ├── main.tf | ||
│ │ │ └── service1.tfvars | ||
│ │ └── service2 | ||
│ │ ├── main.tf | ||
│ │ └── service2.tfvars | ||
│ ├── staging | ||
│ │ ├── main.tf | ||
│ │ └── staging.tfvars | ||
│ └── prod | ||
│ ├── main.tf | ||
│ └── prod.tfvars | ||
├── global.tfvars | ||
|
||
## Building | ||
|
||
TODO | ||
|
||
``` | ||
``` | ||
|
||
## Testing | ||
|
||
TODO | ||
|
||
|
||
``` | ||
``` | ||
|
||
|
||
``` | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/yourusername/varlink/internal/config" | ||
"github.com/yourusername/varlink/internal/env" | ||
"github.com/yourusername/varlink/internal/tfvars" | ||
) | ||
|
||
func main() { | ||
cfg := config.Parse() | ||
|
||
if cfg.Deactivate { | ||
env.Deactivate() | ||
return | ||
} | ||
|
||
environment, tfvarsFiles, err := findEnvironmentAndVars(".", cfg.LevelsAbove, cfg.MaxSearchDepth) | ||
if err != nil { | ||
fmt.Printf("I apologize, but I encountered an issue while searching for your environment and tfvars files:\n%s\n", err) | ||
fmt.Println("Please ensure you're within a project structure that includes an 'environments' directory,") | ||
fmt.Println("or consider adjusting the search parameters if your project structure is non-standard.") | ||
os.Exit(1) | ||
} | ||
|
||
vars, err := tfvars.ParseFiles(tfvarsFiles) | ||
if err != nil { | ||
fmt.Printf("Error parsing tfvars files: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
if cfg.DryRun { | ||
fmt.Printf("Would set environment to: %s\n", environment) | ||
for k, v := range vars { | ||
fmt.Printf("Would set TF_VAR_%s=%s\n", k, v) | ||
} | ||
} else { | ||
env.SetVars(vars) | ||
fmt.Printf("Environment set to: %s\n", environment) | ||
fmt.Printf("Set %d environment variables\n", len(vars)) | ||
} | ||
} | ||
|
||
func findEnvironmentAndVars(startDir string, levelsAbove int, maxSearchDepth int) (string, []string, error) { | ||
currentDir, err := filepath.Abs(startDir) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("error getting absolute path: %w", err) | ||
} | ||
|
||
var environment string | ||
var tfvarsFiles []string | ||
environmentFound := false | ||
levelsAboveCount := 0 | ||
searchDepth := 0 | ||
|
||
for { | ||
searchDepth++ | ||
if searchDepth > maxSearchDepth { | ||
return "", nil, fmt.Errorf("exceeded maximum search depth of %d directories", maxSearchDepth) | ||
} | ||
|
||
files, err := filepath.Glob(filepath.Join(currentDir, "*.tfvars")) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("error searching for tfvars files: %w", err) | ||
} | ||
tfvarsFiles = append(tfvarsFiles, files...) | ||
|
||
parentDir := filepath.Dir(currentDir) | ||
|
||
if !environmentFound { | ||
if filepath.Base(parentDir) == "environments" { | ||
environment = filepath.Base(currentDir) | ||
environmentFound = true | ||
} | ||
} else { | ||
levelsAboveCount++ | ||
if levelsAboveCount > levelsAbove { | ||
break | ||
} | ||
} | ||
|
||
if currentDir == parentDir { | ||
break | ||
} | ||
currentDir = parentDir | ||
} | ||
|
||
if !environmentFound && filepath.Base(currentDir) == "environments" { | ||
environment = filepath.Base(startDir) | ||
environmentFound = true | ||
} | ||
|
||
if !environmentFound { | ||
return "", nil, fmt.Errorf("couldn't find 'environments' directory in the path") | ||
} | ||
|
||
return environment, tfvarsFiles, nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/iancaseydouglas/varlink | ||
|
||
go 1.21 |
Oops, something went wrong.