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

Commit

Permalink
Make it easier to use from Go code.
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-stitt committed Dec 1, 2017
1 parent 95edd92 commit 8321aa0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
40 changes: 35 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ $ go get -u github.com/openpixel/rise

## Usage

### CLI

You can see the usage documation for the CLI by running `rise --help`.

```
Expand All @@ -35,15 +37,15 @@ Flags:
--varFile stringSlice The files that contains the variables to be interpolated
```

### Input (required)
#### Input (required)

The input should be a string that references a file to run the interpolation against.

### Output (optional)
#### Output (optional)

The output is the location that the interpolated content should be written to. If not set, it will print to stdout.

### Variable Files (optional)
#### Variable Files (optional)

The variable files should be in hcl compatible formats. See https://github.com/hashicorp/hcl for reference. Rise loads the files in the order they are supplied, so the latest reference of a variable will always be used. For example, if we had two files that looked like this

Expand All @@ -69,14 +71,42 @@ $ rise ... --varFile vars.hcl --varFile vars2.hcl

The value of `i` would be `10`.

## Basic Example
#### Basic Example

Look in the [examples](https://github.com/OpenPixel/rise/tree/master/examples) directory for an example, including inheritance:

```
$ rise -i ./examples/input.json -o ./examples/output.json --varFile ./examples/vars.hcl --varFile ./examples/vars2.hcl
```

### API

rise can also be used within Go code.

```go
import "github.com/openpixel/rise/template"

vars := map[string]ast.Variable{}

tmpl, err := template.NewTemplate(vars)
// handle error

input := `${lower("FOO")}`
result, err := tmpl.Render(input)
// handle error

fmt.Printf("Value: %s", result.Value.(string)) // Value: foo
```

## Interpolation Methods

- lower
- Convert the provided argument to lowercase
- upper
- Convert the provided argument to uppercase
- env
- Find the provided environment variable

## Coming Soon

- More interpolation methods
Expand All @@ -90,4 +120,4 @@ $ rise -i ./examples/input.json -o ./examples/output.json --varFile ./examples/v

- [hashicorp/hil](https://github.com/hashicorp/hil) - Used to perform interpolation
- [hashicorp/hcl](https://github.com/hashicorp/hcl) - Used as a configuration syntax for variables
- [hashicorp/terraform](https://github.com/hashicorp/terraform) - Inspiration for the tool. A number of the interpolation functions have been extracted directly from terraform.
- [hashicorp/terraform](https://github.com/hashicorp/terraform) - Inspiration for the tool. A number of the interpolation functions have been extracted directly from terraform.
8 changes: 7 additions & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/openpixel/rise/template"
"github.com/openpixel/rise/variables"
)

// Run will run
Expand All @@ -15,7 +16,12 @@ func Run(inputFile, outputFile *string, varFiles *[]string) error {
return err
}

t, err := template.NewTemplate(varFiles)
vars, err := variables.LoadVariableFiles(*varFiles)
if err != nil {
return err
}

t, err := template.NewTemplate(vars)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/openpixel/rise/interpolation"
"github.com/openpixel/rise/variables"
)

// Template is a container for holding onto the ast Variables
Expand All @@ -13,10 +12,9 @@ type Template struct {
}

// NewTemplate will prepare a template object for use
func NewTemplate(varFiles *[]string) (*Template, error) {
vars, err := variables.LoadVariableFiles(*varFiles)
if err != nil {
return nil, err
func NewTemplate(vars map[string]ast.Variable) (*Template, error) {
if vars == nil {
vars = make(map[string]ast.Variable)
}
return &Template{
vars: vars,
Expand Down
52 changes: 52 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package template

import (
"testing"

"github.com/hashicorp/hil/ast"
)

func TestTemplate_Render(t *testing.T) {
t.Run("Test render with variable passed", func(t *testing.T) {
vars := map[string]ast.Variable{
"foo": ast.Variable{
Type: ast.TypeMap,
Value: map[string]ast.Variable{
"bar": ast.Variable{
Type: ast.TypeString,
Value: "Bar",
},
},
},
}
tmpl, err := NewTemplate(vars)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}

result, err := tmpl.Render(`${has(foo, "bar")}`)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}

if result.Value.(string) != "true" {
t.Fatalf("Unexpected result: Expected %s, got %s", "true", result.Value.(string))
}
})

t.Run("Test render with nil variables", func(t *testing.T) {
tmpl, err := NewTemplate(nil)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}

result, err := tmpl.Render(`${lower("FOO")}`)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}

if result.Value.(string) != "foo" {
t.Fatalf("Unexpected result: Expected %s, got %s", "foo", result.Value.(string))
}
})
}

0 comments on commit 8321aa0

Please # to comment.