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

Can I get the original value if variable empty #56

Open
jLopezbarb opened this issue Jun 1, 2023 · 3 comments
Open

Can I get the original value if variable empty #56

jLopezbarb opened this issue Jun 1, 2023 · 3 comments

Comments

@jLopezbarb
Copy link

jLopezbarb commented Jun 1, 2023

Hi, I need to know if there is a variable in my text that is empty after expanding it. I need those variables not to expand and use the original string.

os.Setenv("GREETINGS", "hello")
value := "$GREETINGS $NAME"
result, err := envsubst.String(value)
if err != nil {
	return "", fmt.Errorf("error expanding environment on '%s': %s", value, err.Error())
}
fmt.Println(result) // The output is: "hello "

Is there a way of getting hello $NAME as the output?

@ashvinsharma
Copy link

This would be so helpful.

@artemiyokulov
Copy link

need this too!

@hellt
Copy link

hellt commented Dec 2, 2024

Did anyone implemented this feature over time? I went looking into the forks, and the closes thing I found is https://github.com/dnitsch/envsubst by @dnitsch

https://github.com/dnitsch/envsubst/blob/86a677a7c7e531f5ce7c9efa9e5c3a888f463446/envsubst.go#L33

The only drawback with this fork is that it doesn't seem to handle the default variables, that is when your template contain a variable that is set to a default when not set in the env:

"if default is set, noreplace should not be used": {
	input: `Some: $REPLACE
NoReplaceNotToBeUsed: ${someVarWithDefault:=myDefault}`,
	env:          []string{"REPLACE=bar"},
	restrictions: &Restrictions{NoUnset: false, NoEmpty: false, NoDigit: true, NoReplace: true},
	expected: `Some: bar
NoReplaceNotToBeUsed: myDefault`,
},

The above test fails in this fork...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants