fix: follow up for the relative paths improvement (#262)

Fixes for the bugs that are introduced by #261, that is values.yaml files specified in `values:` have redundant base path in their prefixes, and remaining .dec files after secrets decryption(https://github.com/roboll/helmfile/issues/251#issuecomment-417285854)
This commit is contained in:
KUOKA Yusuke 2018-08-30 21:59:59 +09:00 committed by GitHub
parent 421299c883
commit bb3b44e511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 9 deletions

View File

@ -120,7 +120,8 @@ func (helm *execer) DecryptSecret(name string) (string, error) {
// os.Rename seems to results in "cross-device link` errors in some cases // os.Rename seems to results in "cross-device link` errors in some cases
// Instead of moving, copy it to the destination temp file as a work-around // Instead of moving, copy it to the destination temp file as a work-around
// See https://github.com/roboll/helmfile/issues/251#issuecomment-417166296f // See https://github.com/roboll/helmfile/issues/251#issuecomment-417166296f
decFile, err := os.Open(name + ".dec") decFilename := name + ".dec"
decFile, err := os.Open(decFilename)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -131,6 +132,14 @@ func (helm *execer) DecryptSecret(name string) (string, error) {
return "", err return "", err
} }
if err := decFile.Close(); err != nil {
return "", err
}
if err := os.Remove(decFilename); err != nil {
return "", err
}
return tmpFile.Name(), err return tmpFile.Name(), err
} }

View File

@ -1,5 +1,6 @@
package tmpl package tmpl
type Context struct { type Context struct {
basePath string
readFile func(string) ([]byte, error) readFile func(string) ([]byte, error)
} }

View File

@ -2,11 +2,9 @@ package tmpl
import ( import (
"bytes" "bytes"
"path/filepath"
) )
type templateFileRenderer struct { type templateFileRenderer struct {
basePath string
ReadFile func(string) ([]byte, error) ReadFile func(string) ([]byte, error)
Context *Context Context *Context
} }
@ -17,19 +15,16 @@ type FileRenderer interface {
func NewFileRenderer(readFile func(filename string) ([]byte, error), basePath string) *templateFileRenderer { func NewFileRenderer(readFile func(filename string) ([]byte, error), basePath string) *templateFileRenderer {
return &templateFileRenderer{ return &templateFileRenderer{
basePath: basePath,
ReadFile: readFile, ReadFile: readFile,
Context: &Context{ Context: &Context{
basePath: basePath,
readFile: readFile, readFile: readFile,
}, },
} }
} }
func (r *templateFileRenderer) RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) { func (r *templateFileRenderer) RenderTemplateFileToBuffer(file string) (*bytes.Buffer, error) {
// path to the file relative to the helmfile.yaml content, err := r.ReadFile(file)
path := filepath.Join(r.basePath, file)
content, err := r.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"os" "os"
"path/filepath"
"strings" "strings"
"text/template" "text/template"
) )
@ -21,7 +22,9 @@ func (c *Context) createFuncMap() template.FuncMap {
} }
func (c *Context) ReadFile(filename string) (string, error) { func (c *Context) ReadFile(filename string) (string, error) {
bytes, err := c.readFile(filename) path := filepath.Join(c.basePath, filename)
bytes, err := c.readFile(path)
if err != nil { if err != nil {
return "", err return "", err
} }