From e42a0da48695b9bed78840281a61bdb6ada25158 Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 3 Aug 2023 11:44:26 +0200 Subject: [PATCH] add environment variable support --- Dockerfile | 9 ++++++--- go.mod | 2 ++ go.sum | 2 ++ src/built-ins.go | 1 + src/env.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/env.go diff --git a/Dockerfile b/Dockerfile index 57315fa..08b246f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,11 @@ # Use Alpine as the base image FROM golang:alpine +# Install isotope +FROM ugric/isotope:latest + # Set the Current Working Directory inside the container -WORKDIR /app +WORKDIR /argon # Copy go mod and sum files COPY go.mod go.sum ./ @@ -11,7 +14,7 @@ COPY go.mod go.sum ./ RUN go mod download # Copy the source from the current directory to the Working Directory inside the container -COPY . . +COPY ./src ./src # Build the Go app RUN go build -trimpath -ldflags="-s -w" -o bin/argon ./src @@ -20,4 +23,4 @@ RUN go build -trimpath -ldflags="-s -w" -o bin/argon ./src RUN chmod +x bin/argon # add the binary to the path -ENV PATH="/app/bin:${PATH}" \ No newline at end of file +ENV PATH="/argon/bin:${PATH}" \ No newline at end of file diff --git a/go.mod b/go.mod index ba9a91d..5b1f0e6 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,8 @@ require ( github.com/wadey/go-rounding v1.1.0 ) +require github.com/joho/godotenv v1.5.1 // indirect + require ( github.com/gabriel-vasile/mimetype v1.4.2 golang.org/x/net v0.8.0 // indirect diff --git a/go.sum b/go.sum index feabdf9..f627422 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jwalton/go-supportscolor v1.1.0 h1:HsXFJdMPjRUAx8cIW6g30hVSFYaxh9yRQwEWgkAR7lQ= github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= diff --git a/src/built-ins.go b/src/built-ins.go index 6ff62a3..61a1353 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -8,6 +8,7 @@ import ( func makeGlobal() ArObject { var vars = anymap{} vars["global"] = vars + vars["env"] = env vars["term"] = ArTerm vars["ArgonVersion"] = ArString(VERSION) vars["number"] = builtinFunc{"number", ArgonNumber} diff --git a/src/env.go b/src/env.go new file mode 100644 index 0000000..486420c --- /dev/null +++ b/src/env.go @@ -0,0 +1,34 @@ +package main + +import ( + "os" + "path/filepath" + "strings" + + "github.com/joho/godotenv" +) + +// get the environment variables +func getEnv() ArObject { + env := make(anymap) + for _, e := range os.Environ() { + pair := strings.Split(e, "=") + env[pair[0]] = ArString(pair[1]) + } + cwd, err := os.Getwd() + if err == nil { + envfile := filepath.Join(cwd, ".env") + err := godotenv.Load(envfile) + if err == nil { + values, err := godotenv.Read() + if err == nil { + for k, v := range values { + env[k] = ArString(v) + } + } + } + } + return Map(env) +} + +var env = getEnv()