Skip to content

Commit

Permalink
Merge getSSHPublicKey and sendSSHPublicKey functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoronin committed Nov 28, 2023
1 parent a4c6732 commit 4195fbb
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ func init() {
ec2InstanceConnectClient = ec2instanceconnect.New(sess)
}

func sendSSHPublicKey(instanceID, instanceOSUser, sshPublicKey string) {
func sendSSHPublicKey(instanceID, instanceOSUser, sshPublicKeyPath string) {
file, err := os.Open(sshPublicKeyPath)
if err != nil {
handleError(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Scan()
sshPublicKey := scanner.Text()
if err := scanner.Err(); err != nil {
handleError(err)
}

input := &ec2instanceconnect.SendSSHPublicKeyInput{
InstanceId: aws.String(instanceID),
InstanceOSUser: aws.String(instanceOSUser),
SSHPublicKey: aws.String(sshPublicKey),
}

_, err := ec2InstanceConnectClient.SendSSHPublicKey(input)
_, err = ec2InstanceConnectClient.SendSSHPublicKey(input)
if err != nil {
handleError(err)
}
Expand Down Expand Up @@ -98,22 +111,6 @@ func getEC2InstanceIDByFilter(filterName, filterValue string) string {
return ""
}

func getSSHPublicKey(sshPublicKeyPath string) string {
file, err := os.Open(sshPublicKeyPath)
if err != nil {
handleError(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Scan()
sshPublicKey := scanner.Text()
if err := scanner.Err(); err != nil {
handleError(err)
}
return sshPublicKey
}

func guessDestinationType(dst string) DstType {
if strings.HasPrefix(dst, "i-") {
return DstTypeID
Expand Down Expand Up @@ -180,8 +177,7 @@ func main() {
handleWarning("the option '--use-public-ip' is ignored since an IP address has been provided")
}

sshPublicKey := getSSHPublicKey(opts.sshPublicKeyPath)
sendSSHPublicKey(instanceID, opts.loginUser, sshPublicKey)
sendSSHPublicKey(instanceID, opts.loginUser, opts.sshPublicKeyPath)

cmd := exec.Command("ssh", sshArgs.Args()...)
cmd.Stdin = os.Stdin
Expand Down

0 comments on commit 4195fbb

Please sign in to comment.