|
| 1 | +package copy |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/brevdev/brev-cli/pkg/cmd/cmderrors" |
| 12 | + "github.com/brevdev/brev-cli/pkg/cmd/completions" |
| 13 | + "github.com/brevdev/brev-cli/pkg/cmd/refresh" |
| 14 | + "github.com/brevdev/brev-cli/pkg/cmd/util" |
| 15 | + "github.com/brevdev/brev-cli/pkg/entity" |
| 16 | + breverrors "github.com/brevdev/brev-cli/pkg/errors" |
| 17 | + "github.com/brevdev/brev-cli/pkg/store" |
| 18 | + "github.com/brevdev/brev-cli/pkg/terminal" |
| 19 | + "github.com/brevdev/brev-cli/pkg/writeconnectionevent" |
| 20 | + "github.com/briandowns/spinner" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + copyLong = "Copy files and directories between your local machine and remote instance" |
| 27 | + copyExample = "brev copy instance_name:/path/to/remote/file /path/to/local/file\nbrev copy /path/to/local/file instance_name:/path/to/remote/file\nbrev copy ./local-directory/ instance_name:/remote/path/" |
| 28 | +) |
| 29 | + |
| 30 | +type CopyStore interface { |
| 31 | + util.GetWorkspaceByNameOrIDErrStore |
| 32 | + refresh.RefreshStore |
| 33 | + GetOrganizations(options *store.GetOrganizationsOptions) ([]entity.Organization, error) |
| 34 | + GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error) |
| 35 | + StartWorkspace(workspaceID string) (*entity.Workspace, error) |
| 36 | + GetWorkspace(workspaceID string) (*entity.Workspace, error) |
| 37 | + GetCurrentUserKeys() (*entity.UserKeys, error) |
| 38 | +} |
| 39 | + |
| 40 | +func NewCmdCopy(t *terminal.Terminal, store CopyStore, noLoginStartStore CopyStore) *cobra.Command { |
| 41 | + var host bool |
| 42 | + cmd := &cobra.Command{ |
| 43 | + Annotations: map[string]string{"ssh": ""}, |
| 44 | + Use: "copy", |
| 45 | + Aliases: []string{"cp", "scp"}, |
| 46 | + DisableFlagsInUseLine: true, |
| 47 | + Short: "copy files and directories between local and remote instance", |
| 48 | + Long: copyLong, |
| 49 | + Example: copyExample, |
| 50 | + Args: cmderrors.TransformToValidationError(cobra.ExactArgs(2)), |
| 51 | + ValidArgsFunction: completions.GetAllWorkspaceNameCompletionHandler(noLoginStartStore, t), |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + err := runCopyCommand(t, store, args[0], args[1], host) |
| 54 | + if err != nil { |
| 55 | + return breverrors.WrapAndTrace(err) |
| 56 | + } |
| 57 | + return nil |
| 58 | + }, |
| 59 | + } |
| 60 | + cmd.Flags().BoolVarP(&host, "host", "", false, "copy to/from the host machine instead of the container") |
| 61 | + |
| 62 | + return cmd |
| 63 | +} |
| 64 | + |
| 65 | +func runCopyCommand(t *terminal.Terminal, cstore CopyStore, source, dest string, host bool) error { |
| 66 | + workspaceNameOrID, remotePath, localPath, isUpload, err := parseCopyArguments(source, dest) |
| 67 | + if err != nil { |
| 68 | + return breverrors.WrapAndTrace(err) |
| 69 | + } |
| 70 | + |
| 71 | + if isUpload { |
| 72 | + err = validateLocalFile(localPath) |
| 73 | + if err != nil { |
| 74 | + return breverrors.WrapAndTrace(err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + workspace, err := prepareWorkspace(t, cstore, workspaceNameOrID) |
| 79 | + if err != nil { |
| 80 | + return breverrors.WrapAndTrace(err) |
| 81 | + } |
| 82 | + |
| 83 | + sshName, err := setupSSHConnection(t, cstore, workspace, host) |
| 84 | + if err != nil { |
| 85 | + return breverrors.WrapAndTrace(err) |
| 86 | + } |
| 87 | + |
| 88 | + _ = writeconnectionevent.WriteWCEOnEnv(cstore, workspace.DNS) |
| 89 | + |
| 90 | + err = runSCP(t, sshName, localPath, remotePath, isUpload) |
| 91 | + if err != nil { |
| 92 | + return breverrors.WrapAndTrace(err) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func parseCopyArguments(source, dest string) (workspaceNameOrID, remotePath, localPath string, isUpload bool, err error) { |
| 99 | + sourceWorkspace, sourcePath, err := parseWorkspacePath(source) |
| 100 | + if err != nil { |
| 101 | + return "", "", "", false, err |
| 102 | + } |
| 103 | + |
| 104 | + destWorkspace, destPath, err := parseWorkspacePath(dest) |
| 105 | + if err != nil { |
| 106 | + return "", "", "", false, err |
| 107 | + } |
| 108 | + |
| 109 | + if (sourceWorkspace == "" && destWorkspace == "") || (sourceWorkspace != "" && destWorkspace != "") { |
| 110 | + return "", "", "", false, breverrors.NewValidationError("exactly one of source or destination must be an instance path (format: instance_name:/path)") |
| 111 | + } |
| 112 | + |
| 113 | + if sourceWorkspace != "" { |
| 114 | + return sourceWorkspace, sourcePath, dest, false, nil |
| 115 | + } |
| 116 | + return destWorkspace, destPath, source, true, nil |
| 117 | +} |
| 118 | + |
| 119 | +func prepareWorkspace(t *terminal.Terminal, cstore CopyStore, workspaceNameOrID string) (*entity.Workspace, error) { |
| 120 | + s := t.NewSpinner() |
| 121 | + workspace, err := util.GetUserWorkspaceByNameOrIDErr(cstore, workspaceNameOrID) |
| 122 | + if err != nil { |
| 123 | + return nil, breverrors.WrapAndTrace(err) |
| 124 | + } |
| 125 | + |
| 126 | + if workspace.Status == "STOPPED" { |
| 127 | + err = startWorkspaceIfStopped(t, s, cstore, workspaceNameOrID, workspace) |
| 128 | + if err != nil { |
| 129 | + return nil, breverrors.WrapAndTrace(err) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + err = pollUntil(s, workspace.ID, "RUNNING", cstore, " waiting for instance to be ready...") |
| 134 | + if err != nil { |
| 135 | + return nil, breverrors.WrapAndTrace(err) |
| 136 | + } |
| 137 | + |
| 138 | + workspace, err = util.GetUserWorkspaceByNameOrIDErr(cstore, workspaceNameOrID) |
| 139 | + if err != nil { |
| 140 | + return nil, breverrors.WrapAndTrace(err) |
| 141 | + } |
| 142 | + if workspace.Status != "RUNNING" { |
| 143 | + return nil, breverrors.New("Workspace is not running") |
| 144 | + } |
| 145 | + |
| 146 | + return workspace, nil |
| 147 | +} |
| 148 | + |
| 149 | +func setupSSHConnection(t *terminal.Terminal, cstore CopyStore, workspace *entity.Workspace, host bool) (string, error) { |
| 150 | + refreshRes := refresh.RunRefreshAsync(cstore) |
| 151 | + |
| 152 | + localIdentifier := workspace.GetLocalIdentifier() |
| 153 | + if host { |
| 154 | + localIdentifier = workspace.GetHostIdentifier() |
| 155 | + } |
| 156 | + |
| 157 | + sshName := string(localIdentifier) |
| 158 | + |
| 159 | + err := refreshRes.Await() |
| 160 | + if err != nil { |
| 161 | + return "", breverrors.WrapAndTrace(err) |
| 162 | + } |
| 163 | + |
| 164 | + s := t.NewSpinner() |
| 165 | + err = waitForSSHToBeAvailable(sshName, s) |
| 166 | + if err != nil { |
| 167 | + return "", breverrors.WrapAndTrace(err) |
| 168 | + } |
| 169 | + |
| 170 | + return sshName, nil |
| 171 | +} |
| 172 | + |
| 173 | +func validateLocalFile(localPath string) error { |
| 174 | + _, err := os.Stat(localPath) |
| 175 | + if err != nil { |
| 176 | + if os.IsNotExist(err) { |
| 177 | + return breverrors.NewValidationError(fmt.Sprintf("local file or directory does not exist: %s", localPath)) |
| 178 | + } |
| 179 | + return breverrors.WrapAndTrace(fmt.Errorf("cannot access local file or directory %s: %w", localPath, err)) |
| 180 | + } |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func isDirectory(path string) bool { |
| 185 | + info, err := os.Stat(path) |
| 186 | + if err != nil { |
| 187 | + return false |
| 188 | + } |
| 189 | + return info.IsDir() |
| 190 | +} |
| 191 | + |
| 192 | +func parseWorkspacePath(path string) (workspace, filePath string, err error) { |
| 193 | + if !strings.Contains(path, ":") { |
| 194 | + return "", path, nil |
| 195 | + } |
| 196 | + |
| 197 | + parts := strings.Split(path, ":") |
| 198 | + if len(parts) != 2 { |
| 199 | + return "", "", breverrors.NewValidationError("invalid instance path format, use instance_name:/path") |
| 200 | + } |
| 201 | + |
| 202 | + return parts[0], parts[1], nil |
| 203 | +} |
| 204 | + |
| 205 | +func runSCP(t *terminal.Terminal, sshAlias, localPath, remotePath string, isUpload bool) error { |
| 206 | + var scpCmd *exec.Cmd |
| 207 | + var source, dest string |
| 208 | + |
| 209 | + startTime := time.Now() |
| 210 | + |
| 211 | + scpArgs := []string{"scp"} |
| 212 | + |
| 213 | + if isUpload { |
| 214 | + if isDirectory(localPath) { |
| 215 | + scpArgs = append(scpArgs, "-r") |
| 216 | + } |
| 217 | + scpArgs = append(scpArgs, localPath, fmt.Sprintf("%s:%s", sshAlias, remotePath)) |
| 218 | + source = localPath |
| 219 | + dest = fmt.Sprintf("%s:%s", sshAlias, remotePath) |
| 220 | + } else { |
| 221 | + scpArgs = append(scpArgs, "-r") |
| 222 | + scpArgs = append(scpArgs, fmt.Sprintf("%s:%s", sshAlias, remotePath), localPath) |
| 223 | + source = fmt.Sprintf("%s:%s", sshAlias, remotePath) |
| 224 | + dest = localPath |
| 225 | + } |
| 226 | + |
| 227 | + scpCmd = exec.Command(scpArgs[0], scpArgs[1:]...) //nolint:gosec //sshAlias is validated workspace identifier |
| 228 | + |
| 229 | + output, err := scpCmd.CombinedOutput() |
| 230 | + if err != nil { |
| 231 | + return breverrors.WrapAndTrace(fmt.Errorf("scp failed: %s\nOutput: %s", err.Error(), string(output))) |
| 232 | + } |
| 233 | + |
| 234 | + duration := time.Since(startTime) |
| 235 | + fmt.Print("\n") |
| 236 | + t.Vprint(t.Green(fmt.Sprintf("✓ Successfully copied %s → %s (%v)\n", source, dest, duration.Round(time.Millisecond)))) |
| 237 | + |
| 238 | + return nil |
| 239 | +} |
| 240 | + |
| 241 | +func waitForSSHToBeAvailable(sshAlias string, s *spinner.Spinner) error { |
| 242 | + counter := 0 |
| 243 | + s.Suffix = " waiting for SSH connection to be available" |
| 244 | + s.Start() |
| 245 | + for { |
| 246 | + cmd := exec.Command("ssh", "-o", "ConnectTimeout=10", sshAlias, "echo", " ") |
| 247 | + out, err := cmd.CombinedOutput() |
| 248 | + if err == nil { |
| 249 | + s.Stop() |
| 250 | + return nil |
| 251 | + } |
| 252 | + |
| 253 | + outputStr := string(out) |
| 254 | + stdErr := strings.Split(outputStr, "\n")[1] |
| 255 | + |
| 256 | + if counter == 40 || !store.SatisfactorySSHErrMessage(stdErr) { |
| 257 | + return breverrors.WrapAndTrace(errors.New("\n" + stdErr)) |
| 258 | + } |
| 259 | + |
| 260 | + counter++ |
| 261 | + time.Sleep(1 * time.Second) |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +func startWorkspaceIfStopped(t *terminal.Terminal, s *spinner.Spinner, tstore CopyStore, wsIDOrName string, workspace *entity.Workspace) error { |
| 266 | + activeOrg, err := tstore.GetActiveOrganizationOrDefault() |
| 267 | + if err != nil { |
| 268 | + return breverrors.WrapAndTrace(err) |
| 269 | + } |
| 270 | + workspaces, err := tstore.GetWorkspaceByNameOrID(activeOrg.ID, wsIDOrName) |
| 271 | + if err != nil { |
| 272 | + return breverrors.WrapAndTrace(err) |
| 273 | + } |
| 274 | + startedWorkspace, err := tstore.StartWorkspace(workspaces[0].ID) |
| 275 | + if err != nil { |
| 276 | + return breverrors.WrapAndTrace(err) |
| 277 | + } |
| 278 | + t.Vprintf(t.Yellow("Instance %s is starting. \n\n", startedWorkspace.Name)) |
| 279 | + err = pollUntil(s, workspace.ID, entity.Running, tstore, " hang tight 🤙") |
| 280 | + if err != nil { |
| 281 | + return breverrors.WrapAndTrace(err) |
| 282 | + } |
| 283 | + workspace, err = util.GetUserWorkspaceByNameOrIDErr(tstore, wsIDOrName) |
| 284 | + if err != nil { |
| 285 | + return breverrors.WrapAndTrace(err) |
| 286 | + } |
| 287 | + return nil |
| 288 | +} |
| 289 | + |
| 290 | +func pollUntil(s *spinner.Spinner, wsid string, state string, copyStore CopyStore, waitMsg string) error { |
| 291 | + isReady := false |
| 292 | + s.Suffix = waitMsg |
| 293 | + s.Start() |
| 294 | + for !isReady { |
| 295 | + time.Sleep(5 * time.Second) |
| 296 | + ws, err := copyStore.GetWorkspace(wsid) |
| 297 | + if err != nil { |
| 298 | + s.Stop() |
| 299 | + return breverrors.WrapAndTrace(err) |
| 300 | + } |
| 301 | + s.Suffix = waitMsg |
| 302 | + if ws.Status == state { |
| 303 | + isReady = true |
| 304 | + } |
| 305 | + } |
| 306 | + s.Stop() |
| 307 | + return nil |
| 308 | +} |
0 commit comments