Skip to content

Commit

Permalink
add context to subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
damoon committed Feb 17, 2022
1 parent 21568fc commit cfbef7b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ func main() {
}

func verifyContext(c *cli.Context) error {
ctx := c.Context
allowContext := c.String(allowContext.Name)

allowed, err := d8s.ContextAllowed(allowContext)
allowed, err := d8s.ContextAllowed(ctx, allowContext)
if err != nil {
return fmt.Errorf("verify kubernetes context: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import (

func Down(ctx context.Context, allowContext string) error {
// verify kubernetes context in use
allowed, err := ContextAllowed(allowContext)
allowed, err := ContextAllowed(ctx, allowContext)
if err != nil {
return fmt.Errorf("verify kubernetes context: %v", err)
}
if !allowed {
return fmt.Errorf("kubernetes context not allowed")
}

err = deleteDind()
err = deleteDind(ctx)
if err != nil {
return fmt.Errorf("delete dind: %v", err)
}

return nil
}

func deleteDind() error {
cmd := exec.Command(
func deleteDind(ctx context.Context) error {
cmd := exec.CommandContext(
ctx,
"kubectl",
"delete",
"-f-",
Expand Down
10 changes: 6 additions & 4 deletions pkg/k8s-context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package d8s

import (
"context"
_ "embed"
"fmt"
"os"
Expand All @@ -13,8 +14,8 @@ var (
manifest string
)

func ContextAllowed(envVar string) (bool, error) {
contextName, err := kubectlContext()
func ContextAllowed(ctx context.Context, envVar string) (bool, error) {
contextName, err := kubectlContext(ctx)
if err != nil {
return false, err
}
Expand All @@ -30,8 +31,9 @@ func ContextAllowed(envVar string) (bool, error) {
return false, fmt.Errorf("context %s does not appear to be a development environment", contextName)
}

func kubectlContext() (string, error) {
cmd := exec.Command(
func kubectlContext(ctx context.Context) (string, error) {
cmd := exec.CommandContext(
ctx,
"kubectl",
"config",
"current-context",
Expand Down
21 changes: 13 additions & 8 deletions pkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func Run(ctx context.Context, allowContext string, command []string) error {
// verify kubernetes context in use
allowed, err := ContextAllowed(allowContext)
allowed, err := ContextAllowed(ctx, allowContext)
if err != nil {
return fmt.Errorf("verify kubernetes context: %v", err)
}
Expand All @@ -22,7 +22,7 @@ func Run(ctx context.Context, allowContext string, command []string) error {
}

// port forward
err = awaitDind()
err = awaitDind(ctx)
if err != nil {
return fmt.Errorf("wait for dind to start: %v", err)
}
Expand All @@ -40,16 +40,17 @@ func Run(ctx context.Context, allowContext string, command []string) error {
}

// execute command
err = executeCommand(command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
if err != nil {
return fmt.Errorf("command failed with %s", err)
}

return nil
}

func awaitDind() error {
cmd := exec.Command(
func awaitDind(ctx context.Context) error {
cmd := exec.CommandContext(
ctx,
"kubectl",
"wait",
"--for=condition=available",
Expand Down Expand Up @@ -104,7 +105,8 @@ func portForwardForever(ctx context.Context, localPort, dindPort int) {
}

func portForward(ctx context.Context, localPort, dinnerPort int) error {
cmd := exec.Command(
cmd := exec.CommandContext(
ctx,
"kubectl",
"port-forward",
"deployment/dind",
Expand Down Expand Up @@ -153,8 +155,11 @@ func portOpen(ctx context.Context, host string, port string) bool {
return true
}

func executeCommand(command []string, dockerAddr string) error {
cmd := exec.Command(command[0], command[1:]...)
func executeCommand(ctx context.Context, command []string, dockerAddr string) error {
cmd := exec.CommandContext(
ctx,
command[0],
command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
Expand Down
13 changes: 7 additions & 6 deletions pkg/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (

func Up(ctx context.Context, allowContext string, command []string) error {
// verify kubernetes context in use
allowed, err := ContextAllowed(allowContext)
allowed, err := ContextAllowed(ctx, allowContext)
if err != nil {
return fmt.Errorf("verify kubernetes context: %v", err)
}
Expand All @@ -23,13 +23,13 @@ func Up(ctx context.Context, allowContext string, command []string) error {
}

// deploy docker in docker
err = deployDind()
err = deployDind(ctx)
if err != nil {
return fmt.Errorf("deploy dind: %v", err)
}

// port forward
err = awaitDind()
err = awaitDind(ctx)
if err != nil {
return fmt.Errorf("wait for dind to start: %v", err)
}
Expand All @@ -47,16 +47,17 @@ func Up(ctx context.Context, allowContext string, command []string) error {
}

// execute command
err = executeCommand(command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
if err != nil {
return fmt.Errorf("command failed with %s", err)
}

return nil
}

func deployDind() error {
cmd := exec.Command(
func deployDind(ctx context.Context) error {
cmd := exec.CommandContext(
ctx,
"kubectl",
"apply",
"-f-",
Expand Down

0 comments on commit cfbef7b

Please sign in to comment.