ftp

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 9 Imported by: 0

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: `The cp command allows you to copy files between your local machine and a remote server.
	Copy files between your local machine and a remote server using the cp command.
	This command supports uploading, downloading, and specifying authentication details
	such as username and groupname.
	
	Example usages:
	- To upload multiple files to a remote server:
	  alpacon cp /local/path/file1.txt /local/path/file2.txt [SERVER_NAME]:/remote/path/

	- To upload or download directory:
	  alpacon cp -r /local/path/directory [SERVER_NAME]:/remote/path/
	  alpacon cp -r [SERVER_NAME]:/remote/path/directory /local/path/

	- To download files from a remote server to a local destination:
	  alpacon cp [SERVER_NAME]:/remote/path1 /remote/path2 /local/destination/path

	- To specify username:
	  alpacon cp /local/path/file.txt [USER_NAME]@[SERVER_NAME]:/remote/path/
	  alpacon cp -u [USER_NAME] /local/path/file.txt [SERVER_NAME]:/remote/path/

	- To specify groupname:
	  alpacon cp -g [GROUP_NAME] /local/path/file.txt [SERVER_NAME]:/remote/path/
	`,
	Run: func(cmd *cobra.Command, args []string) {
		username, _ := cmd.Flags().GetString("username")
		groupname, _ := cmd.Flags().GetString("groupname")
		recursive, _ := cmd.Flags().GetBool("recursive")

		if len(args) < 2 {
			utils.CliError("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
		}

		for i, arg := range args {
			if strings.Contains(arg, "@") && (strings.Contains(arg, ":") || !utils.IsRemoteTarget(arg)) {

				sshTarget := utils.ParseSSHTarget(arg)
				if username == "" && sshTarget.User != "" {
					username = sshTarget.User
				}

				if sshTarget.Path != "" {
					args[i] = sshTarget.Host + ":" + sshTarget.Path
				} else {
					args[i] = sshTarget.Host
				}
			}
		}

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

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

		alpaconClient, err := client.NewAlpaconAPIClient()
		if err != nil {
			utils.CliError("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) {
			err := uploadObject(alpaconClient, sources, dest, username, groupname, recursive)
			if err != nil {
				code, _ := utils.ParseErrorResponse(err)
				if code == utils.CodeAuthMFARequired {
					serverName, _ := utils.SplitPath(dest)
					err := mfa.HandleMFAError(alpaconClient, serverName)
					if err != nil {
						utils.CliError("MFA authentication failed: %s", err)
					}
					for {
						fmt.Println("Waiting for MFA authentication...")
						time.Sleep(5 * time.Second)

						err := uploadObject(alpaconClient, sources, dest, username, groupname, recursive)
						if err == nil {
							fmt.Println("MFA authentication has been completed!")
							break
						}
					}
				} else {

					return
				}
			}
		} else if isRemotePath(sources[0]) && isLocalPath(dest) {
			err := downloadObject(alpaconClient, sources[0], dest, username, groupname, recursive)
			if err != nil {
				code, _ := utils.ParseErrorResponse(err)
				if code == utils.CodeAuthMFARequired {
					serverName, _ := utils.SplitPath(sources[0])
					err := mfa.HandleMFAError(alpaconClient, serverName)
					if err != nil {
						utils.CliError("MFA authentication failed: %s", err)
					}
					for {
						fmt.Println("Waiting for MFA authentication...")
						time.Sleep(5 * time.Second)

						err := downloadObject(alpaconClient, sources[0], dest, username, groupname, recursive)
						if err == nil {
							fmt.Println("MFA authentication has been completed!")
							break
						}
					}
				} else {

					return
				}
			}
		} else {
			utils.CliError("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