Skip to content

Commit f96a080

Browse files
committed
Misc
1 parent 8671c31 commit f96a080

20 files changed

+306
-391
lines changed

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"go.lintTool": "golangci-lint",
33
"go.lintFlags": [
44
"--fast"
5-
]
5+
],
6+
"gopls.env": {
7+
"GOFLAGS": "-tags=darwin",
8+
}
69
}

cmd/ec2/ec2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func chooseInstance(instances []ec2.Instance) (*ec2.Instance, error) {
5555
templates := &promptui.SelectTemplates{
5656
Active: fmt.Sprintf(`%s {{ .Name | cyan | bold }} ({{ .ID }})`, promptui.IconSelect),
5757
Inactive: ` {{ .Name | cyan }} ({{ .ID }})`,
58-
Selected: fmt.Sprintf(`%s {{ "Instance" | bold }}: {{ .Name | cyan }} ({{ .ID }})`, promptui.IconGood),
58+
Selected: fmt.Sprintf(`%s {{ "Instance" }}: {{ .Name | cyan }} ({{ .ID }})`, promptui.IconGood),
5959
}
6060

6161
searcher := func(input string, index int) bool {

cmd/ecs/ecs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func chooseContainer(containers []ecs.Container) (string, string, error) {
5151
templates := &promptui.SelectTemplates{
5252
Active: fmt.Sprintf(`%s {{ .Name | cyan | bold }} ({{ .Task }})`, promptui.IconSelect),
5353
Inactive: ` {{ .Name | cyan }} ({{ .Task }})`,
54-
Selected: fmt.Sprintf(`%s {{ "Container" | bold }}: {{ .Name | cyan }} ({{ .Task }})`, promptui.IconGood),
54+
Selected: fmt.Sprintf(`%s {{ "Container" }}: {{ .Name | cyan }} ({{ .Task }})`, promptui.IconGood),
5555
}
5656

5757
searcher := func(input string, index int) bool {

cmd/eks/eks.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func chooseCluster(clusters []eks.Cluster) (*eks.Cluster, error) {
4949
templates := &promptui.SelectTemplates{
5050
Active: fmt.Sprintf(`%s {{ .Name | cyan | bold }} ({{ .Version }})`, promptui.IconSelect),
5151
Inactive: ` {{ .Name | cyan }} ({{ .Version }})`,
52-
Selected: fmt.Sprintf(`%s {{ "Cluster" | bold }}: {{ .Name | cyan }} ({{ .Version }})`, promptui.IconGood),
52+
Selected: fmt.Sprintf(`%s {{ "Cluster" }}: {{ .Name | cyan }} ({{ .Version }})`, promptui.IconGood),
5353
}
5454

5555
searcher := func(input string, index int) bool {
@@ -84,7 +84,7 @@ func findPod(cfg *config.Config, cluster *eks.Cluster, role, namespace, podName,
8484

8585
if podName != "" {
8686
if namespace == "" {
87-
internal.PrintInfo("No namspespace was specified. Set namespace to \"default\"")
87+
internal.PrintInfo("No namespace was specified. Set namespace to \"default\"")
8888

8989
namespace = "default"
9090
}
@@ -108,17 +108,15 @@ func findPod(cfg *config.Config, cluster *eks.Cluster, role, namespace, podName,
108108
return nil, err
109109
}
110110

111-
fmt.Println(pods)
112-
113111
return choosePod(pods)
114112
}
115113

116114
// nolint: dupl // ok
117115
func choosePod(pods []eks.Pod) (*eks.Pod, error) {
118116
templates := &promptui.SelectTemplates{
119-
Active: fmt.Sprintf(`%s {{ .Name | cyan | bold }} [{{ .Container }} ({{ .Namespace }})]`, promptui.IconSelect),
120-
Inactive: ` {{ .Name | cyan }} [{{ .Container }} ({{ .Namespace }})]`,
121-
Selected: fmt.Sprintf(`%s {{ "Pod" | bold }}: {{ .Name | cyan }} [{{ .Container }} ({{ .Namespace }})]`, promptui.IconGood),
117+
Active: fmt.Sprintf(`%s {{ "pod" | cyan | bold }}/{{ .Name | cyan | bold }}/{{ .Container | cyan | bold }} ({{ .Namespace }})`, promptui.IconSelect),
118+
Inactive: ` {{ "pod" | cyan }}/{{ .Name | cyan }}/{{ .Container | cyan }} ({{ .Namespace }})`,
119+
Selected: fmt.Sprintf(`%s {{ "Pod" }}: {{ "pod" | cyan }}/{{ .Name | cyan }}/{{ .Container | cyan }} ({{ .Namespace }})`, promptui.IconGood),
122120
}
123121

124122
searcher := func(input string, index int) bool {

cmd/eks/update_kubeconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newUpdateKubeconfigCmd() *cobra.Command {
4040
opts.alias = cluster.ARN
4141
}
4242

43-
kubeconfig.Update(cfg, cluster, opts.role, opts.alias)
43+
kubeconfig.Update(cfg, opts.alias, cluster, eks.NewExecConfig(cfg, cluster.Name, opts.role))
4444

4545
if err := kubeconfig.WriteToDisk(); err != nil {
4646
return err

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717

1818
require (
1919
github.com/aws/aws-sdk-go-v2/service/sts v1.16.6
20+
github.com/golang/mock v1.5.0
2021
k8s.io/api v0.24.1
2122
k8s.io/apimachinery v0.24.1
2223
)

go.sum

+1-300
Large diffs are not rendered by default.

internal/exec/cmd.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package exec
2+
3+
import (
4+
"io"
5+
"os"
6+
"os/exec"
7+
)
8+
9+
// CmdOption is a type alias to configure a command.
10+
type CmdOption func(cmd *exec.Cmd)
11+
12+
// Stdin sets the internal *exec.Cmd's Stdin field.
13+
func Stdin(r io.Reader) CmdOption {
14+
return func(c *exec.Cmd) {
15+
c.Stdin = r
16+
}
17+
}
18+
19+
// Stdout sets the internal *exec.Cmd's Stdout field.
20+
func Stdout(writer io.Writer) CmdOption {
21+
return func(c *exec.Cmd) {
22+
c.Stdout = writer
23+
}
24+
}
25+
26+
// Stderr sets the internal *exec.Cmd's Stderr field.
27+
func Stderr(writer io.Writer) CmdOption {
28+
return func(c *exec.Cmd) {
29+
c.Stderr = writer
30+
}
31+
}
32+
33+
type cmdRunner interface {
34+
Run() error
35+
}
36+
37+
// Cmd runs external commands, it wraps the exec.Command function from the stdlib so that
38+
// running external commands can be unit tested.
39+
type Cmd struct {
40+
command func(name string, args []string, opts ...CmdOption) cmdRunner
41+
}
42+
43+
// NewCmd returns a Cmd that can run external commands.
44+
func NewCmd() *Cmd {
45+
return &Cmd{
46+
command: func(name string, args []string, opts ...CmdOption) cmdRunner {
47+
cmd := exec.Command(name, args...)
48+
cmd.Stdout = os.Stdout
49+
cmd.Stderr = os.Stderr
50+
for _, opt := range opts {
51+
opt(cmd)
52+
}
53+
return cmd
54+
},
55+
}
56+
}
57+
58+
// Run starts the named command and waits until it finishes.
59+
func (c *Cmd) Run(name string, args []string, opts ...CmdOption) error {
60+
cmd := c.command(name, args, opts...)
61+
return cmd.Run()
62+
}

internal/exec/cmd_mock.go

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/exec/cmd_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package exec
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/golang/mock/gomock"
9+
)
10+
11+
func TestRun(t *testing.T) {
12+
t.Run("should delegate to exec and call Run", func(t *testing.T) {
13+
ctrl := gomock.NewController(t)
14+
defer ctrl.Finish()
15+
16+
cmd := &Cmd{
17+
command: func(name string, args []string, opts ...CmdOption) cmdRunner {
18+
assert.Equal(t, "date", name)
19+
20+
m := NewMockcmdRunner(ctrl)
21+
m.EXPECT().Run().Return(nil)
22+
23+
return m
24+
},
25+
}
26+
27+
err := cmd.Run("date", nil)
28+
29+
assert.NoError(t, err)
30+
})
31+
}

internal/exec/interactive_run.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package exec
5+
6+
import (
7+
"os"
8+
"os/signal"
9+
)
10+
11+
// InteractiveRun runs the input command that starts a child process.
12+
func (c *Cmd) InteractiveRun(name string, args ...string) error {
13+
// Ignore interrupt signal otherwise the program exits.
14+
signal.Ignore(os.Interrupt)
15+
defer signal.Reset(os.Interrupt)
16+
17+
cmd := c.command(name, args, Stdout(os.Stdout), Stdin(os.Stdin), Stderr(os.Stderr))
18+
19+
return cmd.Run()
20+
}

internal/exec/interactive_run_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package exec
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/golang/mock/gomock"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestInteractiveRun(t *testing.T) {
13+
t.Run("should enable default stdin, stdout, and stderr before running the command", func(t *testing.T) {
14+
// GIVEN
15+
ctrl := gomock.NewController(t)
16+
defer ctrl.Finish()
17+
18+
cmd := &Cmd{
19+
command: func(name string, args []string, opts ...CmdOption) cmdRunner {
20+
assert.Equal(t, "date", name)
21+
22+
// Make sure that the options applied are what we expect.
23+
cmd := &exec.Cmd{}
24+
for _, opt := range opts {
25+
opt(cmd)
26+
}
27+
28+
assert.Equal(t, os.Stdin, cmd.Stdin)
29+
assert.Equal(t, os.Stdout, cmd.Stdout)
30+
assert.Equal(t, os.Stderr, cmd.Stderr)
31+
32+
m := NewMockcmdRunner(ctrl)
33+
m.EXPECT().Run().Return(nil)
34+
35+
return m
36+
},
37+
}
38+
39+
// WHEN
40+
err := cmd.InteractiveRun("date")
41+
42+
// THEN
43+
assert.NoError(t, err)
44+
})
45+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package exec
5+
6+
import (
7+
"os"
8+
"os/signal"
9+
)
10+
11+
// InteractiveRun runs the input command that starts a child process.
12+
func (c *Cmd) InteractiveRun(name string, args ...string) error {
13+
sig := make(chan os.Signal, 1)
14+
15+
// See https://golang.org/pkg/os/signal/#hdr-Windows
16+
signal.Notify(sig, os.Interrupt)
17+
defer signal.Reset(os.Interrupt)
18+
19+
cmd := c.command(name, args, Stdout(os.Stdout), Stdin(os.Stdin), Stderr(os.Stderr))
20+
21+
return cmd.Run()
22+
}

internal/subprocess.go

-23
This file was deleted.
File renamed without changes.

pkg/ec2/session.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
aws_ssm "github.com/aws/aws-sdk-go-v2/service/ssm"
9-
"github.com/hupe1980/gotoaws/internal"
9+
"github.com/hupe1980/gotoaws/internal/exec"
1010
"github.com/hupe1980/gotoaws/pkg/config"
1111
"github.com/hupe1980/gotoaws/pkg/ssm"
1212
)
@@ -94,11 +94,9 @@ func (sess *session) RunSSH(input *RunSSHInput) error {
9494
}
9595
}
9696

97-
if err := internal.RunSubprocess("ssh", args...); err != nil {
98-
return err
99-
}
97+
cmd := exec.NewCmd()
10098

101-
return nil
99+
return cmd.InteractiveRun("ssh", args...)
102100
}
103101

104102
func (sess *session) RunSCP(input *RunSCPInput) error {
@@ -115,13 +113,9 @@ func (sess *session) RunSCP(input *RunSCPInput) error {
115113
}
116114
}
117115

118-
fmt.Println(strings.Join(args, " "))
119-
120-
if err := internal.RunSubprocess("scp", args...); err != nil {
121-
return err
122-
}
116+
cmd := exec.NewCmd()
123117

124-
return nil
118+
return cmd.InteractiveRun("scp", args...)
125119
}
126120

127121
func sshArgs(input *RunSSHInput) string {

0 commit comments

Comments
 (0)