github.com/mfpierre/corectl@v0.5.6/delete.go (about)

     1  // Copyright 2015 - António Meireles  <antonio.meireles@reformi.st>
     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  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/blang/semver"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var (
    29  	rmCmd = &cobra.Command{
    30  		Use:     "rm",
    31  		Aliases: []string{"rmi"},
    32  		Short:   "Removes one or more CoreOS images from local fs",
    33  		PreRunE: defaultPreRunE,
    34  		RunE:    rmCommand,
    35  	}
    36  )
    37  
    38  func defaultPreRunE(cmd *cobra.Command, args []string) (err error) {
    39  	if len(args) != 0 {
    40  		return fmt.Errorf("Incorrect usage. " +
    41  			"This command doesn't accept any arguments.")
    42  	}
    43  	engine.rawArgs.BindPFlags(cmd.Flags())
    44  	return err
    45  }
    46  
    47  func rmCommand(cmd *cobra.Command, args []string) (err error) {
    48  	var (
    49  		channel = normalizeChannelName(engine.rawArgs.GetString("channel"))
    50  		version = normalizeVersion(engine.rawArgs.GetString("version"))
    51  		ll      map[string]semver.Versions
    52  		l       semver.Versions
    53  	)
    54  
    55  	if ll, err = localImages(); err != nil {
    56  		return err
    57  	}
    58  	l = ll[channel]
    59  
    60  	if engine.rawArgs.GetBool("old") {
    61  		for _, v := range l[0 : l.Len()-1] {
    62  			if err = os.RemoveAll(filepath.Join(engine.imageDir,
    63  				channel, "/", v.String())); err != nil {
    64  				return
    65  			}
    66  			log.Printf("removed %s/%s\n", channel, version)
    67  		}
    68  		return
    69  	}
    70  
    71  	if version == "latest" {
    72  		if l.Len() > 0 {
    73  			version = l[l.Len()-1].String()
    74  		} else {
    75  			log.Println("nothing to delete")
    76  			return
    77  		}
    78  	}
    79  
    80  	target := filepath.Join(engine.imageDir, channel, "/", version)
    81  	if _, err = os.Stat(target); err != nil {
    82  		log.Printf("%s/%s not found\n", channel, version)
    83  		return nil
    84  	}
    85  	if err = os.RemoveAll(target); err == nil {
    86  		log.Printf("removed %s/%s\n", channel, version)
    87  	}
    88  	return
    89  }
    90  
    91  func init() {
    92  	rmCmd.Flags().String("channel", "alpha", "CoreOS channel")
    93  	rmCmd.Flags().String("version", "latest", "CoreOS version")
    94  	rmCmd.Flags().Bool("old", false, "removes outdated images")
    95  	RootCmd.AddCommand(rmCmd)
    96  }