github.com/mgoltzsche/ctnr@v0.7.1-alpha/cmd/gc.go (about)

     1  // Copyright © 2017 Max Goltzsche
     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 cmd
    16  
    17  import (
    18  	"os"
    19  	"time"
    20  
    21  	exterrors "github.com/mgoltzsche/ctnr/pkg/errors"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var (
    26  	gcCmd = &cobra.Command{
    27  		Use:   "gc",
    28  		Short: "Garage collects all bundles and images in the local store",
    29  		Long:  `Garage collects all bundles and images in the local store.`,
    30  		Run:   wrapRun(runGc),
    31  	}
    32  	flagGcBundleTTL        time.Duration
    33  	flagGcImageTTL         time.Duration
    34  	flagGcImageRefTTL      time.Duration
    35  	flagGcMaxImagesPerRepo int
    36  )
    37  
    38  func init() {
    39  	gcCmd.Flags().DurationVarP(&flagGcBundleTTL, "bundle-ttl", "b", defaultBundleTTL, "bundle lifetime before it gets garbage collected")
    40  	gcCmd.Flags().DurationVarP(&flagGcImageTTL, "image-ttl", "i", defaultImageTTL, "image lifetime before it gets garbage collected")
    41  	gcCmd.Flags().DurationVarP(&flagGcImageRefTTL, "ref-ttl", "r", 0, "tagged image lifetime before it gets garbage collected")
    42  	gcCmd.Flags().IntVarP(&flagGcMaxImagesPerRepo, "max", "m", 0, "max entries per repo (default 0 == unlimited)")
    43  }
    44  
    45  func runGc(cmd *cobra.Command, args []string) (err error) {
    46  	if len(args) > 0 {
    47  		return usageError("No args expected")
    48  	}
    49  	cm, err := newContainerManager()
    50  	if err != nil {
    51  		return
    52  	}
    53  	gcd, err := store.BundleGC(flagGcBundleTTL, cm)
    54  	for _, b := range gcd {
    55  		os.Stdout.WriteString(b.ID() + "\n")
    56  	}
    57  	return exterrors.Append(err, store.ImageGC(flagGcImageTTL, flagGcImageRefTTL, flagGcMaxImagesPerRepo))
    58  }