Skip to content

Commit

Permalink
use the same dotenv parser as docker
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Nov 4, 2024
1 parent ac8a966 commit 666bd31
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 192 deletions.
20 changes: 19 additions & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"errors"
"flag"
"fmt"
"os"
"os/exec"

"github.com/ysmood/goe"
_ "github.com/ysmood/goe/load"
"github.com/ysmood/goe/pkg/utils"
)

Expand All @@ -17,6 +17,24 @@ var (
)

func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [dotenv-file-path]\n", os.Args[0])
flag.PrintDefaults()
}

flag.Parse()

envFile := flag.Arg(0)

if envFile == "" {
envFile = goe.DOTENV
}

err := goe.AutoLoad(envFile)
if err != nil {
panic(err)
}

shell, err := Shell()
if err != nil {
panic(fmt.Errorf("%w: %w", errGetShell, err))
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ module github.com/ysmood/goe
go 1.22

require (
github.com/alecthomas/participle/v2 v2.1.1
github.com/compose-spec/compose-go v1.20.2
github.com/ysmood/got v0.39.4
)

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)

require (
github.com/ysmood/gop v0.2.0 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
Expand Down
34 changes: 26 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
github.com/alecthomas/participle/v2 v2.1.1/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/compose-spec/compose-go v1.20.2 h1:u/yfZHn4EaHGdidrZycWpxXgFffjYULlTbRfJ51ykjQ=
github.com/compose-spec/compose-go v1.20.2/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJqkg7mr4PM9tFKU8RM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ysmood/gop v0.2.0 h1:+tFrG0TWPxT6p9ZaZs+VY+opCvHU8/3Fk6BaNv6kqKg=
github.com/ysmood/gop v0.2.0/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
github.com/ysmood/got v0.39.4 h1:8ru7J25Zmf/sMTNYOF2172xVkjQrPMJ3R5d6uymoqL8=
github.com/ysmood/got v0.39.4/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
32 changes: 26 additions & 6 deletions goe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package goe

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
Expand All @@ -12,10 +13,31 @@ import (
"strings"
"time"

"github.com/ysmood/goe/pkg/dotenv"
"github.com/compose-spec/compose-go/dotenv"
"github.com/ysmood/goe/pkg/utils"
"golang.org/x/exp/constraints"
)

// AutoLoad file and return informative message about what this function has done.
func AutoLoad(file string) error {
err := Load(false, true, file)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
utils.Println(Prefix, "env file not found, skipped loading.")

return nil
}

return err //nolint:wrapcheck
}

path, _ := LookupFile(file)

utils.Println(Prefix, "Loaded environment variables from:", path)

return nil
}

// Load .env file and return informative message about what this function has done.
// It will recursively search for the file in parent folders until it finds one.
// It uses [LoadDotEnv] to parse and load the content.
Expand All @@ -42,14 +64,12 @@ func Load(override, expand bool, file string) error {
// If override is true, it will override the existing env vars.
// If expand is true, it will expand the env vars via [os.ExpandEnv].
func LoadContent(override, expand bool, content string) error {
doc, err := dotenv.Parse(content)
doc, err := dotenv.ParseWithLookup(bytes.NewBufferString(content), os.LookupEnv)
if err != nil {
return fmt.Errorf("failed to parse .env file: %w", err)
return fmt.Errorf("failed to parse env content: %w", err)
}

for _, p := range doc.Lines {
k, v := p.Key, p.Value

for k, v := range doc {
if !override {
if _, has := os.LookupEnv(k); has {
continue
Expand Down
19 changes: 1 addition & 18 deletions load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
package load

import (
"errors"
"fmt"
"os"

"github.com/ysmood/goe"
"github.com/ysmood/goe/pkg/utils"
)

func init() {
Expand All @@ -20,20 +18,5 @@ func init() {
}

func load() error {
err := goe.Load(false, true, goe.DOTENV)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
utils.Println(goe.Prefix, ".env file not found, skipped loading.")

return nil
}

return err //nolint:wrapcheck
}

path, _ := goe.LookupFile(goe.DOTENV)

utils.Println(goe.Prefix, "Loaded environment variables from:", path)

return nil
return goe.AutoLoad(goe.DOTENV)
}
68 changes: 0 additions & 68 deletions pkg/dotenv/.got/snapshots/TestBasic/basic.json

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/dotenv/parser.go

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/dotenv/parser_test.go

This file was deleted.

0 comments on commit 666bd31

Please sign in to comment.