Merge pull request #236 from wagoodman/auto-derive-image-source

Add ability to specify source as a URL scheme
This commit is contained in:
Alex Goodman 2019-10-09 08:47:53 -04:00 committed by GitHub
commit 11a2473807
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 10 deletions

View file

@ -64,7 +64,11 @@ Analyze and image and get a pass/fail result based on the image efficiency and w
**With Multiple Image Sources and Container Engines Supported**
With the `--source` option, you can select where to fetch the container image from:
```bash
dive <your-image-tag> --source podman
dive <your-image> --source <source>
```
or
```bash
dive <source>://<your-image>
```
With valid `source` options as such:

View file

@ -39,16 +39,26 @@ func doAnalyzeCmd(cmd *cobra.Command, args []string) {
os.Exit(1)
}
engine, err := cmd.PersistentFlags().GetString("source")
if err != nil {
fmt.Printf("unable to determine engine: %v\n", err)
os.Exit(1)
var sourceType dive.ImageSource
var imageStr string
sourceType, imageStr = dive.DeriveImageSource(userImage)
if sourceType == dive.SourceUnknown {
sourceStr, err := cmd.PersistentFlags().GetString("source")
if err != nil {
fmt.Printf("unable to determine image source: %v\n", err)
os.Exit(1)
}
sourceType = dive.ParseImageSource(sourceStr)
imageStr = userImage
}
runtime.Run(runtime.Options{
Ci: isCi,
Source: dive.ParseImageSource(engine),
Image: userImage,
Source: sourceType,
Image: imageStr,
ExportFile: exportFile,
CiConfig: ciConfig,
})

View file

@ -5,6 +5,8 @@ import (
"github.com/wagoodman/dive/dive/image"
"github.com/wagoodman/dive/dive/image/docker"
"github.com/wagoodman/dive/dive/image/podman"
"net/url"
"strings"
)
const (
@ -24,11 +26,11 @@ func (r ImageSource) String() string {
func ParseImageSource(r string) ImageSource {
switch r {
case "docker":
case SourceDockerEngine.String():
return SourceDockerEngine
case "podman":
case SourcePodmanEngine.String():
return SourcePodmanEngine
case "docker-archive":
case SourceDockerArchive.String():
return SourceDockerArchive
case "docker-tar":
return SourceDockerArchive
@ -37,6 +39,28 @@ func ParseImageSource(r string) ImageSource {
}
}
func DeriveImageSource(image string) (ImageSource, string) {
u, err := url.Parse(image)
if err != nil {
return SourceUnknown, ""
}
imageSource := strings.TrimPrefix(image, u.Scheme+"://")
switch u.Scheme {
case SourceDockerEngine.String():
return SourceDockerEngine, imageSource
case SourcePodmanEngine.String():
return SourcePodmanEngine, imageSource
case SourceDockerArchive.String():
return SourceDockerArchive, imageSource
case "docker-tar":
return SourceDockerArchive, imageSource
}
return SourceUnknown, ""
}
func GetImageResolver(r ImageSource) (image.Resolver, error) {
switch r {
case SourceDockerEngine: