Skip to content

Commit 14629eb

Browse files
committed
fix: support env var for postStart commands
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent 0dddc52 commit 14629eb

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

pkg/library/lifecycle/poststart.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,13 @@ func buildUserScript(commands []dw.Command) (string, error) {
163163
// Should be caught by earlier validation, but good to be safe
164164
return "", fmt.Errorf("exec command is nil for command ID %s", command.Id)
165165
}
166-
if len(execCmd.Env) > 0 {
167-
return "", fmt.Errorf("env vars in postStart command %s are unsupported", command.Id)
168-
}
169166
var singleCommandParts []string
167+
for _, envVar := range execCmd.Env {
168+
singleCommandParts = append(singleCommandParts, fmt.Sprintf("export %s=%q", envVar.Name, envVar.Value))
169+
}
170+
170171
if execCmd.WorkingDir != "" {
171-
// Safely quote the working directory path
172-
safeWorkingDir := strings.ReplaceAll(execCmd.WorkingDir, "'", `'\''`)
173-
singleCommandParts = append(singleCommandParts, fmt.Sprintf("cd '%s'", safeWorkingDir))
172+
singleCommandParts = append(singleCommandParts, fmt.Sprintf("cd %q", execCmd.WorkingDir))
174173
}
175174
if execCmd.CommandLine != "" {
176175
singleCommandParts = append(singleCommandParts, execCmd.CommandLine)

pkg/library/lifecycle/poststart_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestBuildUserScript(t *testing.T) {
132132
},
133133
},
134134
},
135-
expectedScript: "cd '/projects/app' && ls -la",
135+
expectedScript: "cd \"/projects/app\" && ls -la",
136136
expectedErr: "",
137137
},
138138
{
@@ -148,7 +148,7 @@ func TestBuildUserScript(t *testing.T) {
148148
},
149149
},
150150
},
151-
expectedScript: "cd '/data'",
151+
expectedScript: "cd \"/data\"",
152152
expectedErr: "",
153153
},
154154
{
@@ -165,7 +165,7 @@ func TestBuildUserScript(t *testing.T) {
165165
},
166166
},
167167
},
168-
expectedScript: "cd '/projects/app'\\''s' && cat file.txt",
168+
expectedScript: "cd \"/projects/app's\" && cat file.txt",
169169
expectedErr: "",
170170
},
171171
{
@@ -201,11 +201,11 @@ func TestBuildUserScript(t *testing.T) {
201201
},
202202
},
203203
},
204-
expectedScript: "cd '/projects/frontend' && npm install\nnpm start\ncd '/projects/backend' && mvn spring-boot:run",
204+
expectedScript: "cd \"/projects/frontend\" && npm install\nnpm start\ncd \"/projects/backend\" && mvn spring-boot:run",
205205
expectedErr: "",
206206
},
207207
{
208-
name: "Command with unsupported Env vars",
208+
name: "Command with Env vars",
209209
commands: []dw.Command{
210210
{
211211
Id: "cmd-with-env",
@@ -220,8 +220,8 @@ func TestBuildUserScript(t *testing.T) {
220220
},
221221
},
222222
},
223-
expectedScript: "",
224-
expectedErr: "env vars in postStart command cmd-with-env are unsupported",
223+
expectedScript: "export MY_VAR=\"test\" && echo $MY_VAR",
224+
expectedErr: "",
225225
},
226226
{
227227
name: "Command with nil Exec field",

0 commit comments

Comments
 (0)