mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
monitor: Move commands to a separated package
This commit moves monitor commands to `monior/commands` package. Commands still need access to the `monitor` object and buildx controller so this commit enables this via `Monitor` interface stored in `monitor/types`. Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
This commit is contained in:
76
monitor/commands/reload.go
Normal file
76
monitor/commands/reload.go
Normal file
@ -0,0 +1,76 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
controllererrors "github.com/docker/buildx/controller/errdefs"
|
||||
controllerapi "github.com/docker/buildx/controller/pb"
|
||||
"github.com/docker/buildx/monitor/types"
|
||||
"github.com/docker/buildx/util/progress"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ReloadCmd struct {
|
||||
m types.Monitor
|
||||
|
||||
stdout io.WriteCloser
|
||||
progress *progress.Printer
|
||||
|
||||
options *controllerapi.BuildOptions
|
||||
invokeConfig controllerapi.InvokeConfig
|
||||
}
|
||||
|
||||
func NewReloadCmd(m types.Monitor, stdout io.WriteCloser, progress *progress.Printer, options *controllerapi.BuildOptions, invokeConfig controllerapi.InvokeConfig) types.Command {
|
||||
return &ReloadCmd{m, stdout, progress, options, invokeConfig}
|
||||
}
|
||||
|
||||
func (cm *ReloadCmd) Info() types.CommandInfo {
|
||||
return types.CommandInfo{HelpMessage: "reloads the context and build it"}
|
||||
}
|
||||
|
||||
func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
|
||||
var bo *controllerapi.BuildOptions
|
||||
if cm.m.AttachedSessionID() != "" {
|
||||
// Rebuilding an existing session; Restore the build option used for building this session.
|
||||
res, err := cm.m.Inspect(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to inspect the current build session: %v\n", err)
|
||||
} else {
|
||||
bo = res.Options
|
||||
}
|
||||
} else {
|
||||
bo = cm.options
|
||||
}
|
||||
if bo == nil {
|
||||
return errors.Errorf("no build option is provided")
|
||||
}
|
||||
if cm.m.AttachedSessionID() != "" {
|
||||
if err := cm.m.Disconnect(ctx); err != nil {
|
||||
fmt.Println("disconnect error", err)
|
||||
}
|
||||
}
|
||||
var resultUpdated bool
|
||||
cm.progress.Unpause()
|
||||
ref, _, err := cm.m.Build(ctx, *bo, nil, cm.progress) // TODO: support stdin, hold build ref
|
||||
cm.progress.Pause()
|
||||
if err != nil {
|
||||
var be *controllererrors.BuildError
|
||||
if errors.As(err, &be) {
|
||||
ref = be.Ref
|
||||
resultUpdated = true
|
||||
} else {
|
||||
fmt.Printf("failed to reload: %v\n", err)
|
||||
}
|
||||
} else {
|
||||
resultUpdated = true
|
||||
}
|
||||
cm.m.AttachSession(ref)
|
||||
if resultUpdated {
|
||||
// rollback the running container with the new result
|
||||
id := cm.m.Rollback(ctx, cm.invokeConfig)
|
||||
fmt.Fprintf(cm.stdout, "Interactive container was restarted with process %q. Press Ctrl-a-c to switch to the new container\n", id)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user