github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/cmd/umoci/new.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2016-2020 SUSE LLC
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package main
    19  
    20  import (
    21  	"github.com/opencontainers/umoci"
    22  	"github.com/opencontainers/umoci/oci/cas/dir"
    23  	"github.com/opencontainers/umoci/oci/casext"
    24  	"github.com/pkg/errors"
    25  	"github.com/urfave/cli"
    26  )
    27  
    28  var newCommand = cli.Command{
    29  	Name:  "new",
    30  	Usage: "creates a blank tagged OCI image",
    31  	ArgsUsage: `--image <image-path>:<new-tag>
    32  
    33  Where "<image-path>" is the path to the OCI image, and "<new-tag>" is the name
    34  of the tag for the empty manifest.
    35  
    36  Once you create a new image with umoci-new(1) you can directly use the image
    37  with umoci-unpack(1), umoci-repack(1), and umoci-config(1) to modify the new
    38  manifest as you see fit. This allows you to create entirely new images without
    39  needing a base image to start from.`,
    40  
    41  	// new modifies an image layout.
    42  	Category: "image",
    43  
    44  	Before: func(ctx *cli.Context) error {
    45  		if ctx.NArg() != 0 {
    46  			return errors.Errorf("invalid number of positional arguments: expected none")
    47  		}
    48  		return nil
    49  	},
    50  
    51  	Action: newImage,
    52  }
    53  
    54  func newImage(ctx *cli.Context) error {
    55  	imagePath := ctx.App.Metadata["--image-path"].(string)
    56  	tagName := ctx.App.Metadata["--image-tag"].(string)
    57  
    58  	// Get a reference to the CAS.
    59  	engine, err := dir.Open(imagePath)
    60  	if err != nil {
    61  		return errors.Wrap(err, "open CAS")
    62  	}
    63  	engineExt := casext.NewEngine(engine)
    64  	defer engine.Close()
    65  
    66  	return umoci.NewImage(engineExt, tagName)
    67  }