|
5 | 5 | "io" |
6 | 6 | "os" |
7 | 7 | "path/filepath" |
| 8 | + "strings" |
8 | 9 |
|
9 | 10 | "github.com/pkg/sftp" |
10 | 11 | "github.com/raghavyuva/nixopus-api/internal/features/ssh" |
@@ -108,10 +109,36 @@ var actionHandlers = map[string]fileAction{ |
108 | 109 | "mkdir": handleMkdir, |
109 | 110 | } |
110 | 111 |
|
| 112 | +// expandTilde expands ~ to the actual home directory path for SFTP compatibility |
| 113 | +func expandTilde(path string, sshClient *ssh.SSH) string { |
| 114 | + if len(path) > 0 && path[0] == '~' { |
| 115 | + if len(path) == 1 || path[1] == '/' { |
| 116 | + // Get home directory via command |
| 117 | + homeOutput, err := sshClient.RunCommand("echo $HOME") |
| 118 | + if err == nil && len(homeOutput) > 0 { |
| 119 | + home := strings.TrimSpace(homeOutput) |
| 120 | + if len(path) == 1 { |
| 121 | + return home |
| 122 | + } |
| 123 | + return home + path[1:] |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return path |
| 128 | +} |
| 129 | + |
111 | 130 | func (fileModule) Execute(sshClient *ssh.SSH, step types.SpecStep, vars map[string]interface{}) (string, func(), error) { |
112 | 131 | action, _ := step.Properties["action"].(string) |
113 | | - src, _ := step.Properties["src"].(string) |
114 | | - dest, _ := step.Properties["dest"].(string) |
| 132 | + srcRaw, _ := step.Properties["src"].(string) |
| 133 | + destRaw, _ := step.Properties["dest"].(string) |
| 134 | + |
| 135 | + // Replace variables in src and dest paths |
| 136 | + src := replaceVars(srcRaw, vars) |
| 137 | + dest := replaceVars(destRaw, vars) |
| 138 | + |
| 139 | + // Expand tilde to $HOME for SFTP compatibility |
| 140 | + src = expandTilde(src, sshClient) |
| 141 | + dest = expandTilde(dest, sshClient) |
115 | 142 |
|
116 | 143 | if action == "mkdir" && dest == "" { |
117 | 144 | return "", nil, fmt.Errorf("dest is required for mkdir action") |
|
0 commit comments