github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/monitor/commands/rollback.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	controllerapi "github.com/docker/buildx/controller/pb"
     9  	"github.com/docker/buildx/monitor/types"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type RollbackCmd struct {
    14  	m types.Monitor
    15  
    16  	invokeConfig controllerapi.InvokeConfig
    17  	stdout       io.WriteCloser
    18  }
    19  
    20  func NewRollbackCmd(m types.Monitor, invokeConfig controllerapi.InvokeConfig, stdout io.WriteCloser) types.Command {
    21  	return &RollbackCmd{m, invokeConfig, stdout}
    22  }
    23  
    24  func (cm *RollbackCmd) Info() types.CommandInfo {
    25  	return types.CommandInfo{
    26  		Name:        "rollback",
    27  		HelpMessage: "re-runs the interactive container with the step's rootfs contents",
    28  		HelpMessageLong: `
    29  Usage:
    30    rollback [FLAGS] [COMMAND] [ARG...]
    31  
    32  Flags:
    33    --init Run the container with the initial rootfs of that step.
    34  
    35  COMMAND and ARG... will be executed in the container.
    36  `,
    37  	}
    38  }
    39  
    40  func (cm *RollbackCmd) Exec(ctx context.Context, args []string) error {
    41  	if ref := cm.m.AttachedSessionID(); ref == "" {
    42  		return errors.Errorf("no attaching session")
    43  	}
    44  	cfg := cm.invokeConfig
    45  	if len(args) >= 2 {
    46  		cmds := args[1:]
    47  		if cmds[0] == "--init" {
    48  			cfg.Initial = true
    49  			cmds = cmds[1:]
    50  		}
    51  		if len(cmds) > 0 {
    52  			cfg.Entrypoint = []string{cmds[0]}
    53  			cfg.Cmd = cmds[1:]
    54  			cfg.NoCmd = false
    55  		}
    56  	}
    57  	id := cm.m.Rollback(ctx, cfg)
    58  	fmt.Fprintf(cm.stdout, "Interactive container was restarted with process %q. Press Ctrl-a-c to switch to the new container\n", id)
    59  	return nil
    60  }