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

Commit

Permalink
Git cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-stitt committed Oct 27, 2017
0 parents commit 4540b57
Show file tree
Hide file tree
Showing 27 changed files with 1,184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor
main
dist
examples/output.*
8 changes: 8 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
builds:
- binary: rise
goos:
- windows
- darwin
- linux
goarch:
- amd64
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: go
dist: trusty
go:
- 1.8.4
- 1.9.1
os:
- linux
go_import_path: github.com/openpixel/rise
script:
- go test -race -v ./...
51 changes: 51 additions & 0 deletions Gopkg.lock

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

22 changes: 22 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"

21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Cameron Stitt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[![Build Status](https://travis-ci.org/OpenPixel/rise.svg?branch=master)](https://travis-ci.org/OpenPixel/rise)
[![Go Report Card](https://goreportcard.com/badge/github.com/openpixel/rise)](https://goreportcard.com/report/github.com/openpixel/rise)

# rise

Powerful text interpolation.

## Installation

### Binaries

You can find binaries for the latest release on the [releases](https://github.com/OpenPixel/rise/releases) page.

### Go toolchain

```
$ go get -u github.com/openpixel/rise
```

## Usage

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

```
$ rise --help
A powerful text interpolation tool.
Usage:
rise [flags]
Flags:
-h, --help help for rise
-i, --input string The file to perform interpolation on
-o, --output string The file to output
--varFile stringSlice The files that contains the variables to be interpolated
```

### Input (required)

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

### 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)

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

vars.hcl
```
variable "i" {
value = 6
}
```

vars2.hcl
```
variable "i" {
value = 10
}
```

And ran the following command

```
$ rise ... --varFile vars.hcl --varFile vars2.hcl
```

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

## 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
```

## 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
- join
- split
- replace
- and many more!
- Deeper documentation with examples for interpolation methods
- More configuration CLI arguments
- Support for directories as inputs/outputs
- Support for globs (eg: /tmp/*.json)
- Support for var overrides at cli level (eg: --var "foo=bar")
34 changes: 34 additions & 0 deletions cmd/rise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"log"

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

var inputs string
var outputs string
var varFiles []string

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().StringSliceVar(&varFiles, "varFile", []string{}, "The files that contains the variables to be interpolated")
}

// RootCmd is the root command for the entire cli
var RootCmd = &cobra.Command{
Use: "rise",
Short: "Rise is a powerful text interpolation tool.",
Long: `A powerful text interpolation tool.`,
Run: func(cmd *cobra.Command, args []string) {
if inputs == "" {
log.Fatal("Must have an input")
}
err := runner.Run(&inputs, &outputs, &varFiles)
if err != nil {
log.Fatal(err)
}
},
}
5 changes: 5 additions & 0 deletions examples/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"i": ${i},
"j": "${join(", ", h)}",
"other": "${upper("foobar")}"
}
3 changes: 3 additions & 0 deletions examples/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
${i}:
- ${join(", ", h)}
7 changes: 7 additions & 0 deletions examples/vars.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "i" {
value = 6
}

variable "j" {
value = 100
}
13 changes: 13 additions & 0 deletions examples/vars2.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "j" {
value = "${env("J_VAL")}"
}

variable "k" {
value = {
t = "z"
}
}

variable "h" {
value = ["Foo", "Bar"]
}
35 changes: 35 additions & 0 deletions interpolation/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package interpolation

import (
"errors"
"os"

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

// CoreFunctions are the custom funtions for interpolation
var CoreFunctions = map[string]ast.Function{
"lower": interpolationFuncLower(),
"upper": interpolationFuncUpper(),
"env": interpolationFuncEnv(),
"join": interpolationFuncJoin(),
"has": interpolationFuncHas(),
"map": interpolationFuncMap(),
"list": interpolationFuncList(),
}

// interpolationFuncEnv will extract a variable out of the env
func interpolationFuncEnv() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Variadic: false,
Callback: func(inputs []interface{}) (interface{}, error) {
varName := inputs[0].(string)
if varName == "" {
return "", errors.New("Must provide a variable name")
}
return os.Getenv(varName), nil
},
}
}
Loading

0 comments on commit 4540b57

Please sign in to comment.