diff --git a/go.mod b/go.mod index 3f70c3d..7c34952 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b90df8a..4fd5b18 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/spec/dockerSwarm.go b/spec/dockerSwarm.go index 8c49233..10ebe43 100644 --- a/spec/dockerSwarm.go +++ b/spec/dockerSwarm.go @@ -17,7 +17,6 @@ limitations under the License. package spec import ( - "bufio" "errors" "fmt" "os" @@ -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 { @@ -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 } @@ -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 { diff --git a/spec/dockerSwarm_test.go b/spec/dockerSwarm_test.go index c703b56..27bf80d 100644 --- a/spec/dockerSwarm_test.go +++ b/spec/dockerSwarm_test.go @@ -17,7 +17,6 @@ limitations under the License. package spec import ( - "os" "testing" ) @@ -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) - } -}