Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: using env parsing lib #64

Merged
merged 1 commit into from
Dec 26, 2023
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.11 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/hashicorp/go-envparse v0.1.0
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY=
github.com/hashicorp/go-envparse v0.1.0/go.mod h1:OHheN1GoygLlAkTlXLXvAdnXdZxy8JUweQ1rAXx1xnc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -216,8 +218,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
Expand Down
53 changes: 12 additions & 41 deletions spec/dockerSwarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package spec

import (
"bufio"
"errors"
"fmt"
"os"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/charmbracelet/log"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm"
"github.com/hashicorp/go-envparse"
)

type DockerSwarm struct {
Expand Down Expand Up @@ -108,7 +108,17 @@ func (d *DockerSwarm) GetServiceSpec(appName string, networkID string) ([]swarm.
for _, envFile := range spec.EnvFile {
log.Info("Using environment variable from files", "file", envFile)

envVars, err := getEnvVars(envFile)
fileName, err := normalizeFilePath(envFile)
if err != nil {
return []swarm.ServiceSpec{}, err
}

file, err := os.Open(fileName)
if err != nil {
return []swarm.ServiceSpec{}, err
}

envVars, err := envparse.Parse(file)
if err != nil {
return []swarm.ServiceSpec{}, err
}
Expand Down Expand Up @@ -195,45 +205,6 @@ func (d *DockerSwarm) GetServiceSpec(appName string, networkID string) ([]swarm.
return specs, nil
}

func getEnvVars(fileName string) (map[string]string, error) {
result := make(map[string]string)

fileName, err := normalizeFilePath(fileName)
if err != nil {
return map[string]string{}, err
}

fileData, err := os.Open(fileName)
if err != nil {
log.Warn("file path does not exist", "file", fileName)
fileName, _ = strings.CutPrefix(fileName, "/home")

fileData, err = os.Open(fileName)
if err != nil {
log.Warn("file path does not exist", "file", fileName)
return map[string]string{}, err
}
}
defer fileData.Close()

scanner := bufio.NewScanner(fileData)

for scanner.Scan() {
line := scanner.Text()
tokens := strings.SplitN(line, "=", 2)

if len(tokens) == 2 {
key := strings.TrimSpace(tokens[0])
value := strings.TrimSpace(tokens[1])
value = strings.ReplaceAll(value, "\"", "")

result[key] = value
}
}

return result, err
}

func normalizeFilePath(fileName string) (string, error) {
currentUser, err := user.Current()
if err != nil {
Expand Down
31 changes: 0 additions & 31 deletions spec/dockerSwarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package spec

import (
"os"
"testing"
)

Expand Down Expand Up @@ -55,33 +54,3 @@ func TestNormalizeFilePath(t *testing.T) {
t.Fail()
}
}

func TestGetEnvVars(t *testing.T) {
tempFile, err := os.CreateTemp(os.TempDir(), "test_file-*")
if err != nil {
t.Error(err.Error())
}
defer tempFile.Close()

tempFile.WriteString(`
ENV_1="1"
ENV_2="2"
ENV_3=3
ENV_4=4
# Comment
`)

path := tempFile.Name()

res, err := getEnvVars(path)
if err != nil {
t.Error(err.Error())
}

if res["ENV_1"] != "1" ||
res["ENV_2"] != "2" ||
res["ENV_3"] != "3" ||
res["ENV_4"] != "4" {
t.Error("failed to convert env file into map[string]string", res)
}
}