• Stars
    star
    154
  • Rank 233,615 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate file using template from environment, arguments, json/yaml/toml config files

Build Status License

frep

Generate file using template from environment, arguments, json/yaml/toml config files.

NAME:
   frep - Generate file using template

USAGE:
   frep [options] input-file[:output-file] ...

VERSION:
   1.3.x

AUTHORS:
   Guoqiang Chen <[email protected]>

OPTIONS:
   -e, --env name=value    set variable name=value, can be passed multiple times
       --json jsonstring   load variables from json object string
       --load file         load variables from json/yaml/toml file
       --no-sys-env        exclude system environments, default false
       --overwrite         overwrite if destination file exists
       --dryrun            just output result to console instead of file
       --strict            exit on any error during template processing
       --delims value      template tag delimiters (default: {{:}})
       --missing           handling of missing vars, one of: default/invalid, zero, error (default: default)
       --help              print this usage
       --version           print version information

EXAMPLES:
   frep nginx.conf.in -e webroot=/usr/share/nginx/html -e port=8080
   frep nginx.conf.in:/etc/nginx.conf -e webroot=/usr/share/nginx/html -e port=8080
   frep nginx.conf.in --json '{"webroot": "/usr/share/nginx/html", "port": 8080}'
   frep nginx.conf.in --load config.json --overwrite
   echo "{{ .Env.PATH }}"  | frep -

Downloads

v1.3.13 Release: https://github.com/subchen/frep/releases/tag/v1.3.13

  • Linux

    curl -fSL https://github.com/subchen/frep/releases/download/v1.3.13/frep-1.3.13-linux-amd64 -o /usr/local/bin/frep
    chmod +x /usr/local/bin/frep
    
    # centos / redhat
    yum install https://github.com/subchen/frep/releases/download/v1.3.13/frep-1.3.13-204.x86_64.rpm
    
    # ubuntu
    curl -fSL https://github.com/subchen/frep/releases/download/v1.3.13/frep_1.3.13-204_amd64.deb -o frep_1.3.13-204_amd64.deb
    dpkg -i frep_1.3.13-204_amd64.deb
    
  • macOS

    brew install subchen/tap/frep
    
  • Windows

    wget https://github.com/subchen/frep/releases/download/v1.3.13/frep-1.3.13-windows-amd64.exe
    

Docker

You can run frep using docker container

docker run -it --rm subchen/frep --help

Examples

Load template variables

  • Load from environment

    export webroot=/usr/share/nginx/html
    export port=8080
    frep nginx.conf.in
    
  • Load from arguments

    frep nginx.conf.in -e webroot=/usr/share/nginx/html -e port=8080
    
  • Load from JSON String

    frep nginx.conf.in --json '{"webroot": "/usr/share/nginx/html", "port": 8080}'
    
  • Load from JSON file

    cat > config.json << EOF
    {
      "webroot": "/usr/share/nginx/html",
      "port": 8080,
      "servers": [
        "127.0.0.1:8081",
        "127.0.0.1:8082"
      ]
    }
    EOF
    
    frep nginx.conf.in --load config.json
    
  • Load from YAML file

    cat > config.yaml << EOF
    webroot: /usr/share/nginx/html
    port: 8080
    servers:
      - 127.0.0.1:8081
      - 127.0.0.1:8082
    EOF
    
    frep nginx.conf.in --load config.yaml
    
  • Load from TOML file

    cat > config.toml << EOF
    webroot = /usr/share/nginx/html
    port = 8080
    servers = [
       "127.0.0.1:8081",
       "127.0.0.1:8082"
    ]
    EOF
    
    frep nginx.conf.in --load config.toml
    

Input/Output

  • Input from file

    // input file: nginx.conf
    frep nginx.conf.in
    
  • Input from console(stdin)

    // input from stdin pipe
    echo "{{ .Env.PATH }}" | frep -
    
  • Output to default file (Removed last file ext)

    // output file: nginx.conf
    frep nginx.conf.in --overwrite
    
  • Output to the specified file

    // output file: /etc/nginx.conf
    frep nginx.conf.in:/etc/nginx.conf --overwrite -e port=8080
    
  • Output to console(stdout)

    frep nginx.conf.in --dryrun
    frep nginx.conf.in:-
    
  • Output multiple files

    frep nginx.conf.in redis.conf.in ...
    

Template

Templates use Golang text/template.

You can access environment variables within a template

Env.PATH = {{ .Env.PATH }}

If your template file uses {{ and }} as part of it's syntax, you can change the template escape characters using the --delims.

frep --delims "<%:%>" ...

There are some built-in functions as well: Masterminds/sprig v2.22.0

More funcs added:

  • toJson
  • toYaml
  • toToml
  • toBool
  • fileSize
  • fileLastModified
  • fileGetBytes
  • fileGetString
  • fileExists
  • include
  • countRune
  • pipeline compatible regex functions from sprig
    • reReplaceAll
    • reReplaceAllLiteral
    • reSplit
  • awsSecret
  • awsParameterStore

Sample of nginx.conf.in

