1
0
mirror of synced 2026-02-06 19:05:55 +00:00

fix path on flux push

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
Somtochi Onyekwere
2022-12-17 06:58:08 +01:00
parent 5e44b7b1b3
commit d79e49f80b
3 changed files with 88 additions and 17 deletions

View File

@@ -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)
}

View File

@@ -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))
})
}
}

View File

@@ -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)