This commit is contained in:
Mikaël Cluseau 2019-03-01 12:37:51 +11:00
parent ea6fce68e1
commit e6d281e1ae
3 changed files with 36 additions and 23 deletions

View File

@ -33,6 +33,8 @@ func loadSrc() {
func main() { func main() {
flag.Parse() flag.Parse()
log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
loadSrc() loadSrc()
dst = &localconfig.Config{ dst = &localconfig.Config{

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"path" "path"
"path/filepath"
"strings" "strings"
billy "gopkg.in/src-d/go-billy.v4" 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) file, err := tree.File(filePath)
if err != nil { if err == object.ErrFileNotFound {
return nil, nil
} else if err != nil {
return 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) { func (d *Defaults) ReadAll(rev, filePath string) (ba []byte, err error) {
rd, err := d.Open(rev, filePath) rd, err := d.Open(rev, filePath)
if err != nil || rd == nil {
return
}
return ioutil.ReadAll(rd) 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") { if !strings.HasSuffix(f.Name, ".yaml") {
return return
} }
names = append(names, strings.TrimSuffix(f.Name, ".yaml")) names = append(names, strings.TrimSuffix(filepath.Base(f.Name), ".yaml"))
return return
}) })
return return

View File

@ -22,17 +22,19 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
defaults, err := NewDefaults(defaultsPath) defaults, err := NewDefaults(defaultsPath)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to load defaults: %v", err)
} }
store := &dirStore{dirPath} store := &dirStore{dirPath}
load := func(dir, name string, out Rev) error { load := func(dir, name string, out Rev) error {
ba, err := store.Get(path.Join(dir, name)) ba, err := store.Get(path.Join(dir, name))
if err != nil { if err != nil {
return err return fmt.Errorf("failed to load %s/%s from dir: %v", dir, name, err)
} }
if err = defaults.Load(dir, ".yaml", out, ba); err != nil {
return defaults.Load(dir, ".yaml", out, ba) return fmt.Errorf("failed to enrich %s/%s from defaults: %v", dir, name, err)
}
return nil
} }
config := &Config{Addons: make(map[string][]*Template)} config := &Config{Addons: make(map[string][]*Template)}
@ -40,7 +42,7 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
// load clusters // load clusters
names, err := store.List("clusters") names, err := store.List("clusters")
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to list clusters: %v", err)
} }
for _, name := range names { for _, name := range names {
@ -55,16 +57,20 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
// load groups // load groups
names, err = store.List("groups") names, err = store.List("groups")
if err != nil { 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) { read := func(rev, filePath string) (data []byte, fromDefaults bool, err error) {
data, err = store.Get(filePath) data, err = store.Get(filePath)
if err != nil { if err != nil {
err = fmt.Errorf("faild to read %s: %v", filePath, err)
return return
} }
if data == nil { if data != nil {
return // ok
}
if len(rev) == 0 { if len(rev) == 0 {
err = fmt.Errorf("entry not found: %s", filePath) err = fmt.Errorf("entry not found: %s", filePath)
return return
@ -72,12 +78,11 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
data, err = defaults.ReadAll(rev, filePath+".yaml") data, err = defaults.ReadAll(rev, filePath+".yaml")
if err != nil { if err != nil {
err = fmt.Errorf("failed to read %s:%s: %v", rev, filePath, err)
return return
} }
fromDefaults = true fromDefaults = true
}
return return
} }
@ -136,7 +141,7 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
// load hosts // load hosts
names, err = store.List("hosts") names, err = store.List("hosts")
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to list hosts: %v", err)
} }
for _, name := range names { for _, name := range names {
@ -152,14 +157,14 @@ func FromDir(dirPath, defaultsPath string) (*Config, error) {
loadTemplates := func(rev, dir string, templates *[]*Template) error { loadTemplates := func(rev, dir string, templates *[]*Template) error {
names, err := store.List(dir) names, err := store.List(dir)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to list %s: %v", dir, err)
} }
if len(rev) != 0 { if len(rev) != 0 {
var defaultsNames []string var defaultsNames []string
defaultsNames, err = defaults.List(rev, dir) defaultsNames, err = defaults.List(rev, dir)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to list %s:%s: %v", rev, dir, err)
} }
names = append(names, defaultsNames...) names = append(names, defaultsNames...)