Adds CommandContext to the Cmder interface

Signed-off-by: Chuck Ha <chuckh@vmware.com>
This commit is contained in:
Chuck Ha
2020-02-26 14:37:12 -05:00
parent 52ca6ab1cd
commit 3851627c7a
6 changed files with 68 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package docker
import (
"context"
"io"
"sigs.k8s.io/kind/pkg/exec"
@@ -42,6 +43,15 @@ func (c *containerCmder) Command(command string, args ...string) exec.Cmd {
}
}
func (c *containerCmder) CommandContext(ctx context.Context, command string, args ...string) exec.Cmd {
return &containerCmd{
nameOrID: c.nameOrID,
command: command,
args: args,
ctx: ctx,
}
}
// containerCmd implements exec.Cmd for docker containers
type containerCmd struct {
nameOrID string // the container name or ID
@@ -51,6 +61,7 @@ type containerCmd struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
ctx context.Context
}
func (c *containerCmd) Run() error {
@@ -82,7 +93,12 @@ func (c *containerCmd) Run() error {
// finally, with the caller args
c.args...,
)
cmd := exec.Command("docker", args...)
var cmd exec.Cmd
if c.ctx != nil {
cmd = exec.CommandContext(c.ctx, "docker", args...)
} else {
cmd = exec.Command("docker", args...)
}
if c.stdin != nil {
cmd.SetStdin(c.stdin)
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package docker
import (
"context"
"fmt"
"io"
"strings"
@@ -77,6 +78,15 @@ func (n *node) Command(command string, args ...string) exec.Cmd {
}
}
func (n *node) CommandContext(ctx context.Context, command string, args ...string) exec.Cmd {
return &nodeCmd{
nameOrID: n.name,
command: command,
args: args,
ctx: ctx,
}
}
// nodeCmd implements exec.Cmd for docker nodes
type nodeCmd struct {
nameOrID string // the container name or ID
@@ -86,6 +96,7 @@ type nodeCmd struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
ctx context.Context
}
func (c *nodeCmd) Run() error {
@@ -117,7 +128,12 @@ func (c *nodeCmd) Run() error {
// finally, with the caller args
c.args...,
)
cmd := exec.Command("docker", args...)
var cmd exec.Cmd
if c.ctx != nil {
cmd = exec.CommandContext(c.ctx, "docker", args...)
} else {
cmd = exec.Command("docker", args...)
}
if c.stdin != nil {
cmd.SetStdin(c.stdin)
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package podman
import (
"context"
"fmt"
"io"
"strings"
@@ -77,6 +78,15 @@ func (n *node) Command(command string, args ...string) exec.Cmd {
}
}
func (n *node) CommandContext(ctx context.Context, command string, args ...string) exec.Cmd {
return &nodeCmd{
nameOrID: n.name,
command: command,
args: args,
ctx: ctx,
}
}
// nodeCmd implements exec.Cmd for podman nodes
type nodeCmd struct {
nameOrID string // the container name or ID
@@ -86,6 +96,7 @@ type nodeCmd struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
ctx context.Context
}
func (c *nodeCmd) Run() error {
@@ -117,7 +128,12 @@ func (c *nodeCmd) Run() error {
// finally, with the caller args
c.args...,
)
cmd := exec.Command("podman", args...)
var cmd exec.Cmd
if c.ctx != nil {
cmd = exec.CommandContext(c.ctx, "podman", args...)
} else {
cmd = exec.Command("podman", args...)
}
if c.stdin != nil {
cmd.SetStdin(c.stdin)
}

View File

@@ -16,6 +16,8 @@ limitations under the License.
package exec
import "context"
// DefaultCmder is a LocalCmder instance used for convenience, packages
// originally using os/exec.Command can instead use pkg/kind/exec.Command
// which forwards to this instance
@@ -27,3 +29,8 @@ var DefaultCmder = &LocalCmder{}
func Command(command string, args ...string) Cmd {
return DefaultCmder.Command(command, args...)
}
// CommandContext is a convenience wrapper over DefaultCmder.CommandContext
func CommandContext(ctx context.Context, command string, args ...string) Cmd {
return DefaultCmder.CommandContext(ctx, command, args...)
}

View File

@@ -18,6 +18,7 @@ package exec
import (
"bytes"
"context"
"io"
osexec "os/exec"
"sync"
@@ -44,6 +45,13 @@ func (c *LocalCmder) Command(name string, arg ...string) Cmd {
}
}
// CommandContext is like Command but includes a context
func (c *LocalCmder) CommandContext(ctx context.Context, name string, arg ...string) Cmd {
return &LocalCmd{
Cmd: osexec.CommandContext(ctx, name, arg...),
}
}
// SetEnv sets env
func (cmd *LocalCmd) SetEnv(env ...string) Cmd {
cmd.Env = env

View File

@@ -17,6 +17,7 @@ limitations under the License.
package exec
import (
"context"
"fmt"
"io"
)
@@ -37,6 +38,7 @@ type Cmd interface {
type Cmder interface {
// command, args..., just like os/exec.Cmd
Command(string, ...string) Cmd
CommandContext(context.Context, string, ...string) Cmd
}
// RunError represents an error running a Cmd