diff --git a/cmd/flux/build_artifact.go b/cmd/flux/build_artifact.go index 591d4995..d98e7d44 100644 --- a/cmd/flux/build_artifact.go +++ b/cmd/flux/build_artifact.go @@ -17,7 +17,10 @@ limitations under the License. package main import ( + "bufio" + "bytes" "fmt" + "io" "os" "strings" @@ -34,6 +37,9 @@ var buildArtifactCmd = &cobra.Command{ Example: ` # Build the given manifests directory into an artifact flux build artifact --path ./path/to/local/manifests --output ./path/to/artifact.tgz + # Build manifests fro stdin to an artifact + kustomize build . | flux build artifact --output ./path/to/artifact.tgz --path - + # Build the given single manifest file into an artifact flux build artifact --path ./path/to/local/manifest.yaml --output ./path/to/artifact.tgz @@ -54,7 +60,7 @@ var excludeOCI = append(strings.Split(sourceignore.ExcludeVCS, ","), strings.Spl var buildArtifactArgs buildArtifactFlags func init() { - buildArtifactCmd.Flags().StringVar(&buildArtifactArgs.path, "path", "", "Path to the directory where the Kubernetes manifests are located.") + buildArtifactCmd.Flags().StringVarP(&buildArtifactArgs.path, "path", "p", "", "Path to the directory where the Kubernetes manifests are located.") buildArtifactCmd.Flags().StringVarP(&buildArtifactArgs.output, "output", "o", "artifact.tgz", "Path to where the artifact tgz file should be written.") buildArtifactCmd.Flags().StringSliceVar(&buildArtifactArgs.ignorePaths, "ignore-paths", excludeOCI, "set paths to ignore in .gitignore format") @@ -66,18 +72,47 @@ func buildArtifactCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("invalid path %q", buildArtifactArgs.path) } - if _, err := os.Stat(buildArtifactArgs.path); err != nil { - return fmt.Errorf("invalid path '%s', must point to an existing directory or file", buildArtifactArgs.path) + path := buildArtifactArgs.path + var err error + if buildArtifactArgs.path == "-" { + path, err = saveStdinToFile() + if err != nil { + return err + } + + defer os.Remove(path) + } + + if _, err := os.Stat(path); err != nil { + return fmt.Errorf("invalid path '%s', must point to an existing directory or file", path) } - logger.Actionf("building artifact from %s", buildArtifactArgs.path) + logger.Actionf("building artifact from %s", path) ociClient := oci.NewLocalClient() - if err := ociClient.Build(buildArtifactArgs.output, buildArtifactArgs.path, buildArtifactArgs.ignorePaths); err != nil { + if err := ociClient.Build(buildArtifactArgs.output, path, buildArtifactArgs.ignorePaths); err != nil { return fmt.Errorf("bulding artifact failed, error: %w", err) } logger.Successf("artifact created at %s", buildArtifactArgs.output) - return nil } + +func saveStdinToFile() (string, error) { + b, err := io.ReadAll(bufio.NewReader(os.Stdin)) + if err != nil { + return "", err + } + b = bytes.TrimRight(b, "\r\n") + f, err := os.CreateTemp("", "*.yaml") + if err != nil { + return "", fmt.Errorf("unable to create temp dir for stdin") + } + + _, err = f.Write(b) + if err != nil { + return "", fmt.Errorf("error writing stdin to file: %w", err) + } + + return f.Name(), nil +} diff --git a/cmd/flux/push_artifact.go b/cmd/flux/push_artifact.go index 1c2725e2..c80dd9d6 100644 --- a/cmd/flux/push_artifact.go +++ b/cmd/flux/push_artifact.go @@ -159,7 +159,17 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error { logger.Actionf("pushing artifact to %s", url) - digest, err := ociClient.Push(ctx, url, pushArtifactArgs.path, meta, pushArtifactArgs.ignorePaths) + path := pushArtifactArgs.path + if buildArtifactArgs.path == "-" { + path, err = saveStdinToFile() + if err != nil { + return err + } + + defer os.Remove(path) + } + + digest, err := ociClient.Push(ctx, url, path, meta, pushArtifactArgs.ignorePaths) if err != nil { return fmt.Errorf("pushing artifact failed: %w", err) }