Skip to content

Commit 208b9d3

Browse files
committed
cmd/podman: --ignore errors flag to artifact rm
Signed-off-by: Celso Henrique Souza Silva <celsohenrique367@gmail.com>
1 parent 1671029 commit 208b9d3

File tree

12 files changed

+70
-0
lines changed

12 files changed

+70
-0
lines changed

cmd/podman/artifact/rm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
podman artifact rm quay.io/myimage/myartifact:latest
2424
podman artifact rm -a
2525
podman artifact rm c4dfb1609ee2 93fd78260bd1 c0ed59d05ff7
26+
podman artifact rm -i c4dfb1609ee2
2627
`,
2728
}
2829

@@ -32,6 +33,7 @@ var (
3233
func rmFlags(cmd *cobra.Command) {
3334
flags := cmd.Flags()
3435
flags.BoolVarP(&rmOptions.All, "all", "a", false, "Remove all artifacts")
36+
flags.BoolVarP(&rmOptions.Ignore, "ignore", "i", false, "Ignore error if artifact does not exist")
3537
}
3638
func init() {
3739
registry.Commands = append(registry.Commands, registry.CliCommand{

docs/source/markdown/podman-artifact-rm.1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ providing a name or digest of the artifact.
2222

2323
Print usage statement.
2424

25+
#### **--ignore**, **-i**
26+
27+
Remove artifacts in the local store, ignoring errors when trying to remove artifacts that do not exist.
2528

2629
## EXAMPLES
2730

@@ -49,6 +52,11 @@ Deleted: cee15f7c5ce3e86ae6ce60d84bebdc37ad34acfa9a2611cf47501469ac83a1ab
4952
Deleted: 72875f8f6f78d5b8ba98b2dd2c0a6f395fde8f05ff63a1df580d7a88f5afa97b
5053
```
5154

55+
Remove artifacts ignoring the errors if the artifact does not exist.
56+
```
57+
$ podman artifact rm -i 3f78d5b8ba98b2
58+
```
59+
5260
## SEE ALSO
5361
**[podman(1)](podman.1.md)**, **[podman-artifact(1)](podman-artifact.1.md)**
5462

pkg/api/handlers/libpod/artifacts.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func BatchRemoveArtifact(w http.ResponseWriter, r *http.Request) {
169169
query := struct {
170170
All bool `schema:"all"`
171171
Artifacts []string `schema:"artifacts"`
172+
Ignore bool `schema:"ignore"`
172173
}{}
173174

174175
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
@@ -191,11 +192,16 @@ func BatchRemoveArtifact(w http.ResponseWriter, r *http.Request) {
191192
removeOptions := entities.ArtifactRemoveOptions{
192193
Artifacts: query.Artifacts,
193194
All: query.All,
195+
Ignore: query.Ignore,
194196
}
195197

196198
artifacts, err := imageEngine.ArtifactRm(r.Context(), removeOptions)
197199
if err != nil {
198200
if errors.Is(err, libartifact_types.ErrArtifactNotExist) {
201+
if removeOptions.Ignore {
202+
utils.WriteResponse(w, http.StatusOK, artifacts)
203+
return
204+
}
199205
utils.ArtifactNotFound(w, "", err)
200206
return
201207
}

pkg/api/server/register_artifacts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func (s *APIServer) registerArtifactHandlers(r *mux.Router) error {
116116
// in: query
117117
// description: Remove all artifacts
118118
// type: boolean
119+
// - name: ignore
120+
// in: query
121+
// description: Ignore errors if artifact do not exist
122+
// type: boolean
119123
// responses:
120124
// 200:
121125
// $ref: "#/responses/artifactRemoveResponse"

pkg/bindings/artifacts/remove.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func Remove(ctx context.Context, nameOrID string, options *RemoveOptions) (*enti
2727

2828
response, err := conn.DoRequest(ctx, nil, http.MethodDelete, "/artifacts/remove", params, nil)
2929
if err != nil {
30+
if options.GetIgnore() {
31+
return &entities.ArtifactRemoveReport{}, nil
32+
}
33+
3034
return nil, err
3135
}
3236
defer response.Body.Close()

pkg/bindings/artifacts/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type RemoveOptions struct {
5252
All *bool
5353
// Artifacts is a list of Artifact IDs or names to remove
5454
Artifacts []string
55+
// Ignore errors if IDs or names are not defined
56+
Ignore *bool
5557
}
5658

5759
// AddOptions are optional options for removing images

pkg/bindings/artifacts/types_remove_options.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/domain/entities/artifact.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type ArtifactRemoveOptions struct {
9696
All bool
9797
// Artifacts is a list of Artifact IDs or names to remove
9898
Artifacts []string
99+
// Ignore if a specified artifact does not exist and do not throw any error.
100+
Ignore bool
99101
}
100102

101103
type ArtifactRemoveReport = entitiesTypes.ArtifactRemoveReport

pkg/domain/infra/abi/artifact.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/containers/podman/v5/pkg/domain/entities"
1313
"github.com/containers/podman/v5/pkg/libartifact/types"
1414
"github.com/opencontainers/go-digest"
15+
"github.com/sirupsen/logrus"
1516
"go.podman.io/common/libimage"
1617
)
1718

@@ -124,6 +125,14 @@ func (ir *ImageEngine) ArtifactRm(ctx context.Context, opts entities.ArtifactRem
124125
for _, namesOrDigest := range namesOrDigests {
125126
artifactDigest, err := artStore.Remove(ctx, namesOrDigest)
126127
if err != nil {
128+
if opts.Ignore {
129+
logrus.Debugf(
130+
"Artifact with name or digest: %s does not exist, ignoring error (--ignore): %v",
131+
namesOrDigest,
132+
err,
133+
)
134+
continue
135+
}
127136
return nil, err
128137
}
129138
artifactDigests = append(artifactDigests, artifactDigest)

pkg/domain/infra/tunnel/artifact.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (ir *ImageEngine) ArtifactRm(_ context.Context, opts entities.ArtifactRemov
5757
removeOptions := artifacts.RemoveOptions{
5858
All: &opts.All,
5959
Artifacts: opts.Artifacts,
60+
Ignore: &opts.Ignore,
6061
}
6162

6263
return artifacts.Remove(ir.ClientCtx, "", &removeOptions)

0 commit comments

Comments
 (0)