github.com/dctrud/umoci@v0.4.3-0.20191016193643-05a1d37de015/api.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2018 Cisco Systems
     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 umoci
    19  
    20  import (
    21  	"github.com/openSUSE/umoci/oci/cas/dir"
    22  	"github.com/openSUSE/umoci/oci/casext"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // OpenLayout opens an existing OCI image layout, and fails if it does not
    27  // exist.
    28  func OpenLayout(imagePath string) (casext.Engine, error) {
    29  	// Get a reference to the CAS.
    30  	engine, err := dir.Open(imagePath)
    31  	if err != nil {
    32  		return casext.Engine{}, errors.Wrap(err, "open CAS")
    33  	}
    34  
    35  	return casext.NewEngine(engine), nil
    36  }
    37  
    38  // CreateLayout creates an existing OCI image layout, and fails if it already
    39  // exists.
    40  func CreateLayout(imagePath string) (casext.Engine, error) {
    41  	err := dir.Create(imagePath)
    42  	if err != nil {
    43  		return casext.Engine{}, err
    44  	}
    45  
    46  	return OpenLayout(imagePath)
    47  }