ftp

package
v1.13.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CpCmd = &cobra.Command{
	Use:   "cp [SOURCE...] [DESTINATION]",
	Short: "Copy files between local and remote locations",
	Long: `Copy files between your local machine and a remote server.
Supports SSH-like user@host:path syntax for specifying the username inline with the remote path.
Remote paths use the format [USER@]SERVER:/path.
All inline users must name the same user; -u overrides them all.

Use --work-session to attach this transfer to a specific work-session. This
overrides the workspace's active work-session set via 'alpacon work-session use'.

Exit code 3 indicates a WorkSession gate denial; run with --output json to
parse a machine-readable diagnostic on stderr.
Requires an active WorkSession when using Browser login (Auth0); Token auth (API token or Service token) bypasses this requirement.`,
	Example: `  # Upload files to a remote server
  alpacon cp /local/file1.txt /local/file2.txt my-server:/remote/path/

  # Upload or download a directory
  alpacon cp -r /local/directory my-server:/remote/path/
  alpacon cp -r my-server:/remote/directory /local/path/

  # Download a file from a remote server
  alpacon cp my-server:/remote/file.txt /local/path/

  # Specify username with SSH-like syntax
  alpacon cp /local/file.txt admin@my-server:/remote/path/
  alpacon cp -r admin@my-server:/var/log/ /local/logs/

  # Specify username with flag
  alpacon cp -u admin /local/file.txt my-server:/remote/path/

  # Specify groupname
  alpacon cp -g developers /local/file.txt my-server:/remote/path/

  # Upload without overwriting existing files
  alpacon cp --no-overwrite /local/file.txt my-server:/remote/path/

  # Attach the transfer to a work-session
  alpacon cp /local/file.txt my-server:/remote/path/ --work-session 11111111-2222-3333-4444-555555555555`,
	Run: func(cmd *cobra.Command, args []string) {
		username, _ := cmd.Flags().GetString("username")
		groupname, _ := cmd.Flags().GetString("groupname")
		recursive, _ := cmd.Flags().GetBool("recursive")
		noOverwrite, _ := cmd.Flags().GetBool("no-overwrite")
		flagWorkSession, _ := cmd.Flags().GetString("work-session")
		allowOverwrite := !noOverwrite

		if len(args) < 2 {
			utils.CliErrorWithExit("You must specify at least two arguments.\n\n" +
				"Usage examples:\n" +
				"  • Upload file to server:\n" +
				"    alpacon cp /local/file.txt server:/remote/path/\n" +
				"  • Download file from server:\n" +
				"    alpacon cp server:/remote/file.txt /local/path/\n" +
				"  • Upload folder (recursive):\n" +
				"    alpacon cp -r /local/folder server:/remote/path/\n\n" +
				"Note: Remote paths must include server name (e.g., myserver:/path/)")
			return
		}

		username, err := normalizeArgs(args, username)
		if err != nil {
			utils.CliErrorWithExit("%v", err)
			return
		}

		sources := args[:len(args)-1]
		dest := args[len(args)-1]

		if err := validatePaths(sources, dest); err != nil {
			utils.CliErrorWithExit("%s", err.Error())
			return
		}

		workSessionID := worksession.ResolveOrExit(flagWorkSession)

		authMethod := config.ResolveAuthMethod()

		alpaconClient, err := client.NewAlpaconAPIClient()
		if err != nil {
			utils.CliErrorWithExit("Connection to Alpacon API failed: %s.\n\n"+
				"Try these solutions:\n"+
				"  • Re-login with 'alpacon login'\n"+
				"  • Check your internet connection\n"+
				"  • Verify the API endpoint is accessible", err)
			return
		}

		if isLocalPaths(sources) && isRemotePath(dest) {
			serverName, err := uploadObject(alpaconClient, sources, dest, username, groupname, recursive, allowOverwrite, workSessionID)
			if err != nil {
				err = utils.HandleCommonErrors(err, serverName, mfa.ErrorCallbacks(alpaconClient, func() error {
					_, err := uploadObject(alpaconClient, sources, dest, username, groupname, recursive, allowOverwrite, workSessionID)
					return err
				}))

				if err != nil {
					utils.HandleWorkSessionError(err, "webftp", serverName, authMethod, workSessionID)
					utils.CliErrorWithExit("Failed to upload to '%s': %s", dest, err)
					return
				}
			}
			wrappedSrc := fmt.Sprintf("[%s]", strings.Join(sources, ", "))
			utils.CliSuccess("Uploaded %s to %s", wrappedSrc, dest)
		} else if isRemotePath(sources[0]) && isLocalPath(dest) {
			serverName, err := downloadObject(alpaconClient, sources, dest, username, groupname, recursive, workSessionID)
			if err != nil {
				err = utils.HandleCommonErrors(err, serverName, mfa.ErrorCallbacks(alpaconClient, func() error {
					_, err := downloadObject(alpaconClient, sources, dest, username, groupname, recursive, workSessionID)
					return err
				}))

				if err != nil {
					utils.HandleWorkSessionError(err, "webftp", serverName, authMethod, workSessionID)
					wrappedSrc := fmt.Sprintf("[%s]", strings.Join(sources, ", "))
					utils.CliErrorWithExit("Failed to download from '%s': %s", wrappedSrc, err)
					return
				}
			}
			wrappedSrc := fmt.Sprintf("[%s]", strings.Join(sources, ", "))
			utils.CliSuccess("Downloaded %s to %s", wrappedSrc, dest)
		} else {
			utils.CliErrorWithExit("Invalid combination of source and destination paths.\n\n" +
				"Valid operations:\n" +
				"  • Upload (local → remote): alpacon cp /local/file server:/remote/path/\n" +
				"  • Download (remote → local): alpacon cp server:/remote/file /local/path/\n\n" +
				"Note: Remote paths must be in format 'servername:/path' (e.g., myserver:/tmp/file.txt)")
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL