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

Closed
Dentrax opened this issue Jul 14, 2021 · 7 comments
Closed

Comments

@Dentrax
Copy link

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 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 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.

@mvdan
Copy link
Member

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 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

@seh
Copy link
Contributor

seh commented Oct 13, 2021

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

@rsc
Copy link
Contributor

rsc commented Oct 20, 2021

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Oct 27, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Oct 27, 2021
@golang golang locked and limited conversation to collaborators Oct 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants