(ctx devspacecontext.Context, hook *latest.HookConfig, cmdExtraEnv map[string]string)
| 36 | var EnvironmentVariableRegEx = regexp.MustCompile(`^[A-Za-z0-9_]+$`) |
| 37 | |
| 38 | func (l *localCommandHook) Execute(ctx devspacecontext.Context, hook *latest.HookConfig, cmdExtraEnv map[string]string) error { |
| 39 | // Create extra env variables |
| 40 | osArgsBytes, err := json.Marshal(os.Args) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | extraEnv := map[string]string{ |
| 45 | OsArgsEnv: string(osArgsBytes), |
| 46 | } |
| 47 | if ctx.KubeClient() != nil { |
| 48 | extraEnv[KubeContextEnv] = ctx.KubeClient().CurrentContext() |
| 49 | extraEnv[KubeNamespaceEnv] = ctx.KubeClient().Namespace() |
| 50 | } |
| 51 | for k, v := range cmdExtraEnv { |
| 52 | extraEnv[k] = v |
| 53 | } |
| 54 | for k, v := range ctx.Config().Variables() { |
| 55 | if !EnvironmentVariableRegEx.MatchString(k) { |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | extraEnv[k] = fmt.Sprintf("%v", v) |
| 60 | } |
| 61 | |
| 62 | // resolve hook command and args |
| 63 | hookCommand, hookArgs, err := ResolveCommand(ctx.Context(), hook.Command, hook.Args, ctx.WorkingDir(), ctx.Config(), ctx.Dependencies()) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // if args are nil we execute the command in a shell |
| 69 | stdout := &bytes.Buffer{} |
| 70 | stderr := &bytes.Buffer{} |
| 71 | defer func() { |
| 72 | if hook.Name != "" { |
| 73 | ctx.Config().SetRuntimeVariable("hooks."+hook.Name+".stdout", strings.TrimSpace(stdout.String())) |
| 74 | ctx.Config().SetRuntimeVariable("hooks."+hook.Name+".stderr", strings.TrimSpace(stderr.String())) |
| 75 | } |
| 76 | }() |
| 77 | |
| 78 | if hook.Args == nil { |
| 79 | return engine.ExecuteSimpleShellCommand(ctx.Context(), ctx.WorkingDir(), env.NewVariableEnvProvider(ctx.Environ(), extraEnv), io.MultiWriter(l.Stdout, stdout), io.MultiWriter(l.Stderr, stderr), nil, hookCommand) |
| 80 | } |
| 81 | |
| 82 | // else we execute it directly |
| 83 | return command.Command(ctx.Context(), ctx.WorkingDir(), env.NewVariableEnvProvider(ctx.Environ(), extraEnv), io.MultiWriter(l.Stdout, stdout), io.MultiWriter(l.Stderr, stderr), nil, hookCommand, hookArgs...) |
| 84 | } |
| 85 | |
| 86 | func ResolveCommand(ctx context.Context, command string, args []string, dir string, config config.Config, dependencies []types.Dependency) (string, []string, error) { |
| 87 | // resolve hook command |
nothing calls this directly
no test coverage detected