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

Commit

Permalink
Restructure (#10)
Browse files Browse the repository at this point in the history
* Restructure packages to use internal folder

* Fix missing template panic

* Update travis settings
  • Loading branch information
cam-stitt authored Sep 6, 2018
1 parent 430b06e commit 6fda7e9
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ dist: trusty
go:
- 1.9.x
- 1.10.x
- 1.11.x
os:
- linux
env:
- DEP_VERSION="0.4.1"
- DEP_VERSION="0.5.0"
go_import_path: github.com/openpixel/rise
before_install:
# Download the binary to bin folder in $GOPATH
Expand Down
4 changes: 2 additions & 2 deletions cmd/rise.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"

"github.com/openpixel/rise/runner"
"github.com/spf13/cobra"
)

Expand All @@ -18,6 +17,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&inputs, "input", "i", "", "The file to perform interpolation on")
RootCmd.PersistentFlags().StringVarP(&outputs, "output", "o", "", "The file to output")
RootCmd.PersistentFlags().StringSliceVarP(&configFiles, "config", "c", []string{}, "The files that define the configuration to use for interpolation")

RootCmd.AddCommand(versionCmd)
}

Expand All @@ -30,7 +30,7 @@ var RootCmd = &cobra.Command{
if inputs == "" {
log.Fatal("Must have an input")
}
err := runner.Run(&inputs, &outputs, &configFiles)
err := Run(inputs, outputs, configFiles)
if err != nil {
log.Fatal(err)
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"io"
"io/ioutil"
"os"

"github.com/openpixel/rise/internal/config"
"github.com/openpixel/rise/internal/template"
)

// Run accepts an input, output and config files and performs interpolation.
// If the output is empty, it writes to stdout
func Run(inputFile, outputFile string, configFiles []string) error {
contents, err := ioutil.ReadFile(inputFile)
if err != nil {
return err
}

configResult, err := config.LoadConfigFiles(configFiles)
if err != nil {
return err
}

t, err := template.NewTemplate(configResult)
if err != nil {
return err
}

result, err := t.Render(string(contents))
if err != nil {
return err
}

if outputFile != "" {
err = ioutil.WriteFile(outputFile, []byte(result.Value.(string)), 0644)
if err != nil {
return err
}
} else {
_, err = io.WriteString(os.Stdout, result.Value.(string))
if err != nil {
return err
}
}

return nil
}
6 changes: 3 additions & 3 deletions examples/input.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
${i}:
${var.i}:
- ${join(", ", list("Foo", "Bar"))}
- ${i}
- ${var.i}
- ${upper("foobar")}
- ${length(h)}
- ${length(var.h)}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions template/template.go → internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/openpixel/rise/config"
"github.com/openpixel/rise/interpolation"
"github.com/openpixel/rise/internal/config"
"github.com/openpixel/rise/internal/interpolation"
)

// Template is a container for holding onto the ast Variables
Expand Down Expand Up @@ -92,8 +92,6 @@ func (vf visitorFn) fn(n ast.Node) ast.Node {
if va, ok := vn.Key.(*ast.VariableAccess); ok {
vn.Key = vf.processVariable(va)
}
default:
return n
}
return n
}
Expand All @@ -107,15 +105,20 @@ func (vf visitorFn) processVariable(va *ast.VariableAccess) ast.Node {
vf.resultErr = err
return va
}
resultN = vf.fn(resultN)
return resultN
if resultN != va {
resultN = vf.fn(resultN)
return resultN
}
}

return va
}

func (vf visitorFn) processTemplateNode(original ast.Node, name string) (replacement ast.Node, err error) {
template := vf.templates[name]
template, ok := vf.templates[name]
if !ok {
return original, nil
}

switch template.Type {
case ast.TypeString:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/hashicorp/hil/ast"
"github.com/openpixel/rise/config"
"github.com/openpixel/rise/internal/config"
)

func TestTemplate_Render(t *testing.T) {
Expand Down
47 changes: 0 additions & 47 deletions runner/runner.go

This file was deleted.

0 comments on commit 6fda7e9

Please # to comment.