From cfc00262ffefe81b4608102c30fd89cdd43bb719 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Sat, 29 Nov 2025 20:55:11 -0600 Subject: [PATCH] inject ssh env vars by apppending them to the script --- pkg/backy/ssh.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/backy/ssh.go b/pkg/backy/ssh.go index 3059d95..265f6d8 100755 --- a/pkg/backy/ssh.go +++ b/pkg/backy/ssh.go @@ -471,9 +471,6 @@ func (command *Command) RunCmdOnHost(cmdCtxLogger zerolog.Logger, opts *ConfigOp } defer commandSession.Close() - // Inject environment variables - injectEnvIntoSSH(envVars, commandSession, opts, cmdCtxLogger) - // Set output writers cmdOutWriters = io.MultiWriter(&cmdOutBuf) if IsCmdStdOutEnabled() { @@ -492,6 +489,7 @@ func (command *Command) RunCmdOnHost(cmdCtxLogger zerolog.Logger, opts *ConfigOp return command.runScriptFile(commandSession, cmdCtxLogger, &cmdOutBuf) case PackageCommandType: var remoteHostPackageExecutor RemoteHostPackageExecutor + injectEnvIntoSSH(envVars, commandSession, opts, cmdCtxLogger) return remoteHostPackageExecutor.RunCmdOnHost(command, commandSession, cmdCtxLogger, cmdOutBuf) default: if command.Shell != "" { @@ -501,6 +499,12 @@ func (command *Command) RunCmdOnHost(cmdCtxLogger zerolog.Logger, opts *ConfigOp } cmdCtxLogger.Debug().Str("cmd + args", ArgsStr).Send() + //! environment vars and SSH: + //? skip if commandType is not *script*? + //? option to use SSH setenv or add to beginning? + // Inject environment variables + injectEnvIntoSSH(envVars, commandSession, opts, cmdCtxLogger) + if command.Type == UserCommandType && command.UserOperation == "password" { // cmdCtxLogger.Debug().Msgf("adding stdin") @@ -673,6 +677,11 @@ func (command *Command) runScriptFile(session *ssh.Session, cmdCtxLogger zerolog func (command *Command) prepareScriptBuffer() (*bytes.Buffer, error) { var buffer bytes.Buffer + for _, envVar := range command.Environment { + buffer.WriteString(fmt.Sprintf("export %s", envVar)) + buffer.WriteByte('\n') + } + if command.ScriptEnvFile != "" { envBuffer, err := readFileToBuffer(command.ScriptEnvFile) if err != nil { @@ -694,6 +703,11 @@ func (command *Command) prepareScriptBuffer() (*bytes.Buffer, error) { func (command *Command) prepareScriptFileBuffer() (*bytes.Buffer, error) { var buffer bytes.Buffer + for _, envVar := range command.Environment { + buffer.WriteString(fmt.Sprintf("export %s", envVar)) + buffer.WriteByte('\n') + } + // Handle script environment file if command.ScriptEnvFile != "" { envBuffer, err := readFileToBuffer(command.ScriptEnvFile)