Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract functions into modules
- Loading branch information
Showing
4 changed files
with
91 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package images | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/asottile/dockerfile" | ||
) | ||
|
||
type Image struct { | ||
Name string `json:"name"` | ||
Tag string `json:"tag"` | ||
} | ||
|
||
// GetImages returns the `Image`s used in the given list of Dockerfile commands. | ||
func GetImages(commands []dockerfile.Command, unknownMarker string) []Image { | ||
images := []Image{} | ||
|
||
for _, cmd := range commands { | ||
if cmd.Cmd == "FROM" { | ||
full := cmd.Value | ||
rawImage := full[0] | ||
imageParts := strings.Split(rawImage, ":") | ||
image := imageParts[0] | ||
var version string | ||
|
||
if len(imageParts) > 1 { | ||
version = imageParts[1] | ||
} else { | ||
version = unknownMarker | ||
} | ||
|
||
images = append(images, Image{Name: image, Tag: version}) | ||
} | ||
} | ||
|
||
return images | ||
} | ||
|
||
// GetSingleTag returns the tag for the nth `occurrence` of `query` in | ||
// the given list of `Image`s. | ||
func GetSingleTag(images []Image, query string, occurrence int) (string, error) { | ||
found := 0 | ||
|
||
for _, i := range images { | ||
if i.Name == query { | ||
found += 1 | ||
|
||
if found >= occurrence { | ||
return i.Tag, nil | ||
} | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("could not find image %s in list", query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package input | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// GetInput opens the file named in `args` if present, `os.Stdin` if not. | ||
func GetInput(args []string) (*os.File, error) { | ||
if len(args) == 1 { | ||
name := args[0] | ||
f, err := os.Open(name) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not open %s: %s", name, err) | ||
} | ||
|
||
return f, nil | ||
} | ||
|
||
return os.Stdin, nil | ||
} |