예제 #1
0
func (c *Command) runSync() error {
	sshKey, err := c.getSSHKey()
	if err != nil {
		return err
	}

	remoteUsername, err := sshKey.GetUsername(c.Options.MountName)
	if err != nil {
		c.Stdout.Printlnf(errormessages.FailedGetSSHKey)
		return fmt.Errorf("Error getting remote username. err:%s", err)
	}

	// UX not needed on failure, prepareForSSH prints UX to user.
	if err := c.prepareForSSH(sshKey); err != nil {
		return err
	}

	c.Stdout.Printlnf(
		"Downloading initial contents...Please don't interrupt this process while in progress.",
	)

	sshAuthSock, err := agent.NewClient().GetAuthSock()
	if err != nil || sshAuthSock == "" {
		sshAuthSock = util.GetEnvByKey(os.Environ(), "SSH_AUTH_SOCK")
	}

	cacheReq := req.Cache{
		Debug:             c.Options.Debug,
		Name:              c.Options.MountName,
		LocalPath:         c.mountInfo.LocalPath,
		RemotePath:        c.mountInfo.RemotePath,
		LocalToRemote:     c.Options.SyncDirection == localToRemote,
		Interval:          0, // No interval! Important.
		Username:          remoteUsername,
		SSHAuthSock:       sshAuthSock,
		SSHPrivateKeyPath: sshKey.PrivateKeyPath(),
	}

	cb, err := mount.NewCacheCallback(mount.CacheCallbackInit{
		Log:    c.Log,
		Stdout: c.Stdout,
	})
	if err != nil {
		c.Stdout.Printlnf(errormessages.GenericInternalErrorNoMsg)
		return err
	}

	// UX not needed on error, remoteCache handles that.
	//
	// Note that because we used a callback, this is async - updates are sent to the
	// callback and progress bar. We'll wait and block, below.
	if err := c.callRemoteCache(cacheReq, cb.Callback); err != nil {
		return err
	}

	// Wait until the async callback is done, then check for an error.
	if err := cb.WaitUntilDone(); err != nil {
		c.Stdout.Printlnf(
			c.HealthChecker.CheckAllFailureOrMessagef(errormessages.FailedSyncFolder),
		)
		return fmt.Errorf("remote.cacheFolder returned an error. err:%s", err)
	}

	c.Stdout.Printlnf("Sync complete.")

	return nil
}
예제 #2
0
func (c *Command) runCp() error {
	// parse the user input Source and Destination, obtaining the remote and local
	// paths, machine name, etc.
	//
	// This provides UX on error!
	machineName, localPath, remotePath, localToRemote, err := c.parseSourceAndDest()
	if err != nil {
		return err
	}

	// Get the machineName from a shortcut, if any was used.
	//
	// This provides UX on error!
	if machineName, err = c.machineFromShortcut(machineName); err != nil {
		return err
	}

	if localPath, err = filepath.Abs(localPath); err != nil {
		c.Stdout.Printlnf(errormessages.GenericInternalErrorNoMsg)
		return err
	}

	c.Log.Debug(
		"Parsed source and dest. machineName:%s, localPath:%s, remotePath%s, localToRemote:%t",
		machineName, localPath, remotePath, localToRemote,
	)

	// This provides UX!
	sshKey, err := c.getSSHKey()
	if err != nil {
		return err
	}

	remoteUsername, err := sshKey.GetUsername(machineName)
	if err != nil {
		c.Stdout.Printlnf(errormessages.FailedGetSSHKey)
		return fmt.Errorf("Error getting remote username. err:%s", err)
	}

	// UX not needed on failure, prepareForSSH prints UX to user.
	if err := c.prepareForSSH(machineName, sshKey); err != nil {
		return err
	}

	c.Stdout.Printlnf(
		"Copying file or directory...",
	)

	sshAuthSock, err := agent.NewClient().GetAuthSock()
	if err != nil || sshAuthSock == "" {
		sshAuthSock = util.GetEnvByKey(os.Environ(), "SSH_AUTH_SOCK")
	}

	cacheReq := req.Cache{
		Debug:             c.Options.Debug,
		Name:              machineName,
		LocalPath:         localPath,
		RemotePath:        remotePath,
		LocalToRemote:     localToRemote,
		Interval:          0, // No interval! Important.
		Username:          remoteUsername,
		SSHAuthSock:       sshAuthSock,
		SSHPrivateKeyPath: sshKey.PrivateKeyPath(),
		// This allows the user to copy files, not just dirs.
		// See option docs for details.
		IncludePath: true,
	}

	cb, err := mount.NewCacheCallback(mount.CacheCallbackInit{
		Log:    c.Log,
		Stdout: c.Stdout,
	})
	if err != nil {
		c.Stdout.Printlnf(errormessages.GenericInternalErrorNoMsg)
		return err
	}

	// UX not needed on error, remoteCache handles that.
	//
	// Note that because we used a callback, this is async - updates are sent to the
	// callback and progress bar. We'll wait and block, below.
	if err := c.callRemoteCache(cacheReq, cb.Callback); err != nil {
		return err
	}

	// Wait until the async callback is done, then check for an error.
	if err := cb.WaitUntilDone(); err != nil {
		c.Stdout.Printlnf(
			c.HealthChecker.CheckAllFailureOrMessagef(errormessages.FailedSyncFolder),
		)
		return fmt.Errorf("remote.cacheFolder returned an error. err:%s", err)
	}

	c.Stdout.Printlnf("Copy complete.")

	return nil
}