From e6d281e1aea9611e2e7bbbf5c6635b87725709fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Fri, 1 Mar 2019 12:37:51 +1100 Subject: [PATCH] fixes --- cmd/dkl-dir2config/main.go | 2 ++ pkg/clustersconfig/defaults.go | 10 ++++++-- pkg/clustersconfig/dir.go | 47 +++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/cmd/dkl-dir2config/main.go b/cmd/dkl-dir2config/main.go index 70938dc..bceb80a 100644 --- a/cmd/dkl-dir2config/main.go +++ b/cmd/dkl-dir2config/main.go @@ -33,6 +33,8 @@ func loadSrc() { func main() { flag.Parse() + log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile) + loadSrc() dst = &localconfig.Config{ diff --git a/pkg/clustersconfig/defaults.go b/pkg/clustersconfig/defaults.go index 2d43f68..42ed2c2 100644 --- a/pkg/clustersconfig/defaults.go +++ b/pkg/clustersconfig/defaults.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "path" + "path/filepath" "strings" billy "gopkg.in/src-d/go-billy.v4" @@ -75,7 +76,9 @@ func (d *Defaults) Open(rev, filePath string) (rd io.Reader, err error) { } file, err := tree.File(filePath) - if err != nil { + if err == object.ErrFileNotFound { + return nil, nil + } else if err != nil { return } @@ -84,6 +87,9 @@ func (d *Defaults) Open(rev, filePath string) (rd io.Reader, err error) { func (d *Defaults) ReadAll(rev, filePath string) (ba []byte, err error) { rd, err := d.Open(rev, filePath) + if err != nil || rd == nil { + return + } return ioutil.ReadAll(rd) } @@ -97,7 +103,7 @@ func (d *Defaults) List(rev, dir string) (names []string, err error) { if !strings.HasSuffix(f.Name, ".yaml") { return } - names = append(names, strings.TrimSuffix(f.Name, ".yaml")) + names = append(names, strings.TrimSuffix(filepath.Base(f.Name), ".yaml")) return }) return diff --git a/pkg/clustersconfig/dir.go b/pkg/clustersconfig/dir.go index 519ec3d..9845708 100644 --- a/pkg/clustersconfig/dir.go +++ b/pkg/clustersconfig/dir.go @@ -22,17 +22,19 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) { defaults, err := NewDefaults(defaultsPath) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to load defaults: %v", err) } store := &dirStore{dirPath} load := func(dir, name string, out Rev) error { ba, err := store.Get(path.Join(dir, name)) if err != nil { - return err + return fmt.Errorf("failed to load %s/%s from dir: %v", dir, name, err) } - - return defaults.Load(dir, ".yaml", out, ba) + if err = defaults.Load(dir, ".yaml", out, ba); err != nil { + return fmt.Errorf("failed to enrich %s/%s from defaults: %v", dir, name, err) + } + return nil } config := &Config{Addons: make(map[string][]*Template)} @@ -40,7 +42,7 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) { // load clusters names, err := store.List("clusters") if err != nil { - return nil, err + return nil, fmt.Errorf("failed to list clusters: %v", err) } for _, name := range names { @@ -55,29 +57,32 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) { // load groups names, err = store.List("groups") if err != nil { - return nil, err + return nil, fmt.Errorf("failed to list groups: %v", err) } read := func(rev, filePath string) (data []byte, fromDefaults bool, err error) { data, err = store.Get(filePath) if err != nil { + err = fmt.Errorf("faild to read %s: %v", filePath, err) return } - if data == nil { - if len(rev) == 0 { - err = fmt.Errorf("entry not found: %s", filePath) - return - } - - data, err = defaults.ReadAll(rev, filePath+".yaml") - if err != nil { - return - } - - fromDefaults = true + if data != nil { + return // ok } + if len(rev) == 0 { + err = fmt.Errorf("entry not found: %s", filePath) + return + } + + data, err = defaults.ReadAll(rev, filePath+".yaml") + if err != nil { + err = fmt.Errorf("failed to read %s:%s: %v", rev, filePath, err) + return + } + + fromDefaults = true return } @@ -136,7 +141,7 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) { // load hosts names, err = store.List("hosts") if err != nil { - return nil, err + return nil, fmt.Errorf("failed to list hosts: %v", err) } for _, name := range names { @@ -152,14 +157,14 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) { loadTemplates := func(rev, dir string, templates *[]*Template) error { names, err := store.List(dir) if err != nil { - return err + return fmt.Errorf("failed to list %s: %v", dir, err) } if len(rev) != 0 { var defaultsNames []string defaultsNames, err = defaults.List(rev, dir) if err != nil { - return err + return fmt.Errorf("failed to list %s:%s: %v", rev, dir, err) } names = append(names, defaultsNames...)