like

command module
v0.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2024 License: MIT Imports: 8 Imported by: 0

README

# Like | Template Engine

Quality Gate Status Code Smells Coverage Reliability Rating Maintainability Rating

Build Go Reference Go Report Card

Motivation / Get started

During setup a k8s cluster I start thinking about a language that can make easier to handle tons of mostly identical parts of text and can be/have abilities to be template/meta language. Language as simple as powerful and without extra expressions, so here we are...

Looking ahead there is small example of usage Like with Terraform

Hello World

Following example writes 'Hello World' to console

` Hello World

` is output operator with new line ending, Hello World string expression that should be output

Data types

The language supports the following data types:

  • String expression

Any expression that does not contain control symbols

  • Quoted strings

The language supports single and double quoted strings, all the difference that inside single quoted string you shoud escape single qoute and double quote for double quoted strings

  • Objects

Objects(or key value storage) can be defined following way:

a = { my_prop:a other_prop: 'Hello' }
  • Arrays

Arrays can be defined following way:

a = [1 a 'asd' {a: 'object_property'} ]
@ $a `$_v

String interpolation

Inside strings it is possible to use references so

a='Hello World'
` He said $a
` 'He said $a'
` "He said $a"

Will output: He said Hello World 3 times

Control Symbols

Any control symbol can be escaped using \

Output operators

  • ~ - outputs expression without new-line ending
  • ` - outputs expression with \n new-line ending

References and variables

Assigments

a = Hello World
a = 'Hello World'
a = "Hello world"
# a = 'Hello World'

a = [Hello world]
# a is array with two elements

a = {Hello: world}
# a is object with property Hello

The variable a can be referenced by $ operator so

` $a

will output value of a

Operators and code blocks

For loops and conditions every operator can be introduced as code-block e.g.

a = [Hello world]
@ $a ` $_v
# same as
@ $a {
    ` $_v
}

Loops

@ the loop operator has following syntax

@ <reference-to-array-or-object> <operator>

inside a loop there are two predefined variables are assigned for every iteration: ` $_k(key) and $_v(value)

a = [Hello world]
@ $a ` "$_k: $_v"

will output

0: Hello
1: world

Condition operator

Common form of conditional opertors looks following

? <reference> <true-operator> % <else-operator>

also there is a ternary form for conditions

<reference> ? <true-operator> % <else-operator>
a = Hello
? $a ` World

# will output World

Condition operator consider empty(empty string, empty array, or empty object) as false and any value as true

Lambdas

Functions or lambdas can be defined and invoked following way:

# without arguments
a = () ` Hello World
$a()

# with agruments
a = (p1 p2 p3) {
    ` $p1 ? $p2 % $p3
}

$a([] TRUE FALSE)
# will output FALSE

Lambda returns a value of last evaluated expression

Templates

Templates can be considered as lambdas but with specific format e.g. temlate consists only with string template

`` template_name(p1 p2)
This template text, where
p1 = $p1 and p2 = $p2
``

` $template_name(a b)

will output

This template text, where
p1 = a and p2 = b

Builtin functions

  • cwd() - return current working directory
  • file(fileName) - returns contents of the file
  • joinPath(path1 path2 ... pathN) - accepts multiple arguments which will be joined into single path
  • resolvePath(path) - acts same as include and returns full path
  • len(expression) - returns length of an expression
  • error(expression) - raises error and stop further execution
  • eval(string) - accept string and executes it as Like program
  • exec(cmd arg1 arg2 ... argN) - same as & executes command
  • split(string sep) or $s | split(sep) - splits string and returns a list
  • debug(string) - outputs given expression in debug notation, mostly for debugging operator precedence

Closures

In Like it is possible to create functions on the fly using closures, for example:

a = (b) {
    () {
        ` it was $b
    }
}

# create new lambda with closure to $b
b=$a(Hello)

# executes new lambda
$b()

will output

it was Hello

Parse operator

:< - parse operator it can be useful when you need JSON or other supported by the operator values

for example

# we have JSON file with following content
# a.json
# {
#   "environment" : "dev"
# }
#
# reading file
(& cat "file.json") | $f
a = :< json $f
~ $a.environment

# outputs: dev

Format opertor

Format operator turns objects into specified format

a = { env: dev }
~ :> json $a

# outputs
env: dev

Supported parsers formatters

  • json
  • yaml
  • env

Arguments

Arguments passed via command line are accessible via predefined variable $args

# like file.like one two three
@ $_args ` $_v

Environmnet variables

Environmnet variables are accessible via predefined variable _env

` $_env[TEMP]

also Like try to get file with name .env from current directory and add/replace environment variables from it

Directives

Include

#include './some.like'

Includes and evaluates the specified file, prefix ./ means that path spcified from the location of the file(not current directory)

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL