mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 21:47:13 +08:00
controller: move controllers out of commands into separate package
Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
79
controller/local/controller.go
Normal file
79
controller/local/controller.go
Normal file
@ -0,0 +1,79 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/docker/buildx/build"
|
||||
cbuild "github.com/docker/buildx/controller/build"
|
||||
"github.com/docker/buildx/controller/control"
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/cli/cli/command"
|
||||
)
|
||||
|
||||
func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli) control.BuildxController {
|
||||
return &localController{
|
||||
dockerCli: dockerCli,
|
||||
ref: "local",
|
||||
}
|
||||
}
|
||||
|
||||
type localController struct {
|
||||
dockerCli command.Cli
|
||||
ref string
|
||||
resultCtx *build.ResultContext
|
||||
}
|
||||
|
||||
func (b *localController) Invoke(ctx context.Context, ref string, cfg controllerapi.ContainerConfig, ioIn io.ReadCloser, ioOut io.WriteCloser, ioErr io.WriteCloser) error {
|
||||
if ref != b.ref {
|
||||
return fmt.Errorf("unknown ref %q", ref)
|
||||
}
|
||||
if b.resultCtx == nil {
|
||||
return fmt.Errorf("no build result is registered")
|
||||
}
|
||||
ccfg := build.ContainerConfig{
|
||||
ResultCtx: b.resultCtx,
|
||||
Entrypoint: cfg.Entrypoint,
|
||||
Cmd: cfg.Cmd,
|
||||
Env: cfg.Env,
|
||||
Tty: cfg.Tty,
|
||||
Stdin: ioIn,
|
||||
Stdout: ioOut,
|
||||
Stderr: ioErr,
|
||||
}
|
||||
if !cfg.NoUser {
|
||||
ccfg.User = &cfg.User
|
||||
}
|
||||
if !cfg.NoCwd {
|
||||
ccfg.Cwd = &cfg.Cwd
|
||||
}
|
||||
return build.Invoke(ctx, ccfg)
|
||||
}
|
||||
|
||||
func (b *localController) Build(ctx context.Context, options controllerapi.BuildOptions, in io.ReadCloser, w io.Writer, out console.File, progressMode string) (string, error) {
|
||||
res, err := cbuild.RunBuild(ctx, b.dockerCli, options, in, progressMode, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b.resultCtx = res
|
||||
return b.ref, nil
|
||||
}
|
||||
|
||||
func (b *localController) Kill(context.Context) error {
|
||||
return nil // nop
|
||||
}
|
||||
|
||||
func (b *localController) Close() error {
|
||||
// TODO: cancel current build and invoke
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *localController) List(ctx context.Context) (res []string, _ error) {
|
||||
return []string{b.ref}, nil
|
||||
}
|
||||
|
||||
func (b *localController) Disconnect(ctx context.Context, key string) error {
|
||||
return nil // nop
|
||||
}
|
Reference in New Issue
Block a user