Skip to content

Commit

Permalink
Initial commit for varlink project
Browse files Browse the repository at this point in the history
  • Loading branch information
iancaseydouglas committed Oct 7, 2024
0 parents commit 8d63118
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
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 ./...
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/bin/
*.exe
*.test
*.out

21 changes: 21 additions & 0 deletions LICENSE
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.
10 changes: 10 additions & 0 deletions Makefile
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
67 changes: 67 additions & 0 deletions README.md
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


```
```


```
```


102 changes: 102 additions & 0 deletions cmd/varlink/main.go
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"

Check failure on line 8 in cmd/varlink/main.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/yourusername/varlink/internal/config; to add it:
"github.com/yourusername/varlink/internal/env"

Check failure on line 9 in cmd/varlink/main.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/yourusername/varlink/internal/env; to add it:
"github.com/yourusername/varlink/internal/tfvars"

Check failure on line 10 in cmd/varlink/main.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/yourusername/varlink/internal/tfvars; to add it:
)

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
}

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/iancaseydouglas/varlink

go 1.21
Loading

0 comments on commit 8d63118

Please sign in to comment.