Add --output format flag to `skaffold build`
@@ -18,14 +18,17 @@ package cmd
import (
"context"
- "fmt"
"io"
+ "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags"
+ "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
+var buildFormatFlag = flags.NewTemplateFlag("{{range .Builds}}{{.ImageName}} -> {{.Tag}}\n{{end}}", BuildOutput{})
+
// NewCmdBuild describes the CLI command to build artifacts.
func NewCmdBuild(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
@@ -33,14 +36,20 @@ func NewCmdBuild(out io.Writer) *cobra.Command {
Short: "Builds the artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
- return build(out, filename)
+ return runBuild(out, filename)
},
}
AddRunDevFlags(cmd)
+ cmd.Flags().VarP(buildFormatFlag, "output", "o", buildFormatFlag.Usage())
return cmd
}
-func build(out io.Writer, filename string) error {
+// BuildOutput is the output of `skaffold build`.
+type BuildOutput struct {
+ Builds []build.Build
+}
+
+func runBuild(out io.Writer, filename string) error {
ctx := context.Background()
config, err := readConfiguration(filename)
@@ -58,9 +67,9 @@ func build(out io.Writer, filename string) error {
return errors.Wrap(err, "build step")
}
- for _, build := range bRes {
- fmt.Fprintln(out, build.ImageName, "->", build.Tag)
+ cmdOut := BuildOutput{Builds: bRes}
+ if err := buildFormatFlag.Template().Execute(out, cmdOut); err != nil {
+ return errors.Wrap(err, "executing template")
}
-
- return err
+ return nil
}
