Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Proof-of-Concept #974

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,14 @@ tasks:
cmds:
- echo '{{.GO_PACKAGES}}'
silent: true

d:
container:
image: alpine
flags: [--cpus=1]
env:
MESSAGE: Hello
cmds:
- echo $MESSAGE 1
- echo $MESSAGE 2
- echo $MESSAGE 3
53 changes: 53 additions & 0 deletions internal/container/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package docker

import (
"context"
"fmt"
"io"

"github.com/go-task/task/v3/internal/execext"
)

type Docker struct {
DockerCli string
Image string
Flags []string
Env map[string]string

Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}

func (d *Docker) Setup() error {
if d.DockerCli == "" {
d.DockerCli = "docker"
}
return nil
}

func (d *Docker) Exec(ctx context.Context, cmd string) error {
dockerArgs := []string{"run"}

for k, v := range d.Env {
dockerArgs = append(dockerArgs, "--env", fmt.Sprintf("%s=%s", k, v))
}

dockerArgs = append(dockerArgs, d.Flags...)
dockerArgs = append(dockerArgs, d.Image, "sh", "-c", cmd)

shCmd, err := execext.Build(d.DockerCli, dockerArgs...)
if err != nil {
return err
}

fmt.Printf("DOCKER: %s\n", shCmd)

return execext.RunCommand(ctx, &execext.RunCommandOptions{
Command: shCmd,
Dir: "", // TODO(@andreynering): Implement this
Stdin: d.Stdin,
Stdout: d.Stdout,
Stderr: d.Stderr,
})
}
22 changes: 22 additions & 0 deletions internal/execext/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package execext

import (
"fmt"
"strings"

"mvdan.cc/sh/v3/syntax"
)

func Build(cmd string, args ...string) (string, error) {
quotedArgs := make([]string, 0, len(args))

for _, arg := range args {
quoted, err := syntax.Quote(arg, syntax.LangBash)
if err != nil {
return "", err
}
quotedArgs = append(quotedArgs, quoted)
}

return fmt.Sprintf("%s %s", cmd, strings.Join(quotedArgs, " ")), nil
}
16 changes: 16 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/go-task/task/v3/internal/compiler"
"github.com/go-task/task/v3/internal/container/docker"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/output"
Expand Down Expand Up @@ -127,6 +128,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
if err != nil {
return err
}

if !e.Watch && atomic.AddInt32(e.taskCallCount[t.Task], 1) >= MaximumTaskCall {
return &MaximumTaskCallExceededError{task: t.Task}
}
Expand Down Expand Up @@ -251,6 +253,20 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
return err
}
return nil
case t.Container != nil:
d := docker.Docker{
Image: t.Container.Image,
Env: t.Env.ToStringMap(),
Flags: t.Container.Flags,

Stdin: e.Stdin,
Stdout: e.Stdout,
Stderr: e.Stderr,
}
if err := d.Setup(); err != nil {
return err
}
return d.Exec(ctx, cmd.Cmd)
case cmd.Cmd != "":
if e.Verbose || (!cmd.Silent && !t.Silent && !e.Taskfile.Silent && !e.Silent) {
e.Logger.Errf(logger.Green, "task: [%s] %s", t.Name(), cmd.Cmd)
Expand Down
7 changes: 7 additions & 0 deletions taskfile/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package taskfile

type Container struct {
Type string
Image string
Flags []string
}
3 changes: 3 additions & 0 deletions taskfile/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Task struct {
Prefix string
IgnoreError bool
Run string
Container *Container
IncludeVars *Vars
IncludedTaskfileVars *Vars
IncludedTaskfile *IncludedTaskfile
Expand Down Expand Up @@ -90,6 +91,7 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
Prefix string
IgnoreError bool `yaml:"ignore_error"`
Run string
Container *Container
}
if err := node.Decode(&task); err != nil {
return err
Expand All @@ -115,6 +117,7 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
t.Prefix = task.Prefix
t.IgnoreError = task.IgnoreError
t.Run = task.Run
t.Container = task.Container
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Taskfile struct {
Dotenv []string
Run string
Interval time.Duration
Container *Container
}

func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
Expand All @@ -41,6 +42,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
Dotenv []string
Run string
Interval time.Duration
Container *Container
}
if err := node.Decode(&taskfile); err != nil {
return err
Expand All @@ -57,6 +59,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
tf.Dotenv = taskfile.Dotenv
tf.Run = taskfile.Run
tf.Interval = taskfile.Interval
tf.Container = taskfile.Container
if tf.Expansions <= 0 {
tf.Expansions = 2
}
Expand Down
15 changes: 15 additions & 0 deletions taskfile/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (vs *Vars) Range(yield func(key string, value Var) error) error {
// variables
func (vs *Vars) ToCacheMap() (m map[string]interface{}) {
m = make(map[string]interface{}, vs.Len())

_ = vs.Range(func(k string, v Var) error {
if v.Sh != "" {
// Dynamic variable is not yet resolved; trigger
Expand All @@ -98,6 +99,20 @@ func (vs *Vars) ToCacheMap() (m map[string]interface{}) {
}
return nil
})

return
}

func (vs *Vars) ToStringMap() (m map[string]string) {
m = make(map[string]string, vs.Len())

_ = vs.Range(func(k string, v Var) error {
if v.Static != "" {
m[k] = v.Static
}
return nil
})

return
}

Expand Down
1 change: 1 addition & 0 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
Run: r.Replace(origTask.Run),
IncludeVars: origTask.IncludeVars,
IncludedTaskfileVars: origTask.IncludedTaskfileVars,
Container: origTask.Container,
}
new.Dir, err = execext.Expand(new.Dir)
if err != nil {
Expand Down