manifest: add support for buildah manifest exists

Adds support for `buildah manifest exists <name>` which tells user if
requested manifest is present in local storage or not, if manifest is
present in local-storage command exits with exit code 0 otherwise 1.

Similar to: https://docs.podman.io/en/latest/markdown/podman-manifest-exists.1.html

Closes: https://github.com/containers/buildah/issues/4217

Signed-off-by: Aditya R <arajan@redhat.com>
This commit is contained in:
Aditya R 2022-09-05 12:36:05 +05:30
parent a1fd130f9e
commit 9f435de84e
No known key found for this signature in database
GPG Key ID: 8E5A8A19DF7C8673
4 changed files with 100 additions and 0 deletions

View File

@ -55,6 +55,7 @@ func init() {
manifestInspectDescription = "\n Display the contents of a manifest list or image index."
manifestPushDescription = "\n Pushes manifest lists and image indexes to registries."
manifestRmDescription = "\n Remove one or more manifest lists from local storage."
manifestExistsDescription = "\n Check if a manifest list exists in local storage."
manifestCreateOpts manifestCreateOpts
manifestAddOpts manifestAddOpts
manifestRemoveOpts manifestRemoveOpts
@ -155,6 +156,19 @@ func init() {
manifestRemoveCommand.SetUsageTemplate(UsageTemplate())
manifestCommand.AddCommand(manifestRemoveCommand)
manifestExistsCommand := &cobra.Command{
Use: "exists",
Short: "Check if a manifest list exists in local storage",
Long: manifestExistsDescription,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return manifestExistsCmd(cmd, args)
},
Example: "buildah manifest exists mylist",
}
manifestExistsCommand.SetUsageTemplate(UsageTemplate())
manifestCommand.AddCommand(manifestExistsCommand)
manifestAnnotateCommand := &cobra.Command{
Use: "annotate",
Short: "Add or update information about an entry in a manifest list or image index",
@ -237,6 +251,39 @@ func init() {
manifestCommand.AddCommand(manifestRmCommand)
}
func manifestExistsCmd(c *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("At least a name must be specified for the list")
}
name := args[0]
store, err := getStore(c)
if err != nil {
return err
}
systemContext, err := parse.SystemContextFromOptions(c)
if err != nil {
return fmt.Errorf("error building system context: %w", err)
}
runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext})
if err != nil {
return err
}
_, err = runtime.LookupManifestList(name)
if err != nil {
if errors.Is(err, storage.ErrImageUnknown) {
if err := shutdownStore(c); err != nil {
return err
}
os.Exit(1)
}
return err
}
return nil
}
func manifestCreateCmd(c *cobra.Command, args []string, opts manifestCreateOpts) error {
if len(args) == 0 {
return errors.New("At least a name must be specified for the list")

View File

@ -0,0 +1,40 @@
% buildah-manifest-exists(1)
## NAME
buildah\-manifest\-exists - Check if the given manifest list exists in local storage
## SYNOPSIS
**buildah manifest exists** *manifest*
## DESCRIPTION
**buildah manifest exists** checks if a manifest list exists in local storage. Buildah will
return an exit code of `0` when the manifest list is found. A `1` will be returned otherwise.
An exit code of `125` indicates there was another issue.
## OPTIONS
#### **--help**, **-h**
Print usage statement.
## EXAMPLE
Check if a manifest list called `list1` exists (the manifest list does actually exist).
```
$ buildah manifest exists list1
$ echo $?
0
$
```
Check if an manifest called `mylist` exists (the manifest list does not actually exist).
```
$ buildah manifest exists mylist
$ echo $?
1
$
```
## SEE ALSO
**[buildah(1)](buildah.1.md)**, **[buildah-manifest(1)](buildah-manifest.1.md)**

View File

@ -22,6 +22,7 @@ The `buildah manifest` command provides subcommands which can be used to:
| add | [buildah-manifest-add(1)](buildah-manifest-add.1.md) | Add an image to a manifest list or image index. |
| annotate | [buildah-manifest-annotate(1)](buildah-manifest-annotate.1.md) | Add or update information about an image in a manifest list or image index. |
| create | [buildah-manifest-create(1)](buildah-manifest-create.1.md) | Create a manifest list or image index. |
| exists | [buildah-manifest-exists(1)](buildah-manifest-exists.1.md) | Check if a manifest list exists in local storage. |
| inspect | [buildah-manifest-inspect(1)](buildah-manifest-inspect.1.md) | Display the contents of a manifest list or image index. |
| push | [buildah-manifest-push(1)](buildah-manifest-push.1.md) | Push a manifest list or image index to a registry or other location. |
| remove | [buildah-manifest-remove(1)](buildah-manifest-remove.1.md) | Remove an image from a manifest list or image index. |

View File

@ -18,6 +18,10 @@ IMAGE_LIST_S390X_INSTANCE_DIGEST=sha256:882a20ee0df7399a445285361d38b711c299ca09
assert "$output" =~ "that name is already in use"
run_buildah manifest create --amend foo
assert "$output" == "$listid"
# since manifest exists in local storage this should exit with `0`
run_buildah manifest exists foo
# since manifest does not exist in local storage this should exit with `1`
run_buildah 1 manifest exists foo2
}
@test "manifest-inspect-id" {
@ -29,6 +33,10 @@ IMAGE_LIST_S390X_INSTANCE_DIGEST=sha256:882a20ee0df7399a445285361d38b711c299ca09
@test "manifest-add" {
run_buildah manifest create foo
run_buildah manifest add foo ${IMAGE_LIST}
# since manifest exists in local storage this should exit with `0`
run_buildah manifest exists foo
# since manifest does not exist in local storage this should exit with `1`
run_buildah 1 manifest exists foo2
run_buildah manifest rm foo
}
@ -211,4 +219,8 @@ IMAGE_LIST_S390X_INSTANCE_DIGEST=sha256:882a20ee0df7399a445285361d38b711c299ca09
run_buildah manifest inspect test
# must contain amd64
expect_output --substring "amd64"
# since manifest exists in local storage this should exit with `0`
run_buildah manifest exists test:latest
# since manifest does not exist in local storage this should exit with `1`
run_buildah 1 manifest exists test2
}