github.com/coreos/mantle@v0.13.0/cmd/ore/do/delete-image.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package do
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var (
    26  	cmdDeleteImage = &cobra.Command{
    27  		Use:   "delete-image [options]",
    28  		Short: "Delete image",
    29  		Long:  `Delete an image.`,
    30  		RunE:  runDeleteImage,
    31  	}
    32  )
    33  
    34  func init() {
    35  	DO.AddCommand(cmdDeleteImage)
    36  	cmdDeleteImage.Flags().StringVarP(&imageName, "name", "n", "", "image name")
    37  }
    38  
    39  func runDeleteImage(cmd *cobra.Command, args []string) error {
    40  	if len(args) != 0 {
    41  		fmt.Fprintf(os.Stderr, "Unrecognized args in do delete-image cmd: %v\n", args)
    42  		os.Exit(2)
    43  	}
    44  
    45  	if err := deleteImage(); err != nil {
    46  		fmt.Fprintf(os.Stderr, "%v\n", err)
    47  		os.Exit(1)
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func deleteImage() error {
    54  	if imageName == "" {
    55  		return fmt.Errorf("Image name must be specified")
    56  	}
    57  
    58  	ctx := context.Background()
    59  
    60  	image, err := API.GetUserImage(ctx, imageName, false)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if err := API.DeleteImage(ctx, image.ID); err != nil {
    66  		return err
    67  	}
    68  
    69  	return nil
    70  }