1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// Copyright (c) 2018-2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/sylabs/singularity/v4/docs"
"github.com/sylabs/singularity/v4/internal/app/singularity"
"github.com/sylabs/singularity/v4/internal/pkg/cache"
"github.com/sylabs/singularity/v4/pkg/cmdline"
"github.com/sylabs/singularity/v4/pkg/sylog"
)
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterFlagForCmd(&cacheCleanTypesFlag, cacheCleanCmd)
cmdManager.RegisterFlagForCmd(&cacheCleanDaysFlag, cacheCleanCmd)
cmdManager.RegisterFlagForCmd(&cacheCleanDryFlag, cacheCleanCmd)
cmdManager.RegisterFlagForCmd(&cacheCleanForceFlag, cacheCleanCmd)
})
}
var (
cacheCleanTypes []string
cacheCleanDays int
cacheCleanDry bool
cacheCleanForce bool
// -T|--type
cacheCleanTypesFlag = cmdline.Flag{
ID: "cacheCleanTypes",
Value: &cacheCleanTypes,
DefaultValue: []string{"all"},
Name: "type",
ShortHand: "T",
Usage: "a list of cache types to clean, possible entries: all, " + strings.Join(cache.AllCacheTypes, ", "),
}
// -D|--days
cacheCleanDaysFlag = cmdline.Flag{
ID: "cacheCleanDaysFlag",
Value: &cacheCleanDays,
DefaultValue: 0,
Name: "days",
ShortHand: "D",
Usage: "remove all cache entries older than specified number of days",
}
// -n|--dry-run
cacheCleanDryFlag = cmdline.Flag{
ID: "cacheCleanDryFlag",
Value: &cacheCleanDry,
DefaultValue: false,
Name: "dry-run",
ShortHand: "n",
Usage: "operate in dry run mode and do not actually clean the cache",
}
// -f|--force
cacheCleanForceFlag = cmdline.Flag{
ID: "cacheCleanForceFlag",
Value: &cacheCleanForce,
DefaultValue: false,
Name: "force",
ShortHand: "f",
Usage: "suppress any prompts and clean the cache",
}
// cacheCleanCmd is 'singularity cache clean' and will clear your local singularity cache
cacheCleanCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
if err := cleanCache(); err != nil {
sylog.Fatalf("Handle clean failed: %v", err)
}
},
Use: docs.CacheCleanUse,
Short: docs.CacheCleanShort,
Long: docs.CacheCleanLong,
Example: docs.CacheCleanExample,
}
)
func cleanCache() error {
if cacheCleanDry {
fmt.Println("User requested a dry run. Not actually deleting any data!")
}
if !cacheCleanForce && !cacheCleanDry {
ok, err := cleanCachePrompt()
if err != nil {
return fmt.Errorf("could not prompt user: %v", err)
}
if !ok {
sylog.Infof("Handle cleanup canceled")
return nil
}
}
// create a handle to access the current image cache
imgCache := getCacheHandle(cache.Config{})
err := singularity.CleanSingularityCache(imgCache, cacheCleanDry, cacheCleanTypes, cacheCleanDays)
if err != nil {
return fmt.Errorf("could not clean cache: %v", err)
}
return nil
}
func cleanCachePrompt() (bool, error) {
fmt.Print(`This will delete everything in your cache (containers from all sources and OCI blobs).
Hint: You can see exactly what would be deleted by canceling and using the --dry-run option.
Do you want to continue? [y/N] `)
r := bufio.NewReader(os.Stdin)
input, err := r.ReadString('\n')
if err != nil {
return false, fmt.Errorf("could not read user's input: %s", err)
}
return strings.ToLower(input) == "y\n", nil
}
|