Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"request": "launch",
"mode": "auto",
"program": "./",
"args": ["offline", "-file", "./samples/simple/simple-api.tf", "-module", "simple_api"]
"args": ["offline", "-file", "./samples/simple/simple-api.tf", "-module", "simple_api", "-envfile", "./samples/simple/.env.sample"]
},
{
"name": "Attach Node",
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func main() {
moduleName := cCtx.String("module")
port := cCtx.String("port")
nodeDebugPort := cCtx.Int("node-debug-port")
envFile := cCtx.String("envfile")

err := offline.Run(filePath, moduleName, port, NewDebugConfig(nodeDebugPort))
err := offline.Run(filePath, moduleName, port, NewDebugConfig(nodeDebugPort), envFile)

if err != nil {
return err
Expand Down Expand Up @@ -57,6 +58,12 @@ func main() {
Value: "9229",
Usage: "The port number that the Node.js debugger should listen on",
},
&cli.StringFlag{
Name: "envfile",
Required: false,
Value: "",
Usage: "File containing environment variables in key-value (.env) format",
},
},
},
},
Expand Down
44 changes: 42 additions & 2 deletions offline/offline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package offline

import (
"bufio"
"errors"
"fmt"
"log"
Expand All @@ -19,7 +20,7 @@ import (

var DebugConfig config.DebugConfig

func Run(filePath string, moduleName string, port string, debugConfig config.DebugConfig) error {
func Run(filePath string, moduleName string, port string, debugConfig config.DebugConfig, envFile string) error {
DebugConfig = debugConfig
terrableConfig, err := utils.ParseTerraformFile(filePath, moduleName)

Expand All @@ -46,6 +47,15 @@ func Run(filePath string, moduleName string, port string, debugConfig config.Deb
var wg sync.WaitGroup
defer wg.Wait()

// Read environment variables from the specified env file (if provided)
var fileEnvVars map[string]string
if envFile != "" {
fileEnvVars, err = readEnvFile(envFile)
if err != nil {
return fmt.Errorf("could not read env file: %w", err)
}
}

r := mux.NewRouter()

// Not Found handlers
Expand All @@ -70,7 +80,7 @@ func Run(filePath string, moduleName string, port string, debugConfig config.Deb

ServeHandler(&HandlerInstance{
handlerConfig: handler,
envVars: mergeEnvMaps(terrableConfig.GlobalEnvironmentVariables, handler.EnvironmentVariables),
envVars: mergeEnvMaps(terrableConfig.GlobalEnvironmentVariables, mergeEnvMaps(handler.EnvironmentVariables, fileEnvVars)),
}, r)
}(handler)
}
Expand Down Expand Up @@ -224,3 +234,33 @@ func mergeEnvMaps(global, local map[string]string) map[string]string {

return merged
}

func readEnvFile(filePath string) (map[string]string, error) {
envVars := make(map[string]string)

file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
envVars[key] = value
}
}

if err := scanner.Err(); err != nil {
return nil, err
}

return envVars, nil
}
2 changes: 2 additions & 0 deletions samples/simple/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ENV_FILE_VAL=value-from-env-file
ENV_FILE_OVERRIDE=overridden-value
12 changes: 12 additions & 0 deletions samples/simple/simple-api.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ module "simple_api" {
}
},

# Echo Handler with some variables that should be overwritten by the .env file
EchoEnvTest: {
source = "./src/Echo.ts"
environment_variables = {
ENV_FILE_OVERRIDE = "SHOULD_NOT_ECHO"
}
http = {
GET = "/echo-env-test"
}
},


# Echo Handler with no local environment variables
EchoHandlerNoLocalEnv: {
source = "./src/Echo.ts"
Expand Down
2 changes: 1 addition & 1 deletion terrable_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.9.0
version = 0.10.0
2 changes: 1 addition & 1 deletion tests/_start.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
go build ../
./terrable offline -f "../samples/simple/simple-api.tf" -m "simple_api" -p "8081"
./terrable offline -f "../samples/simple/simple-api.tf" -m "simple_api" -p "8081" -envfile "../samples/simple/.env.sample"
6 changes: 6 additions & 0 deletions tests/requests/env_file_overrides.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://127.0.0.1:8081/echo-env-test
HTTP 200

[Asserts]
jsonpath "$.env.ENV_FILE_VAL" == "value-from-env-file"
jsonpath "$.env.ENV_FILE_OVERRIDE" == "overridden-value"