From d79e49f80b61499e83bddde873de3789fbaced67 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Sat, 17 Dec 2022 06:58:08 +0100 Subject: [PATCH] fix path on flux push Signed-off-by: Somtochi Onyekwere --- cmd/flux/build_artifact.go | 11 +++--- cmd/flux/build_artifact_test.go | 70 +++++++++++++++++++++++++++++++++ cmd/flux/push_artifact.go | 24 +++++------ 3 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 cmd/flux/build_artifact_test.go diff --git a/cmd/flux/build_artifact.go b/cmd/flux/build_artifact.go index 48dd8872..b0c4a931 100644 --- a/cmd/flux/build_artifact.go +++ b/cmd/flux/build_artifact.go @@ -72,7 +72,7 @@ func buildArtifactCmdRun(cmd *cobra.Command, args []string) error { path := buildArtifactArgs.path var err error if buildArtifactArgs.path == "-" { - path, err = saveStdinToFile() + path, err = saveReaderToFile(os.Stdin) if err != nil { return err } @@ -95,8 +95,8 @@ func buildArtifactCmdRun(cmd *cobra.Command, args []string) error { return nil } -func saveStdinToFile() (string, error) { - b, err := io.ReadAll(bufio.NewReader(os.Stdin)) +func saveReaderToFile(reader io.Reader) (string, error) { + b, err := io.ReadAll(bufio.NewReader(reader)) if err != nil { return "", err } @@ -106,8 +106,9 @@ func saveStdinToFile() (string, error) { return "", fmt.Errorf("unable to create temp dir for stdin") } - _, err = f.Write(b) - if err != nil { + defer f.Close() + + if _, err := f.Write(b); err != nil { return "", fmt.Errorf("error writing stdin to file: %w", err) } diff --git a/cmd/flux/build_artifact_test.go b/cmd/flux/build_artifact_test.go new file mode 100644 index 00000000..37ed8f02 --- /dev/null +++ b/cmd/flux/build_artifact_test.go @@ -0,0 +1,70 @@ +/* +Copyright 2022 The Flux authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "os" + "strings" + "testing" + + . "github.com/onsi/gomega" +) + +func Test_saveReaderToFile(t *testing.T) { + g := NewWithT(t) + + testString := `apiVersion: v1 +kind: ConfigMap +metadata: + name: myapp +data: + foo: bar` + + tests := []struct { + name string + string string + expectErr bool + }{ + { + name: "yaml", + string: testString, + }, + { + name: "yaml with carriage return", + string: testString + "\r\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpFile, err := saveReaderToFile(strings.NewReader(tt.string)) + g.Expect(err).To(BeNil()) + + defer os.Remove(tmpFile) + + b, err := os.ReadFile(tmpFile) + if tt.expectErr { + g.Expect(err).To(Not(BeNil())) + return + } + + g.Expect(err).To(BeNil()) + g.Expect(string(b)).To(BeEquivalentTo(testString)) + }) + + } +} diff --git a/cmd/flux/push_artifact.go b/cmd/flux/push_artifact.go index 75384d2d..66d6807b 100644 --- a/cmd/flux/push_artifact.go +++ b/cmd/flux/push_artifact.go @@ -129,8 +129,18 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error { return err } - if _, err := os.Stat(pushArtifactArgs.path); err != nil { - return fmt.Errorf("invalid path '%s', must point to an existing directory or file", buildArtifactArgs.path) + path := pushArtifactArgs.path + if pushArtifactArgs.path == "-" { + path, err = saveReaderToFile(os.Stdin) + 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: %w", path, err) } meta := oci.Metadata{ @@ -164,16 +174,6 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error { logger.Actionf("pushing artifact to %s", url) - 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)