Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
arbitar committed Dec 13, 2022
0 parents commit 880123c
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
uhandles
test/
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# tiny go builder
#

FROM ubuntu:20.04

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
golang-go build-essential ca-certificates

WORKDIR /host

CMD go get && make build
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all:
docker build -t uhandles .
docker run --rm -v "${PWD}":/host uhandles
docker rmi uhandles

build:
go build -o uhandles -tags netgo -installsuffix netgo --ldflags '-w -extldflags "-static"' uhandles.go

clean:
rm uhandles
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# uhandles

An escalation of [jsonsubst](https://github.com/arbitar/jsonsubst).
Small static Go program with no external runtime dependencies that takes a [handlebars](https://handlebarsjs.com) template in from stdin, renders it with data from a JSON file (passed by argument), and outputs the result to stdout.
This is basically just a simple wrapper around [aymerick/raymond](https://github.com/aymerick/raymond).

## Usage
Very simple. One argument: JSON file.

`./uhandles data.json <input.txt >output.txt`

Or, in a pipeline of some kind...

`cat input.txt | ./uhandles data.json | tee output.txt`

## Build
If you're cool and already have a good working Go build environment on your system, just run `make build`.

I'm not cool though, so I use Docker for this kind of thing, which is why it's the default make target. Just run `make` on a system with Docker running on it, and a build image will be created, `make build` executed within the container where all the prerequisites exist, and the build image will be removed after it's done.

## Notes
No external runtime dependencies, so it should run anywhere. Which means it'll run inside your quick-and-dirty Docker stuff without complaint.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/arbitar/uhandles

go 1.19

require github.com/aymerick/raymond v2.0.2+incompatible

require gopkg.in/yaml.v2 v2.4.0 // indirect
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
47 changes: 47 additions & 0 deletions uhandles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/aymerick/raymond"
)

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: uhandlebars <json file>")
os.Exit(1)
}

jsonFile := os.Args[1]

jsonData, err := ioutil.ReadFile(jsonFile)
if err != nil {
fmt.Println("Error reading JSON file:", err)
os.Exit(1)
}

var data interface{}
err = json.Unmarshal(jsonData, &data)
if err != nil {
fmt.Println("Error parsing JSON file:", err)
os.Exit(1)
}

template, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println("Error reading template from stdin:", err)
os.Exit(1)
}

result, err := raymond.Render(string(template), data)
if err != nil {
fmt.Println("Error rendering template:", err)
os.Exit(1)
}

fmt.Println(strings.TrimSpace(result))
}

0 comments on commit 880123c

Please sign in to comment.