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

proposal: os: ExpandEnv: Support Parameter Substitution and Expansion Notations #47187

Open
Dentrax opened this issue Jul 14, 2021 · 5 comments
Open
Labels
Projects
Milestone

Comments

@Dentrax
Copy link

@Dentrax Dentrax commented Jul 14, 2021

Currently, os.ExpandEnv(s string) string - supports only $var and ${var} notations:

_ExpandEnv replaces ${var} or $var in the string according to the values of the current environment variables. References to undefined variables are replaced by the empty string. _

I propose that Go also should support Parameter Substitution and Expansion notations:

Expression Meaning
${var} Value of var (same as $var)
${var-$DEFAULT} If var not set, evaluate expression as $DEFAULT *
${var:-$DEFAULT} If var not set or is empty, evaluate expression as $DEFAULT *
${var=$DEFAULT} If var not set, evaluate expression as $DEFAULT *
${var:=$DEFAULT} If var not set or is empty, evaluate expression as $DEFAULT *
${var+$OTHER} If var set, evaluate expression as $OTHER, otherwise as null string
${var:+$OTHER} If var set, evaluate expression as $OTHER, otherwise as null string
${var?$ERR_MSG} If var not set, print $ERR_MSG and abort script with an exit status of 1.*
${var:?$ERR_MSG} If var not set, print $ERR_MSG and abort script with an exit status of 1.*
${!varprefix*} Matches all previously declared variables beginning with varprefix
${!varprefix@} Matches all previously declared variables beginning with varprefix
  • * If var is set, evaluate the expression as $var with no side-effects.

  • # Note that some of the above behavior of operators has changed from earlier versions of Bash.

Example:

package main

import (
	"fmt"
	"os"
)

func main() {
	os.Setenv("FOO", "foo")
	
	fmt.Println(os.ExpandEnv("${FOO}"))  //  FOO
	fmt.Println(os.ExpandEnv("${BAR:-bar}"))  //  EMPTY
	fmt.Println(os.ExpandEnv("${BAR:=bar}"))  //  EMPTY
	// etc

}
@gopherbot gopherbot added this to the Proposal milestone Jul 14, 2021
@robpike
Copy link
Contributor

@robpike robpike commented Jul 14, 2021

The os library is not meant to be a bash library, and should not evolve in that direction. Although to do so might be convenient in some situations, the operations listed here, as well as their syntax, are not universal, and it would be wrong to act as though they were. The os package must remain portable in both function and behavior across multiple different systems.

Also, the operations here are trivial to implement in Go in the rare cases they are needed.

@Dentrax
Copy link
Author

@Dentrax Dentrax commented Jul 14, 2021

Oh, make sense! Also, I think we may need to write a "tiny" lexer to parse that bash syntax, which may overcomplicate os package. I see the concern now! Thanks.

@rsc rsc added this to Incoming in Proposals Jul 14, 2021
@mvdan
Copy link
Member

@mvdan mvdan commented Jul 14, 2021

If you do want to support the full shell/bash syntax, take a look at https://pkg.go.dev/mvdan.cc/sh/v3/shell#Expand. The module also contains a syntax parser. I fully agree that none of that should be in the standard library.

@rsc
Copy link
Contributor

@rsc rsc commented Oct 13, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals Oct 13, 2021
@seh
Copy link
Contributor

@seh seh commented Oct 13, 2021

The drone/envsubst package gets you most of the way there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants