Skip to content

Commit 1bd07cd

Browse files
authored
Merge pull request #42 from JoseLucasapp/env-support
feat: .env support
2 parents a7983fe + 993b861 commit 1bd07cd

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

code_examples/env/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
API_KEY=abc123
2+
ENV=production
3+
4+
#this file is only for test, never push your .env file public

code_examples/env/env.zum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dotenvLoad(".env");
2+
3+
var key << dotenvGet("API_KEY");
4+
var environment << dotenvGet("ENV");
5+
show("{}, {}", key, environment);

evaluator/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func init() {
1717
}
1818

1919
extras := []string{
20-
"date", "hashCode",
20+
"date", "dotenvLoad", "dotenvGet", "hashCode",
2121
}
2222

2323
http := []string{

object/builtins/builtins.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ var Builtins = []struct {
3939
{
4040
"dictValues", DictValuesBuiltin(),
4141
},
42+
{
43+
"dotenvLoad", loadEnvBuiltin(),
44+
},
45+
{
46+
"dotenvGet", getEnvBuiltin(),
47+
},
4248
{
4349
"first", ArrayFirstBuiltin(),
4450
},

object/builtins/env_builtin.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package builtins
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
"zumbra/object"
9+
)
10+
11+
var EnvVars = map[string]string{}
12+
13+
func loadEnvBuiltin() *object.Builtin {
14+
return &object.Builtin{
15+
Fn: func(args ...object.Object) object.Object {
16+
if len(args) != 1 {
17+
return NewError("wrong number of arguments. got=%d, want=1", len(args))
18+
}
19+
20+
path, ok := args[0].(*object.String)
21+
22+
if !ok {
23+
return NewError("argument to `env` must be STRING, got %s", args[0].Type())
24+
}
25+
26+
file, err := os.Open(path.Value)
27+
if err != nil {
28+
fmt.Println(fmt.Sprintf("failed to open file: %s", err))
29+
return nil
30+
}
31+
defer file.Close()
32+
33+
scanner := bufio.NewScanner(file)
34+
35+
for scanner.Scan() {
36+
line := scanner.Text()
37+
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") {
38+
continue
39+
}
40+
parts := strings.SplitN(line, "=", 2)
41+
if len(parts) == 2 {
42+
key := strings.TrimSpace(parts[0])
43+
value := strings.TrimSpace(parts[1])
44+
EnvVars[key] = value
45+
}
46+
}
47+
48+
if err := scanner.Err(); err != nil {
49+
return NewError("failed to read file: %s", err)
50+
}
51+
52+
return nil
53+
},
54+
}
55+
}
56+
57+
func getEnvBuiltin() *object.Builtin {
58+
return &object.Builtin{
59+
Fn: func(args ...object.Object) object.Object {
60+
if len(args) != 1 {
61+
return NewError("wrong number of arguments. got=%d, want=1", len(args))
62+
}
63+
64+
key, ok := args[0].(*object.String)
65+
66+
if !ok {
67+
return NewError("argument to `getEnv` must be STRING, got %s", args[0].Type())
68+
}
69+
70+
value, ok := EnvVars[key.Value]
71+
72+
if !ok {
73+
return nil
74+
}
75+
76+
return &object.String{Value: value}
77+
},
78+
}
79+
}

object/object.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
FLOAT_OBJ = "FLOAT"
3030
DATE_OBJ = "DATE"
3131
RECORD_OBJ = "RECORD"
32+
ENV_OBJ = "ENV"
3233
)
3334

3435
type Object interface {

0 commit comments

Comments
 (0)