Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from PrFalken/master
Browse files Browse the repository at this point in the history
support file inclusion in env and service attributes
  • Loading branch information
n0rad authored Dec 13, 2016
2 parents 0c60390 + 4070648 commit 78752a5
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/common-attributes/test/includeFile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default:
includeGlobal: includeGlobalValue
1 change: 1 addition & 0 deletions examples/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workPath: ../examples
6 changes: 6 additions & 0 deletions examples/env/prod-dc1/attributes/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include:
- test.includeFile
- env:prod-dc2:test.includeFile

default:
attribute: attributeValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include:
- test.includeFile
- env:prod-dc2:test.includeFile
- env::test.includeFile
45 changes: 45 additions & 0 deletions work/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,55 @@ func (e *Env) loadAttributes() {
if err != nil {
logs.WithEF(err, e.fields).WithField("path", e.path+PATH_ATTRIBUTES).Fatal("Cannot load attribute files")
}
files, err = e.addIncludeFiles(files)
if err != nil {
logs.WithEF(err, e.fields).WithField("path", e.path+PATH_ATTRIBUTES).Fatal("Cannot load include files")
}

e.attributes = attributes.MergeAttributesFiles(files)
logs.WithFields(e.fields).WithField("attributes", e.attributes).Debug("Attributes loaded")
}

func (e *Env) addIncludeFiles(files []string) ([]string, error) {
type includeFiles struct {
Include []string
}
for _, file := range files {
var f includeFiles
yml, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yml, &f)
for _, inclusion := range f.Include {
sepCount := strings.Count(inclusion, ":")
if sepCount == 2 {
fields := strings.Split(inclusion, ":")
includeFile := strings.Replace(fields[2], ".", "/", -1) + ".yml"
if fields[1] == "" {
logs.WithField("include", inclusion).Fatal("Trying to include environment attributes from itself")
} else { // env:prod-dc1:some.include
includeFile = fmt.Sprintf("%v%v/%v%v/%v",
ggn.Home.Config.WorkPath,
PATH_ENV,
fields[1],
PATH_COMMON_ATTRIBUTES,
includeFile,
)
files = append(files, includeFile)
}
} else { // some.global.include
includeFile := strings.Replace(inclusion, ".", "/", -1) + ".yml"
includeFile = fmt.Sprintf("%v%v/%v", ggn.Home.Config.WorkPath, PATH_COMMON_ATTRIBUTES, includeFile)
files = append(files, includeFile)

}
}
}
return files, nil

}

func (e Env) ListServices() []string {
path := e.path + PATH_SERVICES
files, err := ioutil.ReadDir(path)
Expand Down
39 changes: 39 additions & 0 deletions work/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package work

import (
"testing"

"github.com/blablacar/ggn/ggn"
)

func itemInSlice(a string, s []string) bool {
for _, x := range s {
if a == x {
return true
}
}
return false
}

func TestAddEnvIncludeFiles(t *testing.T) {
ggn.Home.Config.WorkPath = "../examples"
env := Env{}
files := []string{"../examples/env/prod-dc1/attributes/test.yml"}
files, err := env.addIncludeFiles(files)
if err != nil {
t.Logf("addIncludeFiles returned an error : %v", err)
t.Fail()
}
for _, includeFile := range []string{
"../examples/common-attributes/test/includeFile.yml",
"../examples/env/prod-dc2/common-attributes/test/includeFile.yml",
} {
if !itemInSlice(includeFile, files) {
t.Logf("File not included : %v", includeFile)
for _, f := range files {
t.Log(f)
}
t.Fail()
}
}
}
49 changes: 49 additions & 0 deletions work/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,60 @@ func (s *Service) loadAttributes() {
if err != nil {
logs.WithEF(err, s.fields).WithField("path", s.path+PATH_ATTRIBUTES).Fatal("Cannot load Attributes files")
}
files, err = s.addIncludeFiles(files)
if err != nil {
logs.WithEF(err, s.fields).WithField("path", s.path+PATH_ATTRIBUTES).Fatal("Cannot load include files")
}
attr = attributes.MergeAttributesFilesForMap(attr, files)
s.attributes = attr
logs.WithFields(s.fields).WithField("attributes", s.attributes).Debug("Attributes loaded")
}

func (s *Service) addIncludeFiles(files []string) ([]string, error) {
type includeFiles struct {
Include []string
}
for _, file := range files {
var f includeFiles
yml, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yml, &f)
for _, inclusion := range f.Include {
sepCount := strings.Count(inclusion, ":")
if sepCount == 2 {
fields := strings.Split(inclusion, ":")
includeFile := strings.Replace(fields[2], ".", "/", -1) + ".yml"
if fields[1] == "" { // env::some.include
includeFile = fmt.Sprintf("%v%v/%v",
s.env.path,
PATH_COMMON_ATTRIBUTES,
includeFile,
)
files = append(files, includeFile)
} else { // env:prod-dc1:some.include
includeFile = fmt.Sprintf("%v%v/%v%v/%v",
ggn.Home.Config.WorkPath,
PATH_ENV,
fields[1],
PATH_COMMON_ATTRIBUTES,
includeFile,
)
files = append(files, includeFile)
}

} else { // some.global.include
includeFile := strings.Replace(inclusion, ".", "/", -1) + ".yml"
includeFile = fmt.Sprintf("%v%v/%v", ggn.Home.Config.WorkPath, PATH_COMMON_ATTRIBUTES, includeFile)
files = append(files, includeFile)

}
}
}
return files, nil
}

func (s *Service) loadUnitTemplate(filename string) (*template.Templating, error) {
path := s.path + filename
source, err := ioutil.ReadFile(path)
Expand Down
27 changes: 27 additions & 0 deletions work/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package work

import "testing"

func TestAddServiceIncludeFiles(t *testing.T) {
service := Service{}
service.env.path = "../examples/env/prod-dc1"
files := []string{"../examples/env/prod-dc1/services/test-service/attributes/test.yml"}
files, err := service.addIncludeFiles(files)
if err != nil {
t.Logf("addIncludeFiles returned an error : %v", err)
t.Fail()
}
for _, includeFile := range []string{
"../examples/common-attributes/test/includeFile.yml",
"../examples/env/prod-dc2/common-attributes/test/includeFile.yml",
"../examples/env/prod-dc1/common-attributes/test/includeFile.yml",
} {
if !itemInSlice(includeFile, files) {
t.Logf("File not included : %v", includeFile)
for _, f := range files {
t.Log(f)
}
t.Fail()
}
}
}
1 change: 1 addition & 0 deletions work/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

const PATH_ATTRIBUTES = "/attributes"
const PATH_TEMPLATES = "/templates"
const PATH_COMMON_ATTRIBUTES = "/common-attributes"

const ACTIVE_ACTIVE = "active"
const SUB_RUNNING = "running"
Expand Down

0 comments on commit 78752a5

Please sign in to comment.