mirror of
https://github.com/tests-always-included/mo.git
synced 2026-04-08 08:50:38 +02:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24e80aedaa | ||
|
|
4311f29879 | ||
|
|
d6794de1e2 | ||
|
|
b22baa9776 | ||
|
|
c28bffe708 | ||
|
|
b01fc43580 | ||
|
|
53e3208ba0 | ||
|
|
6dc284f05a | ||
|
|
a62541fc98 | ||
|
|
b31a97cfb1 | ||
|
|
71f85fa4f2 | ||
|
|
14003ba24a | ||
|
|
54195a6c6e | ||
|
|
81f9ec326d | ||
|
|
dae1c66f8f | ||
|
|
08576fca7b | ||
|
|
3aa5c462f8 | ||
|
|
a28ed0ccd5 | ||
|
|
c86fd9a89b | ||
|
|
03eb3925ac | ||
|
|
8e3e08a42b | ||
|
|
4c332c9188 | ||
|
|
67ba8bae2c | ||
|
|
1d2617dde1 |
13
.github/workflows/ci.yaml
vendored
Normal file
13
.github/workflows/ci.yaml
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
name: CI
|
||||
on: [push]
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v1
|
||||
- name: Run tests
|
||||
run: ./run-tests
|
||||
- name: Run against spec
|
||||
run: ./run-spec
|
||||
45
.github/workflows/release.yaml
vendored
Normal file
45
.github/workflows/release.yaml
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
name: docker push
|
||||
on: [push]
|
||||
jobs:
|
||||
push_to_registry:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- name: Docker meta
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
id: docker_meta
|
||||
uses: crazy-max/ghaction-docker-meta@v1
|
||||
with:
|
||||
images: ghcr.io/${{ github.repository }}
|
||||
tag-match: v(.*)
|
||||
- name: Set up QEMU
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: docker/setup-qemu-action@v1
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Cache Docker layers
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-buildx-
|
||||
- name: Login to GitHub Container Registry
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v2
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
with:
|
||||
builder: ${{ steps.buildx.outputs.name }}
|
||||
platforms: linux/amd64,linux/arm64
|
||||
tags: ${{ steps.docker_meta.outputs.tags }}
|
||||
cache-from: type=local,src=/tmp/.buildx-cache
|
||||
cache-to: type=local,dest=/tmp/.buildx-cache
|
||||
push: true
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,3 +5,5 @@ tests/*.diff
|
||||
spec/
|
||||
spec-runner/
|
||||
node_modules/
|
||||
package.json
|
||||
package-lock.json
|
||||
|
||||
2
API.md
2
API.md
@@ -36,7 +36,7 @@ Display a help message.
|
||||
|
||||
-s=FILE, --source=FILE
|
||||
|
||||
Source a file into the environment before processing template files.
|
||||
Source a file into the environment before processing template files. This can be used multiple times.
|
||||
|
||||
--
|
||||
|
||||
|
||||
7
Dockerfile
Normal file
7
Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM alpine
|
||||
|
||||
RUN apk add --no-cache bash
|
||||
ADD mo /usr/local/bin/mo
|
||||
RUN chmod +x /usr/local/bin/mo
|
||||
|
||||
ENTRYPOINT /usr/local/bin/mo
|
||||
81
README.md
81
README.md
@@ -41,7 +41,7 @@ There are a few ways you can install this tool. How you install it depends on h
|
||||
You can install this file in `/usr/local/bin/` or `/usr/bin/` by simply downloading it, changing the permissions, then moving it to the right location. Double check that your system's PATH includes the destination folder, otherwise users may have a hard time starting the command.
|
||||
|
||||
# Download
|
||||
curl -sSL https://git.io/get-mo -o mo
|
||||
curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo
|
||||
|
||||
# Make executable
|
||||
chmod +x mo
|
||||
@@ -58,7 +58,7 @@ You can install this file in `/usr/local/bin/` or `/usr/bin/` by simply download
|
||||
This is very similar to installing it globally but it does not require root privileges. It is very important that your PATH includes the destination folder otherwise it won't work. Some local folders that are typically used are `~/bin/` and `~/.local/bin/`.
|
||||
|
||||
# Download
|
||||
curl -sSL https://git.io/get-mo -o mo
|
||||
curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo
|
||||
|
||||
# Make executable
|
||||
chmod +x mo
|
||||
@@ -78,7 +78,7 @@ This is very similar to installing it globally but it does not require root priv
|
||||
Bash scripts can source `mo` to include the functionality in their own routines. This usage typically would have `mo` saved to a `lib/` folder in an application and your other scripts would use `. lib/mo` to bring it into your project.
|
||||
|
||||
# Download
|
||||
curl -sSL https://git.io/get-mo -o mo
|
||||
curl -sSL https://raw.githubusercontent.com/tests-always-included/mo/master/mo -o mo
|
||||
|
||||
# Move into your project folder
|
||||
mv mo ~/projects/amazing-things/lib/
|
||||
@@ -91,7 +91,7 @@ How to Use
|
||||
|
||||
If you only plan using strings and numbers, nothing could be simpler. In your shell script you can choose to export the variables. The below script is [`demo/using-strings`](demo/using-strings).
|
||||
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export TEST="This is a test"
|
||||
echo "Your message: {{TEST}}" | ../mo
|
||||
@@ -100,7 +100,7 @@ The result? "Your message: This is a test".
|
||||
|
||||
Using arrays adds a slight level of complexity. *You must source `mo`.* Look at [`demo/using-arrays`](demo/using-arrays).
|
||||
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export ARRAY=( one two "three three three" four five )
|
||||
. ../mo # This loads the "mo" function
|
||||
@@ -118,6 +118,77 @@ There are more scripts available in the [demos directory](demo/) that could help
|
||||
There are additional features that the program supports. Try using `mo --help` to see what is available.
|
||||
|
||||
|
||||
Enhancements
|
||||
-----------
|
||||
|
||||
In addition to many of the features built-in to Mustache, `mo` includes a number of unique features that make it a bit more powerful.
|
||||
|
||||
### Loop @key
|
||||
|
||||
`mo` implements Handlebar's `@key` references for outputting the key inside of a loop:
|
||||
|
||||
Env:
|
||||
```bash
|
||||
myarr=( foo bar )
|
||||
|
||||
# Bash v4+
|
||||
declare -A myassoc
|
||||
myassoc[hello]="mo"
|
||||
myassoc[world]="is great"
|
||||
```
|
||||
|
||||
Template:
|
||||
```handlebars
|
||||
{{#myarr}}
|
||||
- {{@key}} {{.}}
|
||||
{{/myarr}}
|
||||
|
||||
{{#myassoc}}
|
||||
* {{@key}} {{.}}
|
||||
{{/myassoc}}
|
||||
```
|
||||
|
||||
Output:
|
||||
```markdown
|
||||
- 0 foo
|
||||
- 1 bar
|
||||
|
||||
* hello mo
|
||||
* world is great
|
||||
```
|
||||
|
||||
|
||||
### Helpers / Function Arguments
|
||||
|
||||
Function Arguments are not a part of the official Mustache implementation, and are more often associated with Handlebar's Helper functionality.
|
||||
|
||||
`mo` allows for passing strings to functions.
|
||||
|
||||
```handlebars
|
||||
{{myfunc foo bar}}
|
||||
```
|
||||
|
||||
For security reasons, these arguments are not immediately available to function calls without a flag.
|
||||
|
||||
#### with `--allow-function-arguments`
|
||||
|
||||
```bash
|
||||
myfunc() {
|
||||
# Outputs "foo, bar"
|
||||
echo "$1, $2";
|
||||
}
|
||||
```
|
||||
|
||||
#### Using `$MO_FUNCTION_ARGS`
|
||||
|
||||
```bash
|
||||
myfunc() {
|
||||
# Outputs "foo, bar"
|
||||
echo "${MO_FUNCTION_ARGS[0]}, ${MO_FUNCTION_ARGS[1]}";
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Concessions
|
||||
-----------
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This embeds a template in the script without using strange `cat` syntax.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This sources a simple script with the env. variables needed for the template.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$0")"/..
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This sources a simple script with the env. variables needed for the template.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export ARRAY=( one two "three three three" four five )
|
||||
. ../mo
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This example does not source `mo` and is intentionally restricted to
|
||||
# variables that are not arrays.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This requires tomdoc.sh to be in your PATH.
|
||||
# https://github.com/tests-always-included/tomdoc.sh
|
||||
|
||||
19
mo
19
mo
@@ -25,6 +25,7 @@
|
||||
#/ This message.
|
||||
#/ -s=FILE, --source=FILE
|
||||
#/ Load FILE into the environment before processing templates.
|
||||
#/ Can be used multiple times.
|
||||
#
|
||||
# Mo is under a MIT style licence with an additional non-advertising clause.
|
||||
# See LICENSE.md for the full text.
|
||||
@@ -71,6 +72,7 @@
|
||||
# -s=FILE, --source=FILE
|
||||
#
|
||||
# Source a file into the environment before processing template files.
|
||||
# This can be used multiple times.
|
||||
#
|
||||
# --
|
||||
#
|
||||
@@ -300,7 +302,7 @@ moFindEndTag() {
|
||||
moFindString() {
|
||||
local pos string
|
||||
|
||||
string=${2%%$3*}
|
||||
string=${2%%"$3"*}
|
||||
[[ "$string" == "$2" ]] && pos=-1 || pos=${#string}
|
||||
local "$1" && moIndirect "$1" "$pos"
|
||||
}
|
||||
@@ -664,7 +666,7 @@ moLoop() {
|
||||
moParse() {
|
||||
# Keep naming variables mo* here to not overwrite needed variables
|
||||
# used in the string replacements
|
||||
local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag
|
||||
local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag moKey
|
||||
|
||||
moCurrent=$2
|
||||
moIsBeginning=$3
|
||||
@@ -780,6 +782,17 @@ moParse() {
|
||||
moShow "$moTag" "$moCurrent"
|
||||
;;
|
||||
|
||||
'@key')
|
||||
# Special vars
|
||||
moStandaloneDenied moContent "${moContent[@]}"
|
||||
# Current content (environment variable or function)
|
||||
if [[ "$moCurrent" == *.* ]]; then
|
||||
echo -n "${moCurrent#*.}"
|
||||
else
|
||||
echo -n "$moCurrent"
|
||||
fi
|
||||
;;
|
||||
|
||||
*)
|
||||
# Normal environment variable or function call
|
||||
moStandaloneDenied moContent "${moContent[@]}"
|
||||
@@ -1095,7 +1108,7 @@ moUsage() {
|
||||
|
||||
# Save the original command's path for usage later
|
||||
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
|
||||
MO_VERSION="2.2.0"
|
||||
MO_VERSION="2.4.0"
|
||||
|
||||
# If sourced, load all functions.
|
||||
# If executed, perform the actions as expected.
|
||||
|
||||
10
package.json
10
package.json
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"async": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rm -rf package-lock.json node_modules/ spec/",
|
||||
"install-tests": "npm install; git clone https://github.com/mustache/spec.git spec",
|
||||
"test": "node run-spec.js spec/specs/*.json"
|
||||
}
|
||||
}
|
||||
20
run-spec
Executable file
20
run-spec
Executable file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Create a package.json so the dependency package is installed in the local
|
||||
# directory
|
||||
echo '{"private":true, "dependencies":{"async": "*"}}' > package.json
|
||||
npm install
|
||||
|
||||
# Install or update the specs
|
||||
if [[ ! -d spec ]]; then
|
||||
git clone https://github.com/mustache/spec.git spec
|
||||
else
|
||||
(
|
||||
cd spec;
|
||||
git pull
|
||||
)
|
||||
fi
|
||||
|
||||
# Actually run the specs
|
||||
node run-spec.js spec/specs/*.json
|
||||
|
||||
@@ -64,7 +64,7 @@ function runTest(test, done) {
|
||||
var output, partials, script;
|
||||
|
||||
script = [
|
||||
'#!/bin/bash'
|
||||
'#!/usr/bin/env bash'
|
||||
];
|
||||
partials = test.partials || {};
|
||||
|
||||
|
||||
@@ -40,4 +40,6 @@ done
|
||||
echo ""
|
||||
echo "Pass: $PASS"
|
||||
echo "Fail: $FAIL"
|
||||
[[ $FAIL -gt 0 ]] && exit 1
|
||||
if [[ $FAIL -gt 0 ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<b>resque</b>
|
||||
<b>hub</b>
|
||||
<b>rip</b>
|
||||
<b>0 - resque</b>
|
||||
<b>1 - hub</b>
|
||||
<b>2 - rip</b>
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{{#repo}}
|
||||
<b>{{.}}</b>
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
||||
|
||||
4
tests/assoc-array.env
Normal file
4
tests/assoc-array.env
Normal file
@@ -0,0 +1,4 @@
|
||||
declare -A repo
|
||||
repo[resque]="Resque"
|
||||
repo[hub]="Hub"
|
||||
repo[rip]="Rip"
|
||||
3
tests/assoc-array.expected
Normal file
3
tests/assoc-array.expected
Normal file
@@ -0,0 +1,3 @@
|
||||
<b>hub - Hub</b>
|
||||
<b>rip - Rip</b>
|
||||
<b>resque - Resque</b>
|
||||
3
tests/assoc-array.template
Normal file
3
tests/assoc-array.template
Normal file
@@ -0,0 +1,3 @@
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
||||
@@ -14,14 +14,15 @@ Simple usage:
|
||||
Options:
|
||||
|
||||
-u, --fail-not-set
|
||||
- Fail upon expansion of an unset variable.
|
||||
Fail upon expansion of an unset variable.
|
||||
-x, --fail-on-function
|
||||
- Fail when a function returns a non-zero status code.
|
||||
Fail when a function returns a non-zero status code.
|
||||
-e, --false
|
||||
- Treat the string "false" as empty for conditionals.
|
||||
Treat the string "false" as empty for conditionals.
|
||||
-h, --help
|
||||
- This message.
|
||||
This message.
|
||||
-s=FILE, --source=FILE
|
||||
- Load FILE into the environment before processing templates.
|
||||
Load FILE into the environment before processing templates.
|
||||
Can be used multiple times.
|
||||
|
||||
MO_VERSION=2.2.0
|
||||
|
||||
2
tests/source-multiple-1.vars
Normal file
2
tests/source-multiple-1.vars
Normal file
@@ -0,0 +1,2 @@
|
||||
export A=from1
|
||||
export B=from1
|
||||
2
tests/source-multiple-2.vars
Normal file
2
tests/source-multiple-2.vars
Normal file
@@ -0,0 +1,2 @@
|
||||
export B=from2
|
||||
export C=from2
|
||||
3
tests/source-multiple.expected
Normal file
3
tests/source-multiple.expected
Normal file
@@ -0,0 +1,3 @@
|
||||
A: from1
|
||||
B: from2
|
||||
C: from2
|
||||
8
tests/source-multiple.sh
Executable file
8
tests/source-multiple.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "${0%/*}" || exit 1
|
||||
cat <<EOF | ../mo --source=source-multiple-1.vars --source=source-multiple-2.vars
|
||||
A: {{A}}
|
||||
B: {{B}}
|
||||
C: {{C}}
|
||||
EOF
|
||||
Reference in New Issue
Block a user