Skip to content
/ hogan Public

Customized handlebars wrapper, with config inheritance

License

Notifications You must be signed in to change notification settings

cvent/hogan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

57790c2 · Mar 3, 2023
Dec 29, 2021
Mar 3, 2023
Nov 17, 2022
Jan 25, 2023
Feb 22, 2019
Aug 28, 2019
Nov 17, 2022
Mar 18, 2020
Jan 25, 2023
Jan 25, 2023
May 17, 2022
Apr 19, 2018
May 24, 2019
Apr 20, 2018

Repository files navigation

Build Status Build status

hogan

Purpose

The purpose of this project is to generate config overrides so that we can keep a template up to date, and populate values on the fly with ease at build time.

Installation

Grab a binary for your OS from the latest release, and put it somewhere in your PATH.

MacOS

brew tap cvent/tap
brew install hogan

Linux

curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git cvent/hogan --target x86_64-unknown-linux-gnu --to /usr/local/bin

Tests

You can run the tests via cargo test. The tests should always pass and all new behavior should be tested.

Usage

Once you have installed hogan, you can execute it as hogan. Some of the arguments are described below:

  • environments-filter: Regex specifying which environment(s) to update.
  • templates: The directory to use for searching for template files (recursively).
  • configs: The directory where hogan-formatted config files can be found (These are config.ENVIRONMENT.json files)

Example

    hogan transform --environments-filter ENVIRONMENT --templates . --configs ./Configs

You can also create an alias of the above command and add to bash_profile

    hg() { hogan transform --configs ~/cventcode/hogan-configs/configs/ --templates . --templates-filter "$1" --environments-filter "$2"; }

and then use the alisa to generate config like

    hg tempate.yaml prod

this will generate the config for prod environment using template.yaml as template file.

Custom handlers in config files

The following custom handlers exist

comma-list

Allows an array of objects to be turned into a comma separated list by passing in an array:

// Given a config of:
let config = json!({
  "Memcache": {
    "Servers": [
      {
        "Endpoint": "192.168.1.100",
        "Port": "1122"
      },
      {
        "Endpoint": "192.168.1.101",
        "Port": "1122"
      },
      {
        "Endpoint": "192.168.1.102",
        "Port": "1122"
      }
    ]
  }
});

// and a template of:
let template = "{{#comma-list Memcache.Servers}}{{Endpoint}}:{{Port}}{{/comma-list}}";

// The helper will transform it into:
let transformed = "192.168.1.100:1122,192.168.1.101:1122,192.168.1.102:1122";

equal, eq

Like if, but compares the two arguments provided for equality:

// Given a config of:
let config = json!({
  "Region": {
    "Key": "TEST"
  }
});

// and a template of:
let template = r#"{{#equal Region.Key "TEST"}}True{{else}}False{{/equal}}"#;

// The helper will transform it into:
let transformed = "True";

or

Logical OR two parameters:

// Given a config of:
let config = json!({
  "Region": {
    "Key": "TEST"
  }
});

// and a template of:
let template = r#"{{#or (eq Region.Key "TEST") (eq Region.Key "TEST2")}}True{{else}}False{{/or}}"#;

// The helper will transform it into:
let transformed = "True";

yaml-string

Escapes a string for valid injection into a Yaml file:

// Given a config of:
let config = json!({
  "app": {
    "path": "C:\\Program Files\\My App"
  }
});

// and a template of:
let template = r#"windows:
  path: "{{yaml-string app.path}}""#;

// The helper will transform it into:
let transformed = r#"windows:
  path: "C:\\Program Files\\My App""#;

url-rm-slash

Removes the trailing slash on an endpoint:

// Given a config of:
let config = json!({
  "SlashService": {
    "endpoint": "https://slash.com/"
  }
});

// and a template of:
let template = "{{url-rm-slash SlashService.endpoint}}";

// The helper will transform it into:
let transformed = "https://slash.com";

url-add-slash

Adds the trailing slashes on an endpoint:

// Given a config of:
let config = json!({
  "NonSlashService": {
    "endpoint": "https://nonslash.com"
  }
});

// and a template of:
let template = "{{url-add-slash NonSlashService.endpoint}}";

// The helper will transform it into:
let transformed = "https://nonslash.com/";

url-rm-path

Removes the last slash plus content to the end of the string:

// Given a config of:
let config = json!({
  "PathService": {
    "endpoint": "https://path.com/path/remove-this"
  }
});

// and a template of:
let template = "{{url-rm-path PathService.endpoint}}";

// The helper will transform it into:
let transformed = "https://path.com/path";

Helpful Information