File: uninstall.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (64 lines) | stat: -rw-r--r-- 1,631 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
package commands

import (
	"os"

	"github.com/docker/buildx/util/cobrautil"
	"github.com/docker/buildx/util/cobrautil/completion"
	"github.com/docker/cli/cli"
	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/config"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

type uninstallOptions struct {
}

func runUninstall(dockerCli command.Cli, in uninstallOptions) error {
	dir := config.Dir()
	cfg, err := config.Load(dir)
	if err != nil {
		return errors.Wrap(err, "could not load docker config to uninstall 'docker builder' alias")
	}
	// config.Load does not return an error if config file does not exist
	// so let's detect that case, to avoid writing an empty config to disk.
	if _, err := os.Stat(cfg.Filename); err != nil {
		if !os.IsNotExist(err) {
			// should never happen, already handled in config.Load
			return errors.Wrap(err, "unexpected error loading docker config")
		}
		// no-op
		return nil
	}

	delete(cfg.Aliases, "builder")
	if len(cfg.Aliases) == 0 {
		cfg.Aliases = nil
	}

	if err := cfg.Save(); err != nil {
		return errors.Wrap(err, "could not write docker config")
	}
	return nil
}

func uninstallCmd(dockerCli command.Cli) *cobra.Command {
	var options uninstallOptions

	cmd := &cobra.Command{
		Use:   "uninstall",
		Short: "Uninstall the 'docker builder' alias",
		Args:  cli.ExactArgs(0),
		RunE: func(cmd *cobra.Command, args []string) error {
			return runUninstall(dockerCli, options)
		},
		Hidden:            true,
		ValidArgsFunction: completion.Disable,
	}

	// hide builder persistent flag for this command
	cobrautil.HideInheritedFlags(cmd, "builder")

	return cmd
}