TODO can we replace a lot of this func with RuntimeSubprocess.invokeWithEnv?
(input *Input)
| 41 | |
| 42 | // TODO can we replace a lot of this func with RuntimeSubprocess.invokeWithEnv? |
| 43 | func (r *SubprocessPluginRuntime) runGetter(input *Input) (*Output, error) { |
| 44 | msg, ok := (input.Message).(schema.InputMessageGetterV1) |
| 45 | if !ok { |
| 46 | return nil, fmt.Errorf("expected input type schema.InputMessageGetterV1, got %T", input) |
| 47 | } |
| 48 | |
| 49 | tmpDir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("helm-plugin-%s-", r.metadata.Name)) |
| 50 | if err != nil { |
| 51 | return nil, fmt.Errorf("failed to create temporary directory: %w", err) |
| 52 | } |
| 53 | defer os.RemoveAll(tmpDir) |
| 54 | |
| 55 | d := getProtocolCommand(r.RuntimeConfig.ProtocolCommands, msg.Protocol) |
| 56 | if d == nil { |
| 57 | return nil, fmt.Errorf("no downloader found for protocol %q", msg.Protocol) |
| 58 | } |
| 59 | |
| 60 | env := ParseEnv(os.Environ()) |
| 61 | maps.Insert(env, maps.All(r.EnvVars)) |
| 62 | maps.Insert(env, maps.All(ParseEnv(input.Env))) |
| 63 | env["HELM_PLUGIN_NAME"] = r.metadata.Name |
| 64 | env["HELM_PLUGIN_DIR"] = r.pluginDir |
| 65 | env["HELM_PLUGIN_USERNAME"] = msg.Options.Username |
| 66 | env["HELM_PLUGIN_PASSWORD"] = msg.Options.Password |
| 67 | env["HELM_PLUGIN_PASS_CREDENTIALS_ALL"] = strconv.FormatBool(msg.Options.PassCredentialsAll) |
| 68 | |
| 69 | command, args, err := PrepareCommands(d.PlatformCommand, false, []string{}, env) |
| 70 | if err != nil { |
| 71 | return nil, fmt.Errorf("failed to prepare commands for protocol %q: %w", msg.Protocol, err) |
| 72 | } |
| 73 | |
| 74 | args = append( |
| 75 | args, |
| 76 | msg.Options.CertFile, |
| 77 | msg.Options.KeyFile, |
| 78 | msg.Options.CAFile, |
| 79 | msg.Href) |
| 80 | |
| 81 | buf := bytes.Buffer{} // subprocess getters are expected to write content to stdout |
| 82 | |
| 83 | pluginCommand := filepath.Join(r.pluginDir, command) |
| 84 | cmd := exec.Command( |
| 85 | pluginCommand, |
| 86 | args...) |
| 87 | cmd.Env = FormatEnv(env) |
| 88 | cmd.Stdout = &buf |
| 89 | cmd.Stderr = os.Stderr |
| 90 | |
| 91 | slog.Debug("executing plugin command", slog.String("pluginName", r.metadata.Name), slog.String("command", cmd.String())) |
| 92 | if err := executeCmd(cmd, r.metadata.Name); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | return &Output{ |
| 97 | Message: schema.OutputMessageGetterV1{ |
| 98 | Data: buf.Bytes(), |
| 99 | }, |
| 100 | }, nil |
no test coverage detected