Skip to content

Commit

Permalink
main: return different exit code when an input is not found
Browse files Browse the repository at this point in the history
This is a proof of concept that shows how we could do a different
exit code in skopeo when an input is not found. The use case is
`osbuild` which uses skopeo to inspect images and it would be
nice to differenciate between an image that is not found and
general skopeo errors (or errors like network issues etc).

Note that this POC is not ideal:
1. lacks tests (happy to do them if there is a chance this is considered)
2. The output breaks, it used to start with `FATA[000]` and now starts
   with `ERRO[0000]`

It's likely that (2) is a show stopper, it seems there is no way
to override the exit code of logrus for logrus.Fatal() so it would
require a bit more work if the output must stay identical.

I picked exit code (2) for not found because it is also the value
of ENOENT but that is arbitrary and I'm happy to switch to any
other value (we could do `44` as `404` :-) - happy about ideas).

P.S. I also found that for local images that are not found `ErrNotAnImage`
is returned so the docstring of `storage.ErrNotAnImage` may need
updating (I'm happy to do this separtely if desired).

To reproduce:
```
$ skopeo inspect containers-storage:010101010101
FATA[0000] Error parsing image name "containers-storage:010101010101": reference "[overlay@/media/space/home/egon/.local/share/containers/storage+/run/user/1000/containers]docker.io/library/010101010101:latest" does not resolve to an image ID: identifier is not an image
```

For references (from storage/errors.go):
```
ErrNotAnImage = errors.New("identifier is not an image")
ErrImageUnknown = errors.New("image not known")
```

Signed-off-by: Michael Vogt <[email protected]>
  • Loading branch information
mvo5 committed Feb 26, 2024
1 parent d7c99d5 commit 2dfafe0
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/skopeo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/types"
"github.com/containers/skopeo/version"
"github.com/containers/storage"
"github.com/containers/storage/pkg/reexec"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -123,12 +125,21 @@ func (opts *globalOptions) before(cmd *cobra.Command, args []string) error {
return nil
}

func isImageNotExists(err error) bool {
return errors.Is(err, storage.ErrImageUnknown) ||
errors.Is(err, storage.ErrNotAnImage)
}

func main() {
if reexec.Init() {
return
}
rootCmd, _ := createApp()
if err := rootCmd.Execute(); err != nil {
if isImageNotExists(err) {
logrus.Error(err)
logrus.Exit(2)
}
logrus.Fatal(err)
}
}
Expand Down

0 comments on commit 2dfafe0

Please sign in to comment.