/
cache_clean_linux.go
133 lines (117 loc) · 3.84 KB
/
cache_clean_linux.go
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
130
131
132
133
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// 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/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/app/apptainer"
"github.com/apptainer/apptainer/internal/pkg/cache"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
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 values: library, oci, shub, blob, net, oras, all)",
}
// -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 'apptainer cache clean' and will clear your local apptainer 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 := apptainer.CleanApptainerCache(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
}