diff options
Diffstat (limited to 'cmd/ship/host/ssh.go')
| -rw-r--r-- | cmd/ship/host/ssh.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cmd/ship/host/ssh.go b/cmd/ship/host/ssh.go new file mode 100644 index 0000000..e480e47 --- /dev/null +++ b/cmd/ship/host/ssh.go | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | package host | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "os" | ||
| 6 | "os/exec" | ||
| 7 | |||
| 8 | "github.com/bdw/ship/internal/state" | ||
| 9 | "github.com/spf13/cobra" | ||
| 10 | ) | ||
| 11 | |||
| 12 | var sshCmd = &cobra.Command{ | ||
| 13 | Use: "ssh", | ||
| 14 | Short: "Open interactive SSH session", | ||
| 15 | RunE: runSSH, | ||
| 16 | } | ||
| 17 | |||
| 18 | func runSSH(cmd *cobra.Command, args []string) error { | ||
| 19 | st, err := state.Load() | ||
| 20 | if err != nil { | ||
| 21 | return fmt.Errorf("error loading state: %w", err) | ||
| 22 | } | ||
| 23 | |||
| 24 | host, _ := cmd.Flags().GetString("host") | ||
| 25 | if host == "" { | ||
| 26 | host = st.GetDefaultHost() | ||
| 27 | } | ||
| 28 | |||
| 29 | if host == "" { | ||
| 30 | return fmt.Errorf("--host is required (no default host set)") | ||
| 31 | } | ||
| 32 | |||
| 33 | sshCmd := exec.Command("ssh", host) | ||
| 34 | sshCmd.Stdin = os.Stdin | ||
| 35 | sshCmd.Stdout = os.Stdout | ||
| 36 | sshCmd.Stderr = os.Stderr | ||
| 37 | |||
| 38 | if err := sshCmd.Run(); err != nil { | ||
| 39 | if exitErr, ok := err.(*exec.ExitError); ok { | ||
| 40 | os.Exit(exitErr.ExitCode()) | ||
| 41 | } | ||
| 42 | return err | ||
| 43 | } | ||
| 44 | return nil | ||
| 45 | } | ||
