File: rename.go

package info (click to toggle)
golang-github-containers-buildah 1.41.4%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,152 kB
  • sloc: sh: 2,569; makefile: 241; perl: 187; asm: 16; awk: 12; ansic: 1
file content (59 lines) | stat: -rw-r--r-- 1,388 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
package main

import (
	"fmt"

	"github.com/containers/buildah"
	"github.com/spf13/cobra"
)

var (
	renameDescription = "\n  Renames a local container."
	renameCommand     = &cobra.Command{
		Use:   "rename",
		Short: "Rename a container",
		Long:  renameDescription,
		RunE:  renameCmd,
		Example: `buildah rename containerName NewName
  buildah rename containerID NewName`,
		Args: cobra.ExactArgs(2),
	}
)

func init() {
	renameCommand.SetUsageTemplate(UsageTemplate())
	rootCmd.AddCommand(renameCommand)
}

func renameCmd(c *cobra.Command, args []string) error {
	var builder *buildah.Builder

	name := args[0]
	newName := args[1]

	store, err := getStore(c)
	if err != nil {
		return err
	}

	builder, err = openBuilder(getContext(), store, name)
	if err != nil {
		return fmt.Errorf("reading build container %q: %w", name, err)
	}

	oldName := builder.Container
	if oldName == newName {
		return fmt.Errorf("renaming a container with the same name as its current name")
	}

	if build, err := openBuilder(getContext(), store, newName); err == nil {
		return fmt.Errorf("the container name %q is already in use by container %q", newName, build.ContainerID)
	}

	err = store.SetNames(builder.ContainerID, []string{newName})
	if err != nil {
		return fmt.Errorf("renaming container %q to the name %q: %w", oldName, newName, err)
	}
	builder.Container = newName
	return builder.Save()
}