Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --analyze flag to skaffold init #1725

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -52,6 +53,7 @@ var (
cliArtifacts []string
skipBuild bool
force bool
analyze bool
)

// NewCmdInit describes the CLI command to generate a Skaffold configuration.
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions cmd/skaffold/app/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"bytes"
"fmt"
"testing"

Expand Down Expand Up @@ -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())
})
}
}
2 changes: 2 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down