server {
    listen {{ .port }} default_server;

    root {{ .webroot | default "/usr/share/nginx/html" }};
    index index.html index.htm;

    location /api {
        {{ include "shared/log.nginx" | indent 8 | trim }}
        proxy_pass http://backend;
    }
}

upstream backend {
    ip_hash;
{{- range .servers }}
    server {{.}};
{{- end }}
}

Sample using secrets, first of all take into account that in order to use the secret functionality you need to have a proper AWS configuration in place and permissions enough to read secrets from AWS Secrets Manager. More details of how to configure AWSCLI can be found at https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html

Once you have all the requirements just create a template like this one:

# application.conf
mysql_host: {{ .mysql_host }}
mysql_user: {{ .mysql_user }}
mysql_pass: {{ awsSecret "application/mysql/password" }}

In above example mysql_host and mysql_user will be filled as usual by using frep config file or environment variables but mysql_pass will be fetch straight from AWS Secrets Manager by looking at secret name application/mysql/password

If you have multiple items in a single secret you can retrieve an specific key by specifying the key you want in template, for example:

# application.conf
mysql_host: {{ .mysql_host }}
mysql_user: {{ .mysql_user }}
mysql_pass: {{ awsSecret "application/mysql/password" }}

external_api_client: {{ awsSecret "application/external_api" "client_id" }}
external_api_secret: {{ awsSecret "application/external_api" "secret_key" }}

Sample using AWS Parameter Store, first of all take into account that in order to use the ssm functionality you need to have a proper AWS configuration in place and permissions enough to read parameters from AWS Parameter Store. More details of how to configure AWSCLI can be found at https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html

Once you have all the requirements just create a template like this one:

# application.conf
mysql_host: {{ .mysql_host }}
mysql_user: {{ .mysql_user }}
mysql_pass: {{ awsSecret "application/mysql/password" }}
mysql_dns: {{ awsParameterStore "application/mysql/dns" }}

In above example mysql_dns will be filled as usual by using frep config file or environment variables but mysql_pass will be fetch straight from AWS Parameter Store by looking at application/mysql/dns

SSM Limitation: You can get parameter from ParameterStore just in textplain.

More Repositories

1

jetbrick-template-2x

Template Engine for Java
Java
371
star
2

javadoc.chm

This software generates an Microsoft HTML Help(CHM) from generic javadoc style API documents
Java
143
star
3

angular-async-loader

Load modules and components asynchronously for angular 1.x application.
JavaScript
136
star
4

jetbrick-template-1x

Java template engine, quickly and easily.
Java
68
star
5

go-xmldom

XML DOM processing for Golang, supports xpath query
Go
44
star
6

go-trylock

TryLock support on read-write lock for Golang
Go
33
star
7

jetbrick-webmvc

jetbrick web mvc framework
Java
25
star
8

centos-7-kickstart

build a centos 7 iso/ovf/ova with kickstart
Shell
21
star
9

jetbrick-commons

jetbrick utility classes
Java
21
star
10

go-log

Simple and configurable Logging in Go, with level, formatters and writers
Go
14
star
11

go-cli

A full-featured and easy to use command-line package
Go
13
star
12

jetbrick-template-2x-samples

Samples for jetbrick-template-2x
Java
11
star
13

jetbrick-template-1x-samples

The samples for jetbrick-template integrated with webmvc
Java
9
star
14

jetbrick-ioc

IoC framwork for jetbrick
Java
7
star
15

jetbrick-webmvc-samples

Samples for jetbrick-webmvc
Java
6
star
16

go-stack

Common utility functions for Golang
Go
6
star
17

go-tableify

Pretty console printing of tabular data
Go
5
star
18

go-curl

A Go HTTP client library for creating and sending API requests
Go
5
star
19

jetbrick-orm

Object-relational mapping framework
Java
4
star
20

jetbrick-all-1x

A full-stack framework written with java
Java
4
star
21

gls

A goroutine-local storage for golang
Go
3
star
22

subchen.github.io

WebSite for subchen.github.com
HTML
3
star
23

ovfenv-installer

Configure networking from vSphere ovfEnv properties
Go
3
star
24

homebrew-tap

Tools of macOS Homebrew Packages
Ruby
2
star
25

snack

Generic functional library for javascript/node.js
JavaScript
2
star
26

snack-string

Generic string functional library for javascript/node.js
JavaScript
2
star
27

dev-box

A development box deployer, Support macOS, CentOS and Ubuntu.
Shell
2
star
28

shaft

Java simple database operation framework
Java
2
star
29

snack-cli

command-line interfaces for node.js
JavaScript
2
star
30

bintray-cli

Command line for api.bintray.com
1
star
31

jetbrick-template-shiro

Shiro tag extension for jetbrick-template
Java
1
star
32

jetbrick-website-app

website for http://subchen.github.io/
Java
1
star
33

jetbrick-extension

jetbrick extension modules
Java
1
star
34

jetbrick-jdbclog

jdbc logger for jetbrick
Java
1
star
35

jetbrick-ioc-spring

Spring intergrate for Jetbrick IoC
Java
1
star