File: reload.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (89 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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/moby/buildkit/solver/errdefs"
	"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{
		Name:        "reload",
		HelpMessage: "reloads the context and build it",
		HelpMessageLong: `
Usage:
  reload
`,
	}
}

func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
	var bo *controllerapi.BuildOptions
	if ref := cm.m.AttachedSessionID(); ref != "" {
		// Rebuilding an existing session; Restore the build option used for building this session.
		res, err := cm.m.Inspect(ctx, ref)
		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 ref := cm.m.AttachedSessionID(); ref != "" {
		if err := cm.m.Disconnect(ctx, ref); 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)
		}
		// report error
		for _, s := range errdefs.Sources(err) {
			s.Print(cm.stdout)
		}
		fmt.Fprintf(cm.stdout, "ERROR: %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
}