Skip to content

Commit

Permalink
Refactored. Add ssh facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Neishkasha committed Jan 15, 2020
1 parent 6dde730 commit 370afbe
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
2 changes: 2 additions & 0 deletions aws_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"log"
)

const RunningCode = 16

type AwsFacade struct {
region string
env string
Expand Down
60 changes: 2 additions & 58 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package main

import (
"errors"
"github.com/creack/pty"
"github.com/joho/godotenv"
"golang.org/x/crypto/ssh/terminal"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)

const RunningCode = 16

func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
Expand All @@ -25,53 +16,6 @@ func main() {
if err != nil {
log.Fatal(err)
}
pathToKey, err := pathToKey(env)
if err != nil {
log.Fatal(err)
}
runSsh(ip, pathToKey)
}

func pathToKey(environment string) (key string, err error) {
if environment == "stg" {
return os.Getenv("STAGE_KEY"), nil
} else if environment == "prod" {
return os.Getenv("PROD_KEY"), nil
} else {
return "", errors.New("Key for env '" + environment + "' not found")
}
}

func runSsh(ip string, pathToKey string) {
cmd := exec.Command("ssh", "-i"+pathToKey, "ubuntu@"+ip)
// Start the command with a pty.
ptmx, err := pty.Start(cmd)
if err != nil {
log.Fatal(err)
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.

// Handle pty size.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Printf("error resizing pty: %s", err)
}
}
}()
ch <- syscall.SIGWINCH // Initial resize.

// Set stdin in raw mode.
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

// Copy stdin to the pty and the pty to stdout.
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx)
ssh := SshFacade{ip, env}
ssh.Connect()
}
66 changes: 66 additions & 0 deletions ssh_facade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"errors"
"github.com/creack/pty"
"golang.org/x/crypto/ssh/terminal"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)

type SshFacade struct {
ip string
env string
}

func (ssh *SshFacade) Connect() {
pathToKey, err := ssh.pathToKey()
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("ssh", "-i"+pathToKey, "ubuntu@"+ssh.ip)
// Start the command with a pty.
ptmx, err := pty.Start(cmd)
if err != nil {
log.Fatal(err)
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.

// Handle pty size.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Printf("error resizing pty: %s", err)
}
}
}()
ch <- syscall.SIGWINCH // Initial resize.

// Set stdin in raw mode.
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

// Copy stdin to the pty and the pty to stdout.
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx)
}

func (ssh *SshFacade) pathToKey() (key string, err error) {
if ssh.env == "stg" {
return os.Getenv("STAGE_KEY"), nil
} else if ssh.env == "prod" {
return os.Getenv("PROD_KEY"), nil
} else {
return "", errors.New("Key for env '" + ssh.env + "' not found")
}
}

0 comments on commit 370afbe

Please sign in to comment.