From 370afbec5cd57321305b17ff9ea282a9d17df4b9 Mon Sep 17 00:00:00 2001 From: Alexander Neishkasha Date: Wed, 15 Jan 2020 22:20:31 +0200 Subject: [PATCH] Refactored. Add ssh facade --- aws_facade.go | 2 ++ main.go | 60 ++-------------------------------------------- ssh_facade.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 58 deletions(-) create mode 100644 ssh_facade.go diff --git a/aws_facade.go b/aws_facade.go index b3e77c6..6d374f7 100644 --- a/aws_facade.go +++ b/aws_facade.go @@ -9,6 +9,8 @@ import ( "log" ) +const RunningCode = 16 + type AwsFacade struct { region string env string diff --git a/main.go b/main.go index 9f4d3a0..6700277 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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() } diff --git a/ssh_facade.go b/ssh_facade.go new file mode 100644 index 0000000..e83f1a3 --- /dev/null +++ b/ssh_facade.go @@ -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") + } +}