github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/sharedaction/ssh.go (about)

     1  package sharedaction
     2  
     3  import "code.cloudfoundry.org/cli/util/clissh"
     4  
     5  type TTYOption clissh.TTYRequest
     6  
     7  const (
     8  	RequestTTYAuto TTYOption = iota
     9  	RequestTTYNo
    10  	RequestTTYYes
    11  	RequestTTYForce
    12  )
    13  
    14  type LocalPortForward clissh.LocalPortForward
    15  
    16  type SSHOptions struct {
    17  	Commands              []string
    18  	Username              string
    19  	Passcode              string
    20  	Endpoint              string
    21  	HostKeyFingerprint    string
    22  	SkipHostValidation    bool
    23  	SkipRemoteExecution   bool
    24  	TTYOption             TTYOption
    25  	LocalPortForwardSpecs []LocalPortForward
    26  }
    27  
    28  func (actor Actor) ExecuteSecureShell(sshClient SecureShellClient, sshOptions SSHOptions) error {
    29  	err := sshClient.Connect(sshOptions.Username, sshOptions.Passcode, sshOptions.Endpoint, sshOptions.HostKeyFingerprint, sshOptions.SkipHostValidation)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer sshClient.Close()
    34  
    35  	err = sshClient.LocalPortForward(convertActorToSSHPackageForwardingSpecs(sshOptions.LocalPortForwardSpecs))
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if sshOptions.SkipRemoteExecution {
    41  		err = sshClient.Wait()
    42  	} else {
    43  		err = sshClient.InteractiveSession(sshOptions.Commands, clissh.TTYRequest(sshOptions.TTYOption))
    44  	}
    45  	return err
    46  }
    47  
    48  func convertActorToSSHPackageForwardingSpecs(actorSpecs []LocalPortForward) []clissh.LocalPortForward {
    49  	sshPackageSpecs := []clissh.LocalPortForward{}
    50  
    51  	for _, spec := range actorSpecs {
    52  		sshPackageSpecs = append(sshPackageSpecs, clissh.LocalPortForward(spec))
    53  	}
    54  
    55  	return sshPackageSpecs
    56  }