From bea4d3312b2b0b32cf352334800accc10a173824 Mon Sep 17 00:00:00 2001 From: Shiv Jha-Mathur Date: Sun, 7 Nov 2021 22:13:22 +0530 Subject: [PATCH] refactor: use cobra to parse arguments --- go.mod | 1 + go.sum | 3 +++ main.go | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 86aacfd..f271e25 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5fe318a..166a43e 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -952,6 +953,7 @@ 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= @@ -959,6 +961,7 @@ github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzu 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= diff --git a/main.go b/main.go index 6c13f87..655e0ed 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/asottile/dockerfile" + "github.com/spf13/cobra" ) const ( @@ -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 {