From e6dba2c15d5b60c7099e4a6f870b96d12cad27a8 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 4 Mar 2019 13:37:12 -0800 Subject: [PATCH] Add --analyze flag to skaffold init The --analyze flag will print out all dockerfiles and images found in the workspace in JSON format. Should fix #1710 --- cmd/skaffold/app/cmd/init.go | 23 +++++++++++ cmd/skaffold/app/cmd/init_test.go | 38 +++++++++++++++++++ docs/content/en/docs/references/cli/_index.md | 2 + 3 files changed, 63 insertions(+) diff --git a/cmd/skaffold/app/cmd/init.go b/cmd/skaffold/app/cmd/init.go index a666567269e..c85b023e306 100644 --- a/cmd/skaffold/app/cmd/init.go +++ b/cmd/skaffold/app/cmd/init.go @@ -18,6 +18,7 @@ package cmd import ( "bufio" + "encoding/json" "fmt" "io" "io/ioutil" @@ -52,6 +53,7 @@ var ( cliArtifacts []string skipBuild bool force bool + analyze bool ) // NewCmdInit describes the CLI command to generate a Skaffold configuration. @@ -69,6 +71,7 @@ func NewCmdInit(out io.Writer) *cobra.Command { cmd.Flags().BoolVar(&force, "force", false, "Force the generation of the Skaffold config") cmd.Flags().StringVar(&composeFile, "compose-file", "", "Initialize from a docker-compose file") cmd.Flags().StringArrayVarP(&cliArtifacts, "artifact", "a", nil, "'='-delimited dockerfile/image pair to generate build artifact\n(example: --artifact=/web/Dockerfile.web=gcr.io/web-project/image)") + cmd.Flags().BoolVar(&analyze, "analyze", false, "Print all discoverable Dockerfiles and images in JSON format to stdout") return cmd } @@ -125,6 +128,10 @@ func doInit(out io.Writer) error { } } + if analyze { + return printAnalyzeJSON(out, dockerfiles, images) + } + var pairs []dockerfilePair // conditionally generate build artifacts if !skipBuild { @@ -189,6 +196,22 @@ func doInit(out io.Writer) error { return nil } +func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error { + a := struct { + Dockerfiles []string `json:"dockerfiles,omitempty"` + Images []string `json:"images,omitempty"` + }{ + Dockerfiles: dockerfiles, + Images: images, + } + contents, err := json.Marshal(a) + if err != nil { + return errors.Wrap(err, "marshalling contents") + } + _, err = out.Write(contents) + return err +} + func processCliArtifacts(artifacts []string) ([]dockerfilePair, error) { var pairs []dockerfilePair for _, artifact := range artifacts { diff --git a/cmd/skaffold/app/cmd/init_test.go b/cmd/skaffold/app/cmd/init_test.go index db2c17c794d..4cb55d83880 100644 --- a/cmd/skaffold/app/cmd/init_test.go +++ b/cmd/skaffold/app/cmd/init_test.go @@ -17,6 +17,7 @@ limitations under the License. package cmd import ( + "bytes" "fmt" "testing" @@ -60,3 +61,40 @@ deploy: testutil.CheckErrorAndDeepEqual(t, false, err, expectedYaml, string(buf)) } + +func TestPrintAnalyzeJSON(t *testing.T) { + tests := []struct { + name string + dockerfiles []string + images []string + expected string + }{ + { + name: "dockerfile and image", + dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, + images: []string{"image1", "image2"}, + expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}", + }, + { + name: "no dockerfile", + images: []string{"image1", "image2"}, + expected: "{\"images\":[\"image1\",\"image2\"]}", + }, + { + name: "no images", + dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, + expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"]}", + }, + { + name: "no dockerfiles or images", + expected: "{}", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + out := bytes.NewBuffer([]byte{}) + err := printAnalyzeJSON(out, test.dockerfiles, test.images) + testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, out.String()) + }) + } +} diff --git a/docs/content/en/docs/references/cli/_index.md b/docs/content/en/docs/references/cli/_index.md index 58d160f837f..c6227b2c8ad 100644 --- a/docs/content/en/docs/references/cli/_index.md +++ b/docs/content/en/docs/references/cli/_index.md @@ -335,6 +335,7 @@ Usage: skaffold init [flags] Flags: + --analyze Print all discoverable Dockerfiles and images in JSON format to stdout -a, --artifact stringArray '='-delimited dockerfile/image pair to generate build artifact (example: --artifact=/web/Dockerfile.web=gcr.io/web-project/image) --compose-file string Initialize from a docker-compose file @@ -350,6 +351,7 @@ Global Flags: ``` Env vars: +* `SKAFFOLD_ANALYZE` (same as --analyze) * `SKAFFOLD_ARTIFACT` (same as --artifact) * `SKAFFOLD_COMPOSE_FILE` (same as --compose-file) * `SKAFFOLD_FILENAME` (same as --filename)