fixed bug that caused helm create to not overwrite modified files

Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>
pull/8751/head
Matt Butcher 4 years ago
parent 459dcd7f72
commit 106f1fb45c
No known key found for this signature in database
GPG Key ID: DCD5F5E5EF32C345

@ -107,6 +107,7 @@ func (o *createOptions) run(out io.Writer) error {
return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter) return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter)
} }
chartutil.Stderr = out
_, err := chartutil.Create(chartname, filepath.Dir(o.name)) _, err := chartutil.Create(chartname, filepath.Dir(o.name))
return err return err
} }

@ -18,6 +18,7 @@ package chartutil
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -468,6 +469,12 @@ spec:
restartPolicy: Never restartPolicy: Never
` `
// Stderr is an io.Writer to which error messages can be written
//
// In Helm 4, this will be replaced. It is needed in Helm 3 to preserve API backward
// compatibility.
var Stderr io.Writer = os.Stderr
// CreateFrom creates a new chart, but scaffolds it from the src chart. // CreateFrom creates a new chart, but scaffolds it from the src chart.
func CreateFrom(chartfile *chart.Metadata, dest, src string) error { func CreateFrom(chartfile *chart.Metadata, dest, src string) error {
schart, err := loader.Load(src) schart, err := loader.Load(src)
@ -601,8 +608,8 @@ func Create(name, dir string) (string, error) {
for _, file := range files { for _, file := range files {
if _, err := os.Stat(file.path); err == nil { if _, err := os.Stat(file.path); err == nil {
// File exists and is okay. Skip it. // There is no handle to a preferred output stream here.
continue fmt.Fprintf(Stderr, "WARNING: File %q already exists. Overwriting.\n", file.path)
} }
if err := writeFile(file.path, file.content); err != nil { if err := writeFile(file.path, file.content); err != nil {
return cdir, err return cdir, err

@ -117,3 +117,42 @@ func TestCreateFrom(t *testing.T) {
} }
} }
} }
// TestCreate_Overwrite is a regression test for making sure that files are overwritten.
func TestCreate_Overwrite(t *testing.T) {
tdir, err := ioutil.TempDir("", "helm-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tdir)
var errlog bytes.Buffer
if _, err := Create("foo", tdir); err != nil {
t.Fatal(err)
}
dir := filepath.Join(tdir, "foo")
tplname := filepath.Join(dir, "templates/hpa.yaml")
writeFile(tplname, []byte("FOO"))
// Now re-run the create
Stderr = &errlog
if _, err := Create("foo", tdir); err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadFile(tplname)
if err != nil {
t.Fatal(err)
}
if string(data) == "FOO" {
t.Fatal("File that should have been modified was not.")
}
if errlog.Len() == 0 {
t.Errorf("Expected warnings about overwriting files.")
}
}

Loading…
Cancel
Save