Skip to content

Commit

Permalink
Merge pull request #5 from st0le/master
Browse files Browse the repository at this point in the history
feat: overwrite feature

Example:

`enve --overwrite --file .env`
  • Loading branch information
joseluisq committed Dec 26, 2023
2 parents 1c40d04 + 60afa71 commit cf5dbb2
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func Execute() {
Value: "text",
Summary: "Output environment variables using text, json or xml format",
},
cli.FlagBool{
Name: "overwrite",
Aliases: []string{"w"},
Value: false,
Summary: "Overwrite environment variables if already set",
},
}
app.Handler = appHandler

Expand All @@ -65,6 +71,7 @@ func appHandler(ctx *cli.AppContext) error {
if err != nil {
return err
}

fileProvided := file.IsProvided()
filePath := file.Value()
if fileProvided && filePath == "" {
Expand All @@ -74,8 +81,23 @@ func appHandler(ctx *cli.AppContext) error {
if fileProvided && !fileFound {
return fmt.Errorf("file path was not found or inaccessible")
}

overwrite, err := flags.Bool("overwrite")
if err != nil {
return err
}

overwriteValue, err := overwrite.Value()
if err != nil {
return err
}

if fileFound {
err = godotenv.Load(filePath)
if overwriteValue {
err = godotenv.Overload(filePath)
} else {
err = godotenv.Load(filePath)
}
}
if err != nil {
return fmt.Errorf("env file: %v", err)
Expand All @@ -86,7 +108,7 @@ func appHandler(ctx *cli.AppContext) error {
// 2. Print all env variables in text format by default
providedFlags := len(flags.GetProvided())
if (providedFlags == 0 && len(tailArgs) == 0) ||
(providedFlags == 1 && len(tailArgs) == 0 && fileProvided) {
(providedFlags <= 2 && len(tailArgs) == 0 && fileProvided) {
return printEnvText()
}

Expand Down
96 changes: 96 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,99 @@ func TestPlainEnv(t *testing.T) {
t.Error("one or more env keys have wrong values")
}
}

func TestOverwriteDisabledPlainEnv(t *testing.T) {
expected := strings.Join([]string{
"DB_PROTOCOL=udp",
"DB_HOST=127.0.0.1",
"DB_PORT=3306",
"DB_DEFAULT_CHARACTER_SET=utf8",
"DB_EXPORT_GZIP=true",
"DB_EXPORT_FILE_PATH=dbname.sql.gz",
"DB_NAME=dbname",
"DB_USERNAME=username",
"DB_PASSWORD=passwd",
"DB_ARGS=",
}, "\n")

cwd, err := os.Getwd()

if err != nil {
t.Error(err)
}

basePath := path.Dir(cwd)

envFile := basePath + "/fixtures/plain.env"
bashFile := basePath + "/fixtures/test.sh"

// Set DB_PROTOCOL as UDP before running the script
os.Setenv("DB_PROTOCOL", "udp")

cmd := exec.Command("go", "run", basePath+"/main.go", "-f", envFile, bashFile)

var out bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = &out

err = cmd.Run()

if err != nil {
t.Error("error trying to read the .env file.", err)
}

actual := strings.Trim(out.String(), "\n")

if expected != actual {
t.Error("one or more env keys have wrong values")
}
}

func TestOverwriteEnabledPlainEnv(t *testing.T) {
expected := strings.Join([]string{
"DB_PROTOCOL=tcp",
"DB_HOST=127.0.0.1",
"DB_PORT=3306",
"DB_DEFAULT_CHARACTER_SET=utf8",
"DB_EXPORT_GZIP=true",
"DB_EXPORT_FILE_PATH=dbname.sql.gz",
"DB_NAME=dbname",
"DB_USERNAME=username",
"DB_PASSWORD=passwd",
"DB_ARGS=",
}, "\n")

cwd, err := os.Getwd()

if err != nil {
t.Error(err)
}

basePath := path.Dir(cwd)

envFile := basePath + "/fixtures/plain.env"
bashFile := basePath + "/fixtures/test.sh"

// Set DB_PROTOCOL as UDP before running the script
os.Setenv("DB_PROTOCOL", "udp")

cmd := exec.Command("go", "run", basePath+"/main.go", "-w", "-f", envFile, bashFile)

var out bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = &out

err = cmd.Run()

if err != nil {
t.Error("error trying to read the .env file.", err)
}

actual := strings.Trim(out.String(), "\n")

if expected != actual {
t.Error("one or more env keys have wrong values")
}
}
88 changes: 88 additions & 0 deletions cmd/cmd_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,91 @@ func TestPlainEnv(t *testing.T) {
}
}
}

func TestOverwriteDisabledPlainEnv(t *testing.T) {
expected := []string{
"DB_PROTOCOL=udp",
"DB_HOST=127.0.0.1",
"DB_PORT=3306",
"DB_DEFAULT_CHARACTER_SET=utf8",
"DB_EXPORT_GZIP=true",
"DB_EXPORT_FILE_PATH=dbname.sql.gz",
"DB_NAME=dbname",
"DB_USERNAME=username",
"DB_PASSWORD=passwd",
"DB_ARGS=",
}

basePath := path.Dir("./../")

envFile := basePath + "/fixtures/plain.env"
psFile := basePath + "/fixtures/test.ps1"

// Set DB_PROTOCOL as UDP before running the script
os.Setenv("DB_PROTOCOL", "udp")

cmd := exec.Command("go", "run", basePath+"/main.go", "-f", envFile, "powershell", "-ExecutionPolicy", "Bypass", "-File", psFile)

var out bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = &out

err := cmd.Run()

if err != nil {
t.Errorf("error trying to read the .env file.\n %s", err)
}

actual := strings.Split(out.String(), "\n")
for i, exp := range expected {
act := strings.TrimRight(actual[i], "\r")
if exp != act {
t.Errorf("actual: [%s] expected: [%s]", act, exp)
}
}
}

func TestOverwriteEnabledPlainEnv(t *testing.T) {
expected := []string{
"DB_PROTOCOL=tcp",
"DB_HOST=127.0.0.1",
"DB_PORT=3306",
"DB_DEFAULT_CHARACTER_SET=utf8",
"DB_EXPORT_GZIP=true",
"DB_EXPORT_FILE_PATH=dbname.sql.gz",
"DB_NAME=dbname",
"DB_USERNAME=username",
"DB_PASSWORD=passwd",
"DB_ARGS=",
}

basePath := path.Dir("./../")

envFile := basePath + "/fixtures/plain.env"
psFile := basePath + "/fixtures/test.ps1"

// Set DB_PROTOCOL as UDP before running the script
os.Setenv("DB_PROTOCOL", "udp")

cmd := exec.Command("go", "run", basePath+"/main.go", "-w", "-f", envFile, "powershell", "-ExecutionPolicy", "Bypass", "-File", psFile)

var out bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = &out

err := cmd.Run()

if err != nil {
t.Errorf("error trying to read the .env file.\n %s", err)
}

actual := strings.Split(out.String(), "\n")
for i, exp := range expected {
act := strings.TrimRight(actual[i], "\r")
if exp != act {
t.Errorf("actual: [%s] expected: [%s]", act, exp)
}
}
}

0 comments on commit cf5dbb2

Please sign in to comment.