Skip to content

Commit

Permalink
refactor: use cobra to parse arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
shivjm committed Nov 7, 2021
1 parent 6b2ac0b commit bea4d33
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -5,5 +5,6 @@ go 1.16
require (
github.com/asottile/dockerfile v3.1.0+incompatible
github.com/moby/buildkit v0.9.2 // indirect
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.7.0
)
3 changes: 3 additions & 0 deletions go.sum
Expand Up @@ -646,6 +646,7 @@ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ishidawataru/sctp v0.0.0-20191218070446-00ab2ac2db07/go.mod h1:co9pwDoBCm1kGxawmb4sPq0cSIOOWNPT4KnHotMP1Zg=
github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee/go.mod h1:co9pwDoBCm1kGxawmb4sPq0cSIOOWNPT4KnHotMP1Zg=
Expand Down Expand Up @@ -952,13 +953,15 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
Expand Down
42 changes: 27 additions & 15 deletions main.go
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/asottile/dockerfile"
"github.com/spf13/cobra"
)

const (
Expand All @@ -20,33 +21,44 @@ type Image struct {
}

func main() {
file, err := getInput(os.Args)
var rootCmd = &cobra.Command{
Use: "dockerfile-image-tags",
Short: "List images & tags used in a Dockerfile.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
file, err := getInput(args)

if err != nil {
log.Fatalf("Could not read Dockerfile: %s", err)
}

if err != nil {
log.Fatalf("Could not read Dockerfile: %s", err)
}
parsed, err := dockerfile.ParseReader(file)

parsed, err := dockerfile.ParseReader(file)
if err != nil {
log.Fatalf("Could not parse Dockerfile: %s\n", err)
}

if err != nil {
log.Fatalf("Could not parse Dockerfile: %s\n", err)
}
images := getTags(parsed, UNKNOWN_MARKER)

images := getTags(parsed, UNKNOWN_MARKER)
val, err := json.Marshal(images)

val, err := json.Marshal(images)
if err != nil {
log.Fatalf("Could not serialize images as JSON: %s\n", err)
}

if err != nil {
log.Fatalf("Could not serialize images as JSON: %s\n", err)
fmt.Println(string(val))
},
}

fmt.Println(string(val))
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}

// 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[1]
if len(args) == 1 {
name := args[0]
f, err := os.Open(name)

if err != nil {
Expand Down

0 comments on commit bea4d33

Please sign in to comment.