1
1
package artifact
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
6
7
"github.com/containers/podman/v5/cmd/podman/common"
@@ -11,40 +12,67 @@ import (
11
12
12
13
var (
13
14
rmCmd = & cobra.Command {
14
- Use : "rm ARTIFACT" ,
15
- Short : "Remove an OCI artifact" ,
16
- Long : "Remove an OCI from local storage" ,
17
- RunE : rm ,
18
- Aliases : []string {"remove" },
19
- Args : cobra .ExactArgs (1 ),
15
+ Use : "rm [options] ARTIFACT" ,
16
+ Short : "Remove an OCI artifact" ,
17
+ Long : "Remove an OCI artifact from local storage" ,
18
+ RunE : rm ,
19
+ Aliases : []string {"remove" },
20
+ Args : func (cmd * cobra.Command , args []string ) error { //nolint: gocritic
21
+ return checkAllAndArgs (cmd , args )
22
+ },
20
23
ValidArgsFunction : common .AutocompleteArtifacts ,
21
- Example : `podman artifact rm quay.io/myimage/myartifact:latest` ,
22
- Annotations : map [string ]string {registry .EngineMode : registry .ABIMode },
24
+ Example : `podman artifact rm quay.io/myimage/myartifact:latest
25
+ podman artifact rm -a` ,
26
+ Annotations : map [string ]string {registry .EngineMode : registry .ABIMode },
23
27
}
24
- // The lint avoid here is because someday soon we will need flags for
25
- // this command
26
- rmFlag = rmFlagType {} //nolint:unused
28
+
29
+ rmOptions = entities.ArtifactRemoveOptions {}
27
30
)
28
31
29
- // TODO at some point force will be a required option; but this cannot be
30
- // until we have artifacts being consumed by other parts of libpod like
31
- // volumes
32
- type rmFlagType struct { //nolint:unused
33
- force bool
32
+ func rmFlags (cmd * cobra.Command ) {
33
+ flags := cmd .Flags ()
34
+ flags .BoolVarP (& rmOptions .All , "all" , "a" , false , "Remove all artifacts" )
34
35
}
35
-
36
36
func init () {
37
37
registry .Commands = append (registry .Commands , registry.CliCommand {
38
38
Command : rmCmd ,
39
39
Parent : artifactCmd ,
40
40
})
41
+ rmFlags (rmCmd )
41
42
}
42
43
43
44
func rm (cmd * cobra.Command , args []string ) error {
44
- artifactRemoveReport , err := registry .ImageEngine ().ArtifactRm (registry .Context (), args [0 ], entities.ArtifactRemoveOptions {})
45
+ var nameOrID string
46
+ if len (args ) > 0 {
47
+ nameOrID = args [0 ]
48
+ }
49
+ artifactRemoveReport , err := registry .ImageEngine ().ArtifactRm (registry .Context (), nameOrID , rmOptions )
45
50
if err != nil {
46
51
return err
47
52
}
48
- fmt .Println (artifactRemoveReport .ArtfactDigest .Encoded ())
53
+ for _ , d := range artifactRemoveReport .ArtifactDigests {
54
+ fmt .Println (d .Encoded ())
55
+ }
56
+ return nil
57
+ }
58
+
59
+ // checkAllAndArgs takes a cobra command and args and checks if
60
+ // all is used, then no args can be passed. note: this was created
61
+ // as an unexported local func for now and could be moved to pkg
62
+ // validate. if we add "--latest" to the command, then perhaps
63
+ // one of the existing plg validate funcs would be appropriate.
64
+ func checkAllAndArgs (c * cobra.Command , args []string ) error {
65
+ all , _ := c .Flags ().GetBool ("all" )
66
+ if all && len (args ) > 0 {
67
+ return fmt .Errorf ("when using the --all switch, you may not pass any artifact names or digests" )
68
+ }
69
+ if ! all {
70
+ if len (args ) < 1 {
71
+ return errors .New ("a single artifact name or digest must be specified" )
72
+ }
73
+ if len (args ) > 1 {
74
+ return errors .New ("too many arguments: only accepts one artifact name or digest " )
75
+ }
76
+ }
49
77
return nil
50
78
}
0 commit comments