Skip to content

Commit

Permalink
make use of curly-braces in yaml files optional
Browse files Browse the repository at this point in the history
  • Loading branch information
herrjulz committed Oct 5, 2018
1 parent 45b2895 commit ed93f4e
Show file tree
Hide file tree
Showing 29 changed files with 933 additions and 329 deletions.
82 changes: 73 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If you have to handle rather complex YAML files (for BOSH or Concourse), you jus
### OS X

```
$ wget -O /usr/local/bin/aviator https://github.com/JulzDiverse/aviator/releases/download/v0.20.0/aviator-darwin-amd64 && chmod +x /usr/local/bin/aviator
$ wget -O /usr/local/bin/aviator https://github.com/JulzDiverse/aviator/releases/download/v1.0.0/aviator-darwin-amd64 && chmod +x /usr/local/bin/aviator
```

**Via Homebrew**
Expand All @@ -26,13 +26,13 @@ $ brew install aviator
### Linux

```
$ wget -O /usr/bin/aviator https://github.com/JulzDiverse/aviator/releases/download/v0.20.0/aviator-linux-amd64 && chmod +x /usr/bin/aviator
$ wget -O /usr/bin/aviator https://github.com/JulzDiverse/aviator/releases/download/v1.0.0/aviator-linux-amd64 && chmod +x /usr/bin/aviator
```

### Windows (NOT TESTED)

```
https://github.com/JulzDiverse/aviator/releases/download/v0.20.0/aviator-win
https://github.com/JulzDiverse/aviator/releases/download/v1.0.0/aviator-win
```

## Executors
Expand Down Expand Up @@ -590,6 +590,29 @@ Note, that the generated `pipeline.yml` is used in the `fly` section as `config`

_NOTE: You will need to fly login first, before executing `aviator`_


---

### CLI Options

#### `--curly-braces`

Some YAML based tools (like concourse in the past) are using `{{}}` sytnax. This is not YAML conform. Using the `--curly-braces` option you can allow this syntax.

#### `--silent`

This option will output no infromation to stdout.

#### `--verbose`

This option prints which files are excluded from a merge.

#### `--var`

You can provide variables to the aviator file.

---

# Development

