Skip to content

Commit

Permalink
fix: use SplitN when working with vars, unbreak vars with = in them
Browse files Browse the repository at this point in the history
Previously, the functions that interacted with and handling the
splitting of variables into names/values was using `strings.Split()`,
which splits on each token and returns a slice with all of them.

In the context we're operating here where it's `var=val`, we only ever
need to split on the first `=`, so this switches to `strings.SplitN()`
with a count of 2. This ensures that we only ever split on the first `=`
present and that the second slice item is the entire remainder of the
unsplit string. This ensures that variables with quotes in them can be
properly parsed and not be skipped for being invalid. Ie, a var like
this will now be properly parsed:

`NODE_EXPORTER_FLAGS='--web.listen-address=":9100"'
  • Loading branch information
tjhop committed Jun 27, 2024
1 parent 60dc660 commit b9df464
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func getUpdatedVars(oldVars, newVars VariableSlice) VariableSlice {

// compare envs before and after and only return the new/updated variables
for _, newKV := range newVars {
newTokens := strings.Split(newKV, "=")
newTokens := strings.SplitN(newKV, "=", 2)
if len(newTokens) != 2 {
continue
}
Expand All @@ -110,7 +110,7 @@ func getUpdatedVars(oldVars, newVars VariableSlice) VariableSlice {
found := false

for _, oldKV := range oldVars {
oldTokens := strings.Split(oldKV, "=")
oldTokens := strings.SplitN(oldKV, "=", 2)
if len(oldTokens) != 2 {
continue
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func flattenEnvVarMap(varMap map[string]expand.Variable) VariableSlice {
func MakeVariableMap(varSlice VariableSlice) VariableMap {
varMap := make(VariableMap)
for _, v := range varSlice {
tokens := strings.Split(v, "=")
tokens := strings.SplitN(v, "=", 2)
if len(tokens) != 2 {
continue
}
Expand Down

0 comments on commit b9df464

Please sign in to comment.