github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/cmd/umoci/init.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  	"fmt"
    22  	"os"
    23  
    24  	"github.com/apex/log"
    25  	"github.com/opencontainers/umoci/oci/cas/dir"
    26  	"github.com/pkg/errors"
    27  	"github.com/urfave/cli"
    28  )
    29  
    30  var initCommand = cli.Command{
    31  	Name:  "init",
    32  	Usage: "create a new OCI layout",
    33  	ArgsUsage: `--layout <image-path>
    34  
    35  Where "<image-path>" is the path to the OCI image to be created.
    36  
    37  The new OCI image does not contain any references or blobs, but those can be
    38  created through the use of umoci-new(1), umoci-tag(1) and other similar
    39  commands.`,
    40  
    41  	// create modifies an image layout.
    42  	Category: "layout",
    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: initLayout,
    52  }
    53  
    54  func initLayout(ctx *cli.Context) error {
    55  	imagePath := ctx.App.Metadata["--image-path"].(string)
    56  
    57  	if _, err := os.Stat(imagePath); !os.IsNotExist(err) {
    58  		if err == nil {
    59  			err = fmt.Errorf("path already exists: %s", imagePath)
    60  		}
    61  		return errors.Wrap(err, "image layout creation")
    62  	}
    63  
    64  	if err := dir.Create(imagePath); err != nil {
    65  		return errors.Wrap(err, "image layout creation")
    66  	}
    67  
    68  	log.Infof("created new OCI image: %s", imagePath)
    69  	return nil
    70  }