```
Expand Down
4 changes: 4 additions & 0 deletions cmd/aviator/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func getFlags() []cli.Flag {
Name: "var",
Usage: "provides a variable to an aviator file: [key=value]",
},
cli.BoolFlag{
Name: "curly-braces, b",
Usage: "allow {{}} syntax in yaml files",
},
}
return flags
}
10 changes: 7 additions & 3 deletions cmd/aviator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ func main() {
cmd.Action = func(c *cli.Context) error {
aviatorFile := c.String("file")
if !verifyAviatorFileExists(aviatorFile) {

exitWithNoAviatorFile()

} else {
vars := c.StringSlice("var")
varsMap := varsToMap(vars)

aviatorYml, err := ioutil.ReadFile(aviatorFile)
exitWithError(err)

cockpit := cockpit.New()
cockpit := cockpit.New(c.Bool("curly-braces"))
aviator, err := cockpit.NewAviator(aviatorYml, varsMap)
handleError(err)

err = aviator.ProcessSprucePlan(c.Bool("verbose"), c.Bool("silent"))
exitWithError(err)

squash := aviator.AviatorYaml.Squash
if len(squash.Content) != 0 {
err = aviator.ProcessSquashPlan()
exitWithError(err)
}

fly := aviator.AviatorYaml.Fly
if fly.Name != "" && fly.Target != "" && fly.Config != "" {
err = aviator.ExecuteFly()
Expand Down
40 changes: 38 additions & 2 deletions cockpit/cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/JulzDiverse/aviator"
"github.com/JulzDiverse/aviator/evaluator"
"github.com/JulzDiverse/aviator/executor"
"github.com/JulzDiverse/aviator/filemanager"
"github.com/JulzDiverse/aviator/processor"
"github.com/JulzDiverse/aviator/squasher"
"github.com/JulzDiverse/aviator/validator"
"github.com/JulzDiverse/osenv"
"github.com/pkg/errors"
Expand Down Expand Up @@ -34,9 +36,9 @@ func Init(
return &Cockpit{spruceProcessor, flyExecuter, validator}
}

func New() *Cockpit {
func New(curlyBraces bool) *Cockpit {
return &Cockpit{
spruceProcessor: processor.New(),
spruceProcessor: processor.New(curlyBraces),
validator: validator.New(),
flyExecutor: executor.NewFlyExecutor(),
}
Expand Down Expand Up @@ -76,6 +78,40 @@ func (a *Aviator) ProcessSprucePlan(verbose bool, silent bool) error {
return nil
}

func (a *Aviator) ProcessSquashPlan() error {
var err error
var result []byte

store := filemanager.Store(false)
fp := processor.FileProcessor{store}

content := a.AviatorYaml.Squash.Content
for _, c := range content {
var squashed []byte
if len(c.Files) != 0 {
files := store.ReadFiles(c.Files)
squashed, err = squasher.Squash(files)
} else {
paths := fp.CollectFilesFromDir(c.Dir, "", []string{})
files := store.ReadFiles(paths)
squashed, err = squasher.Squash(files)
}

if err != nil {
return err
}

result = append(result, squashed...)
}

err = store.WriteFile(a.AviatorYaml.Squash.To, result)
if err != nil {
return err
}

return nil
}

func (a *Aviator) ExecuteFly() error {
err := a.cockpit.flyExecutor.Execute(a.AviatorYaml.Fly)
if err != nil {
Expand Down
21 changes: 17 additions & 4 deletions filemanager/filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type FileManager struct {
root *mingoak.Dir
CurlyBraces bool
root *mingoak.Dir
}

//var quoteRegexOld = `\{\{([-\_\.\/\w\p{L}\/]+)\}\}`
Expand All @@ -21,9 +22,9 @@ var re = regexp.MustCompile("(" + quoteRegex + ")")
var dere = regexp.MustCompile("['\"](" + quoteRegex + ")[\"']")
var store *FileManager

func Store() *FileManager {
func Store(curlyBraces bool) *FileManager {
if store == nil {
store = &FileManager{mingoak.MkRoot()}
store = &FileManager{curlyBraces, mingoak.MkRoot()}
}
return store
}
Expand All @@ -46,8 +47,20 @@ func (ds *FileManager) ReadFile(key string) ([]byte, bool) {
return file, true
}

func (ds *FileManager) ReadFiles(keys []string) [][]byte {
result := [][]byte{}
for _, k := range keys {
file, _ := ds.ReadFile(k)
result = append(result, file)
}
return result
}

func (ds *FileManager) WriteFile(key string, file []byte) error {
file = dequoteCurlyBraces(file)
if ds.CurlyBraces {
file = dequoteCurlyBraces(file)
}

if re.MatchString(key) {
key = getKeyFromRegexp(key)
//if _, err := ds.root.ReadFile(key); err == nil {
Expand Down
21 changes: 20 additions & 1 deletion filemanager/filemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import (
var _ = Describe("Filemanager", func() {

var store *FileManager
var allowCurlyBraces bool

BeforeEach(func() {
store = Store()
allowCurlyBraces = true
})

JustBeforeEach(func() {
store = Store(allowCurlyBraces)
})

Context("Write/ReadFile", func() {
Expand Down Expand Up @@ -69,4 +74,18 @@ var _ = Describe("Filemanager", func() {
//Expect(err).ToNot(HaveOccurred())
//})
//})

Context("When double curly braces are not allowed", func() {
BeforeEach(func() {
allowCurlyBraces = false
})

It("doesn't unquote curly braces on write", func() {
err := store.WriteFile("{{keyE}}", []byte("{{content E}}"))
Expect(err).ToNot(HaveOccurred())
file, ok := store.ReadFile("{{keyE}}")
Expect(ok).To(Equal(true))
Expect(string(file)).To(ContainSubstring("{{content E}}"))
})
})
})
11 changes: 11 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "os"
type AviatorYaml struct {
Spruce []Spruce `yaml:"spruce"`
Fly Fly `yaml:"fly"`
Squash Squash `yaml:"squash"`
}

type Spruce struct {
Expand Down Expand Up @@ -77,6 +78,16 @@ type PathVal struct {
Value string `yaml:"value"`
}

type Squash struct {
Content []SquashContent `yaml:"content"`
To string `yaml:"to"`
}

type SquashContent struct {
Files []string `yaml:"files"`
Dir string `yaml:"dir"`
}

//go:generate counterfeiter . SpruceProcessor
type SpruceProcessor interface {
Process([]Spruce) error
Expand Down
Loading

0 comments on commit ed93f4e

Please sign in to comment.