-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathupgrade.go
More file actions
121 lines (97 loc) · 2.95 KB
/
upgrade.go
File metadata and controls
121 lines (97 loc) · 2.95 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package commands
import (
"context"
"errors"
"fmt"
"github.com/89luca89/distrobox/pkg/config"
"github.com/89luca89/distrobox/pkg/containermanager"
"github.com/89luca89/distrobox/pkg/ui"
)
//nolint:lll // upgrade command mirrors the shell version
const upgradeScript = `command -v su-exec 2>/dev/null && su-exec root /usr/bin/entrypoint --upgrade || command -v doas 2>/dev/null && doas /usr/bin/entrypoint --upgrade || sudo -S /usr/bin/entrypoint --upgrade`
type UpgradeOptions struct {
ContainerNames []string
All bool
Running bool
NonInteractive bool
}
type UpgradeCommand struct {
cfg *config.Values
containerManager containermanager.ContainerManager
listCmd *ListCommand
enterCmd *EnterCommand
prompter *ui.Prompter
}
var ErrUpgradeAbortedByUser = errors.New("upgrade operation aborted by user")
func NewUpgradeCommand(
cfg *config.Values,
cm containermanager.ContainerManager,
progress *ui.Progress,
printer *ui.Printer,
prompter *ui.Prompter,
) *UpgradeCommand {
return &UpgradeCommand{
cfg: cfg,
containerManager: cm,
listCmd: NewListCommand(cfg, cm),
enterCmd: NewEnterCommand(cfg, cm, progress, printer),
prompter: prompter,
}
}
func (c *UpgradeCommand) Execute(ctx context.Context, opts *UpgradeOptions) error {
var containerNames []string
switch {
case opts.All, opts.Running:
containers, err := c.listCmd.Execute(ctx, nil)
if err != nil {
return fmt.Errorf("failed to list containers: %w", err)
}
if len(containers.Containers) == 0 {
return ErrEmptyContainerList
}
containerNames = make([]string, 0, len(containers.Containers))
for _, container := range containers.Containers {
if opts.Running && !container.IsRunning() {
continue
}
containerNames = append(containerNames, container.Name)
}
if len(containerNames) == 0 {
return ErrEmptyContainerList
}
case len(opts.ContainerNames) > 0:
containerNames = opts.ContainerNames
default:
containerNames = []string{c.cfg.DefaultContainerName}
}
proceed := opts.NonInteractive || c.canProceed(containerNames)
if !proceed {
return ErrUpgradeAbortedByUser
}
var lastErr error
for _, name := range containerNames {
if err := c.upgradeContainer(ctx, name); err != nil {
//nolint:forbidigo // FIXME: waiting for the logger implementation
fmt.Printf("error upgrading %s: %s\n", name, err)
lastErr = err
continue
}
}
return lastErr
}
func (c *UpgradeCommand) upgradeContainer(ctx context.Context, name string) error {
enterOpts := EnterOptions{
ContainerName: name,
CustomCommand: []string{"sh", "-c", upgradeScript},
}
if _, err := c.enterCmd.Execute(ctx, enterOpts); err != nil {
return fmt.Errorf("failed to upgrade container %s: %w", name, err)
}
return nil
}
func (c *UpgradeCommand) canProceed(containerNames []string) bool {
return c.prompter.Prompt(
fmt.Sprintf("Do you really want to upgrade %s?", containerNames),
true,
)
}