Skip to content

Commit

Permalink
cmdio/ctr: Always execute with standard input enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
lesiw committed Oct 13, 2024
1 parent a85c161 commit dd79312
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
11 changes: 9 additions & 2 deletions ctr/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (c *cmd) Attach() error {
return nil
}

func (c *cmd) Close() error {
if cl, ok := c.ReadWriter.(io.Closer); ok {
return cl.Close()
}
return nil
}

func (c *cmd) String() string {
if s, ok := c.ReadWriter.(fmt.Stringer); ok {
return s.String()
Expand All @@ -45,9 +52,9 @@ func (c *cmd) String() string {
}

func (c *cmd) setCmd(attach bool) {
cmd := []string{"container", "exec"}
cmd := []string{"container", "exec", "-i"}
if attach {
cmd = append(cmd, "-ti")
cmd = append(cmd, "-t")
}
if c.env["PWD"] != "" {
cmd = append(cmd, "-w", c.env["PWD"])
Expand Down
2 changes: 1 addition & 1 deletion ctr/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestString(t *testing.T) {
defer rnr.Close()

cmd := rnr.Command("echo", "hello world")
str := fmt.Sprintf("docker container exec %s echo 'hello world'",
str := fmt.Sprintf("docker container exec -i %s echo 'hello world'",
rnr.Commander.(*cdr).ctrid)
if got, want := fmt.Sprintf("%v", cmd), str; got != want {
t.Errorf("Sprintf(cmd) = %q, want = %q", got, want)
Expand Down
29 changes: 24 additions & 5 deletions runner_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cmdio_test
import (
"context"
"errors"
"io"
"reflect"
"testing"
"time"

"lesiw.io/cmdio"
"lesiw.io/cmdio/ctr"
Expand All @@ -18,7 +18,13 @@ var runners = map[string]*cmdio.Runner{
}

func TestRunners(t *testing.T) {
cmdio.Trace = io.Discard
t.Cleanup(func() {
for _, rnr := range runners {
if err := rnr.Close(); err != nil {
panic(err)
}
}
})
for name, rnr := range runners {
suite := reflect.TypeOf(rnrtests{})
for i := 0; i < suite.NumMethod(); i++ {
Expand All @@ -31,14 +37,27 @@ func TestRunners(t *testing.T) {
})
})
}
if err := rnr.Close(); err != nil {
t.Fatal(err)
}
}
}

type rnrtests struct{}

func (rnrtests) TestPipe(t *testing.T, rnr *cmdio.Runner) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
rnr = rnr.WithContext(ctx)
r, err := cmdio.GetPipe(
rnr.Command("echo", "hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
if err != nil {
t.Error(err)
}
if got, want := r.Out, "HELLO WORLD"; got != want {
t.Errorf("[echo hello world] | [tr a-z A-Z] = %q, want %q", got, want)
}
cancel()
}

func (rnrtests) TestPipeToCompletedCommand(t *testing.T, rnr *cmdio.Runner) {
err := cmdio.Pipe(
rnr.Command("sleep", "0.001"),
Expand Down

0 comments on commit dd79312

Please sign in to comment.