Skip to content

Commit

Permalink
cli: improve output of update command
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Oct 30, 2024
1 parent bd9b569 commit 338fa15
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
17 changes: 16 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,22 @@ func (c *colimaApp) Update() error {
return err
}

return container.Update(ctx)
oldVersion := container.Version(ctx)

updated, err := container.Update(ctx)
if err != nil {
return err
}

if updated {
fmt.Println("previous version")
fmt.Println(oldVersion)
fmt.Println()
fmt.Println("current version")
fmt.Println(container.Version(ctx))
}

return nil
}

func generateSSHConfig(modifySSHConfig bool) error {
Expand Down
2 changes: 1 addition & 1 deletion environment/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Container interface {
// Teardown tears down/uninstall the container runtime.
Teardown(ctx context.Context) error
// Update the container runtime.
Update(ctx context.Context) error
Update(ctx context.Context) (bool, error)
// Version returns the container runtime version.
Version(ctx context.Context) string
// Running returns if the container runtime is currently running.
Expand Down
4 changes: 2 additions & 2 deletions environment/container/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ func (c containerdRuntime) Version(ctx context.Context) string {
return version
}

func (c *containerdRuntime) Update(ctx context.Context) error {
return fmt.Errorf("update not supported for the %s runtime", Name)
func (c *containerdRuntime) Update(ctx context.Context) (bool, error) {
return false, fmt.Errorf("update not supported for the %s runtime", Name)
}
2 changes: 1 addition & 1 deletion environment/container/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (d dockerRuntime) Version(ctx context.Context) string {
return version
}

func (d *dockerRuntime) Update(ctx context.Context) error {
func (d *dockerRuntime) Update(ctx context.Context) (bool, error) {
packages := []string{
"docker-ce",
"docker-ce-cli",
Expand Down
2 changes: 1 addition & 1 deletion environment/container/incus/incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ type networkInfo struct {
Type string `json:"type"`
}

func (c *incusRuntime) Update(ctx context.Context) error {
func (c *incusRuntime) Update(ctx context.Context) (bool, error) {
packages := []string{
"incus",
"incus-base",
Expand Down
5 changes: 2 additions & 3 deletions environment/container/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ func (c kubernetesRuntime) Version(context.Context) string {
return version
}

func (c *kubernetesRuntime) Update(ctx context.Context) error {
// update not supported
return nil
func (c *kubernetesRuntime) Update(ctx context.Context) (bool, error) {
return false, fmt.Errorf("update not supported for the %s runtime", Name)
}
14 changes: 9 additions & 5 deletions util/debutil/debutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func UpdateRuntime(
chain cli.CommandChain,
runtime string,
packageNames ...string,
) error {
) (bool, error) {
a := chain.Init(ctx)
log := a.Logger()

packages := packages(packageNames)

updatesAvailable := false
hasUpdates := false
updated := false

a.Stage("refreshing package manager")
a.Add(func() error {
Expand All @@ -58,12 +59,12 @@ func UpdateRuntime(
"-c",
packages.Upgradable(),
)
updatesAvailable = err == nil
hasUpdates = err == nil
return nil
})

a.Add(func() (err error) {
if !updatesAvailable {
if !hasUpdates {
log.Warnf("no updates available for %s runtime", runtime)
return
}
Expand All @@ -75,10 +76,13 @@ func UpdateRuntime(
packages.Install(),
)
if err == nil {
updated = true
log.Println("done")
}
return
})

return a.Exec()
// it is necessary to execute the chain here to get the correct value for `updated`.
err := a.Exec()
return updated, err
}

0 comments on commit 338fa15

Please sign in to comment.