Skip to content

Commit

Permalink
Merge pull request #6 from zbintliff/feature/interrupt
Browse files Browse the repository at this point in the history
Capture and Pass through SIGTERM and SIGINT
  • Loading branch information
rothgar committed Mar 23, 2020
2 parents 2500e81 + 1b5608a commit f772c32
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cmd/ssm-session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"

"github.com/AlecAivazis/survey/v2"
"github.com/aws/aws-sdk-go/service/ssm"
Expand Down Expand Up @@ -297,13 +299,40 @@ func setSessionStatus(sessionName string, status string) (err error) {
return rawCmd.Run()
}

func startSSMSession(profile string, region string, instanceID string) (err error) {
func startSSMSession(profile string, region string, instanceID string) error {
rawCmd := exec.Command("aws", "ssm", "start-session", "--profile", profile, "--region", region, "--target", instanceID)
rawCmd.Stdin = os.Stdin
rawCmd.Stdout = os.Stdout
rawCmd.Stderr = os.Stderr

return rawCmd.Run()
err := rawCmd.Start()
if err != nil {
return err
}

// Set up to capture Ctrl+C
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

doneChan := make(chan struct{}, 2)

// Run Wait() in its own chan so we don't block
go func() {
err = rawCmd.Wait()
doneChan <- struct{}{}
}()
// Here we block until command is done
for {
select {
case s := <-sigChan:
// user typed Ctrl-C, most likley meant for ssm-session pass through
rawCmd.Process.Signal(s)
case <-doneChan:
// command is done
return err
}
}
return err
}

func attachTmuxSession(sessionName string) (err error) {
Expand Down

0 comments on commit f772c32

Please sign in to comment.