Skip to content

Commit

Permalink
feat(cli): add --init-env flag to provide extra environment variable
Browse files Browse the repository at this point in the history
during workspace initialization
  • Loading branch information
pascalbreuninger committed Jun 21, 2024
1 parent 66d6fd6 commit acc118b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
upCmd.Flags().StringSliceVar(&cmd.PrebuildRepositories, "prebuild-repository", []string{}, "Docker repository that hosts devpod prebuilds for this workspace")
upCmd.Flags().StringArrayVar(&cmd.WorkspaceEnv, "workspace-env", []string{}, "Extra env variables to put into the workspace. E.g. MY_ENV_VAR=MY_VALUE")
upCmd.Flags().StringSliceVar(&cmd.WorkspaceEnvFile, "workspace-env-file", []string{}, "The path to files containing a list of extra env variables to put into the workspace. E.g. MY_ENV_VAR=MY_VALUE")
upCmd.Flags().StringArrayVar(&cmd.InitEnv, "init-env", []string{}, "Extra env variables to inject during the initialization of the workspace. E.g. MY_ENV_VAR=MY_VALUE")
upCmd.Flags().StringVar(&cmd.ID, "id", "", "The id to use for the workspace")
upCmd.Flags().StringVar(&cmd.Machine, "machine", "", "The machine to use for this workspace. The machine needs to exist beforehand or the command will fail. If the workspace already exists, this option has no effect")
upCmd.Flags().StringVar(&cmd.IDE, "ide", "", "The IDE to open the workspace in. If empty will use vscode locally or in browser")
Expand Down Expand Up @@ -791,6 +792,7 @@ func mergeDevPodUpOptions(baseOptions *provider2.CLIOptions) error {
return fmt.Errorf("decode up options: %w", err)
} else if found {
baseOptions.WorkspaceEnv = append(oldOptions.WorkspaceEnv, baseOptions.WorkspaceEnv...)
baseOptions.InitEnv = append(oldOptions.InitEnv, baseOptions.InitEnv...)
baseOptions.PrebuildRepositories = append(oldOptions.PrebuildRepositories, baseOptions.PrebuildRepositories...)
baseOptions.IDEOptions = append(oldOptions.IDEOptions, baseOptions.IDEOptions...)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/devcontainer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (r *runner) Up(ctx context.Context, options UpOptions) (*config.Result, err
}()

// run initializeCommand
err = runInitializeCommand(r.LocalWorkspaceFolder, substitutedConfig.Config, r.Log)
err = runInitializeCommand(r.LocalWorkspaceFolder, substitutedConfig.Config, options.InitEnv, r.Log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -315,6 +315,7 @@ func isDockerFileConfig(config *config.DevContainerConfig) bool {
func runInitializeCommand(
workspaceFolder string,
config *config.DevContainerConfig,
extraEnvVars []string,
log log.Logger,
) error {
if len(config.InitializeCommand) == 0 {
Expand All @@ -338,9 +339,13 @@ func runInitializeCommand(
defer errwriter.Close()

cmd := exec.Command(args[0], args[1:]...)
env := cmd.Environ()
env = append(env, extraEnvVars...)

cmd.Stdout = writer
cmd.Stderr = errwriter
cmd.Dir = workspaceFolder
cmd.Env = env
err := cmd.Run()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type CLIOptions struct {
DevContainerPath string `json:"devContainerPath,omitempty"`
WorkspaceEnv []string `json:"workspaceEnv,omitempty"`
WorkspaceEnvFile []string `json:"workspaceEnvFile,omitempty"`
InitEnv []string `json:"initEnv,omitempty"`
Recreate bool `json:"recreate,omitempty"`
Reset bool `json:"reset,omitempty"`
Proxy bool `json:"proxy,omitempty"`
Expand Down

0 comments on commit acc118b

Please sign in to comment.