cmd

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandAppendLayers = cobra.Command{
	Use:   "append-layers oci-layout-path [path-to-tarball...]",
	Short: "Appends a tarball or directory to every image in an OCI index",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		oci := args[0]
		extra := args[1:]

		if len(extra) == 0 {
			return
		}

		{
			path, err := layout.FromPath(oci)
			must("could not load oci directory", err)

			index, err := path.ImageIndex()
			must("could not load oci image index", err)

			layers := []untypedLayer{}
			for _, path := range extra {
				layers = append(layers, newUntypedLayerFromPath(path))
			}

			index, err = pkg.MutateOCITree(
				index, nil,
				func(img v1.Image) (v1.Image, error) {
					imgMediaType, err := img.MediaType()
					if err != nil {
						return nil, fmt.Errorf("could not get image media type: %w", err)
					}

					layerType := types.DockerLayer
					if imgMediaType == types.OCIManifestSchema1 {
						layerType = types.OCILayer
					}

					for _, untypedLayer := range layers {
						layer, err := untypedLayer.ToLayer(layerType)
						if err != nil {
							return nil, fmt.Errorf("could not load image layer: %w", err)
						}

						img, err = mutate.AppendLayers(img, layer)
						if err != nil {
							return nil, fmt.Errorf("could not append layer: %w", err)
						}
					}

					return img, nil
				},
				nil,
			)
			must("could not modify oci tree", err)

			_, err = layout.Write(oci, index)
			must("could not write image", err)
		}

		{
			path, err := layout.FromPath(oci)
			must("could not load oci directory", err)

			hashesToRemove, err := path.GarbageCollect()
			must("could not garbage collect oci image", err)

			for _, hash := range hashesToRemove {
				err := path.RemoveBlob(hash)
				must("could not remove blob", err)
			}
		}
	},
}
View Source
var CommandConvertFromOCITar = cobra.Command{
	Use:   "convert-from-oci-tar oci-tarball oci-layout-path",
	Short: "Reads the OCI layout tarball (=docker build output) and outputs an OCI layout directory (=ko output, crane and image-tool input)",
	Args:  cobra.ExactArgs(2),
	Run: func(cmd *cobra.Command, args []string) {
		path := args[0]
		output := args[1]

		{
			err := untar(path, output)
			must("could not untar OCI tarball", err)
		}

		{
			path, err := layout.FromPath(output)
			must("could not load oci directory", err)

			hashesToRemove, err := path.GarbageCollect()
			must("could not garbage collect oci image", err)

			for _, hash := range hashesToRemove {
				err := path.RemoveBlob(hash)
				must("could not remove blob", err)
			}
		}
	},
}
View Source
var CommandConvertToDockerTar = cobra.Command{
	Use:   "convert-to-docker-tar oci-layout-path docker-tarball image-name",
	Short: "Reads the OCI layout directory and outputs a tarball that is compatible with \"docker load\"",
	Args:  cobra.ExactArgs(3),
	Run: func(cmd *cobra.Command, args []string) {
		path := args[0]
		output := args[1]
		imageName := args[2]

		ociLayout, err := layout.FromPath(path)
		must("could not load oci directory", err)

		index, err := ociLayout.ImageIndex()
		must("could not load oci image index", err)

		var images []v1.Image
		err = pkg.SearchOCITree(index, nil,
			func(descriptors []*v1.Descriptor, image v1.Image) error {
				var platform *v1.Platform

				for _, desc := range descriptors {
					if desc.Platform != nil {
						platform = desc.Platform
					}
				}

				{
					cfg, err := image.ConfigFile()
					if err != nil {
						return fmt.Errorf("could not load image config: %w", err)
					}
					if imgPlatform := cfg.Platform(); imgPlatform != nil {
						platform = imgPlatform
					}
				}

				if platform != nil && platform.Architecture == runtime.GOARCH {
					images = append(images, image)
				}

				return nil
			},
		)
		must("could not find images", err)

		switch {
		case len(images) == 0:
			fail("no matching images found")
		case len(images) > 1:
			fail("multiple matching images found")
		}

		ref, err := name.ParseReference(imageName)
		must("invalid image name", err)

		err = tarball.WriteToFile(output, ref, images[0])
		must("could not write tarball", err)
	},
}
View Source
var CommandListDigests = cobra.Command{
	Use:   "list-digests oci-layout-path",
	Short: "Outputs the digests for images found in the OCI layout directory",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		path := args[0]

		ociLayout, err := layout.FromPath(path)
		must("could not load oci directory", err)

		imageIndex, err := ociLayout.ImageIndex()
		must("could not load oci image index", err)

		indexManifest, err := imageIndex.IndexManifest()
		must("could not load oci index manifest", err)

		for _, man := range indexManifest.Manifests {
			fmt.Fprintf(cmd.OutOrStdout(), "%s\n", man.Digest)
		}
	},
}
View Source
var CommandResetLabelsAndAnnotations = cobra.Command{
	Use:   "reset-labels-and-annotations oci-layout-path",
	Short: "Removes all labels and annotations from OCI indices, images and descriptors in a OCI layout directory",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		oci := args[0]

		{
			path, err := layout.FromPath(oci)
			must("could not load oci directory", err)

			index, err := path.ImageIndex()
			must("could not load oci image index", err)

			index, err = pkg.MutateOCITree(
				index,
				func(index v1.ImageIndex) (v1.ImageIndex, error) {
					return pkg.ReplaceImageIndexAnnotations(index, map[string]string{}), nil
				}, func(image v1.Image) (v1.Image, error) {
					configFile, err := image.ConfigFile()
					if err != nil {
						return nil, fmt.Errorf("could not parse config file: %w", err)
					}

					configFile.Config.Labels = map[string]string{}

					image, err = mutate.ConfigFile(image, configFile)
					if err != nil {
						return nil, fmt.Errorf("could not replace config file: %w", err)
					}

					return pkg.ReplaceImageAnnotations(image, map[string]string{}), nil
				}, func(descriptor v1.Descriptor) (v1.Descriptor, error) {
					descriptor.Annotations = map[string]string{}
					return descriptor, nil
				},
			)
			must("could not modify oci tree", err)

			_, err = layout.Write(oci, index)
			must("could not write image", err)
		}

		{
			path, err := layout.FromPath(oci)
			must("could not load oci directory", err)

			hashesToRemove, err := path.GarbageCollect()
			must("could not garbage collect oci image", err)

			for _, hash := range hashesToRemove {
				err := path.RemoveBlob(hash)
				must("could not remove blob", err)
			}
		}
	},
}
View Source
var CommandRoot = cobra.Command{
	Use: "image-tool",
}
View Source
var CommandTagDockerTar = cobra.Command{
	Use:   "tag-docker-tar docker-tarball image-name",
	Short: "Replaces the image name in the docker tarball (image name should include a tag)",
	Args:  cobra.ExactArgs(2),
	Run: func(cmd *cobra.Command, args []string) {
		path := args[0]
		imageName := args[1]

		tmpOutFile := fmt.Sprintf("%s.tmp", path)

		{
			image, err := tarball.ImageFromPath(path, nil)
			must("could not read tarball", err)

			ref, err := name.ParseReference(imageName)
			must("invalid image name", err)

			err = tarball.WriteToFile(tmpOutFile, ref, image)
			must("could not write temporary tarball", err)
		}

		err := os.Rename(tmpOutFile, path)
		must("could not move temporary tarball to destination", err)
	},
}

Functions

func Run

func Run()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL