MCPcopy Index your code
hub / github.com/devspace-sh/devspace / Execute

Method Execute

pkg/devspace/hook/local_command.go:38–84  ·  view source on GitHub ↗
(ctx devspacecontext.Context, hook *latest.HookConfig, cmdExtraEnv map[string]string)

Source from the content-addressed store, hash-verified

36var EnvironmentVariableRegEx = regexp.MustCompile(`^[A-Za-z0-9_]+$`)
37
38func (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
86func ResolveCommand(ctx context.Context, command string, args []string, dir string, config config.Config, dependencies []types.Dependency) (string, []string, error) {
87 // resolve hook command

Callers

nothing calls this directly

Calls 14

StringMethod · 0.95
NewVariableEnvProviderFunction · 0.92
ResolveCommandFunction · 0.85
KubeClientMethod · 0.65
CurrentContextMethod · 0.65
NamespaceMethod · 0.65
VariablesMethod · 0.65
ConfigMethod · 0.65
ContextMethod · 0.65
WorkingDirMethod · 0.65
DependenciesMethod · 0.65

Tested by

no test coverage detected