Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: create separate
list
and query
sub-commands
- Loading branch information
Showing
6 changed files
with
153 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/asottile/dockerfile" | ||
"github.com/shivjm/dockerfile-image-tags/pkg/images" | ||
"github.com/shivjm/dockerfile-image-tags/pkg/input" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List images and tags", | ||
Long: "Print a list of the images and tags in the Dockerfile as a JSON array. Every `FROM` instruction in the file corresponds to an entry in the array, so the same image might appear multiple times, with the same or different tags. A special marker (default: `?`) will be returned if the tag is not specified.", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
file, err := input.GetInput(args) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not read Dockerfile: %s", err) | ||
} | ||
|
||
parsed, err := dockerfile.ParseReader(file) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not parse Dockerfile: %s\n", err) | ||
} | ||
|
||
allImages := images.GetImages(parsed, unknownMarker) | ||
|
||
val, err := json.Marshal(allImages) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not serialize images as JSON: %s\n", err) | ||
} | ||
|
||
fmt.Println(string(val)) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(listCmd) | ||
} |
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,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/asottile/dockerfile" | ||
"github.com/shivjm/dockerfile-image-tags/pkg/images" | ||
"github.com/shivjm/dockerfile-image-tags/pkg/input" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
occurrence int | ||
|
||
queryCmd = &cobra.Command{ | ||
Use: "query", | ||
Short: "Find single image tag", | ||
Long: "Print the tag for a specific image if found, exit with an error otherwise. The image may appear multiple times in the Dockerfile, in which case `occurrence` (default: 1) specifies which instance to return the tag for.", | ||
Args: cobra.RangeArgs(1, 2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var file *os.File | ||
var query string | ||
|
||
if len(args) == 2 { | ||
f, err := input.GetInput(args[0:1]) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not read Dockerfile: %s", err) | ||
} | ||
|
||
file = f | ||
query = args[1] | ||
} else { | ||
f, _ := input.GetInput([]string{}) | ||
|
||
file = f | ||
query = args[0] | ||
} | ||
|
||
parsed, err := dockerfile.ParseReader(file) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not parse Dockerfile: %s\n", err) | ||
} | ||
|
||
allImages := images.GetImages(parsed, unknownMarker) | ||
|
||
tag, err := images.GetSingleTag(allImages, query, occurrence) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not find image in Dockerfile: %s", query) | ||
} | ||
|
||
fmt.Println(tag) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
queryCmd.Flags().IntVarP(&occurrence, "occurrence", "n", 1, "which occurrence of image to return tag for") | ||
|
||
rootCmd.AddCommand(queryCmd) | ||
} |
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 cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
unknownMarker string | ||
|
||
rootCmd = &cobra.Command{ | ||
Use: "dockerfile-image-tags", | ||
Short: "List or query images and tags used in a Dockerfile.", | ||
} | ||
) | ||
|
||
func Execute() error { | ||
return rootCmd.Execute() | ||
} | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().StringVarP(&unknownMarker, "unknown-marker", "m", "?", "string to use to indicate unknown tag") | ||
} |
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 |
---|---|---|
@@ -1,66 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/asottile/dockerfile" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/shivjm/dockerfile-image-tags/pkg/images" | ||
"github.com/shivjm/dockerfile-image-tags/pkg/input" | ||
"github.com/shivjm/dockerfile-image-tags/cmd" | ||
) | ||
|
||
|
||
func main() { | ||
var unknownMarker string | ||
var query string | ||
var occurrence int | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "dockerfile-image-tags", | ||
Short: "List or query images & tags used in a Dockerfile.", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
file, err := input.GetInput(args) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not read Dockerfile: %s", err) | ||
} | ||
|
||
parsed, err := dockerfile.ParseReader(file) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not parse Dockerfile: %s\n", err) | ||
} | ||
|
||
allImages := images.GetImages(parsed, unknownMarker) | ||
|
||
val, err := json.Marshal(allImages) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not serialize images as JSON: %s\n", err) | ||
} | ||
|
||
if query == "" { | ||
fmt.Println(string(val)) | ||
} else { | ||
tag, err := images.GetSingleTag(allImages, query, occurrence) | ||
|
||
if err != nil { | ||
log.Fatalf("Could not find image in Dockerfile: %s", query) | ||
} | ||
|
||
fmt.Println(tag) | ||
} | ||
}, | ||
} | ||
rootCmd.Flags().StringVarP(&unknownMarker, "unknown-marker", "m", "?", "string to use to indicate unknown tags") | ||
rootCmd.Flags().StringVarP(&query, "query", "q", "", "single image to return tag for (see `occurrence`)") | ||
rootCmd.Flags().IntVarP(&occurrence, "occurrence", "n", 1, "which occurrence of image to return tag for") | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
cmd.Execute() | ||
} |
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