Skip to content

Instantly share code, notes, and snippets.

@olivoil
Last active July 20, 2017 15:58
Show Gist options
  • Save olivoil/1348a7e78db264c5e6ca876140fb1948 to your computer and use it in GitHub Desktop.
Save olivoil/1348a7e78db264c5e6ca876140fb1948 to your computer and use it in GitHub Desktop.
example of what an `up` configuration file would look like in HCL
name = "api"
description = "Some API for whatever"
hooks {
build = "make build"
clean = "make clean"
}
environment {
BAR = "here"
FOO = "here"
}
cors {
allowed_origins = ["example.com"]
allowed_methods = ["GET"]
max_age = 123123
}
lambda {
memory = 1024
timeout = 5
}
dns {
zone "apex.sh" {
record "blog.apex.sh" {
type = "CNAME"
ttl = 300
}
}
}
{
"name": "api",
"description": "Some API for whatever",
"hooks": {
"build": "make build",
"clean": "make clean"
},
"environment": {
"BAR": "here",
"FOO": "here"
},
"cors": {
"allowed_origins": null,
"allowed_methods": null,
"max_age": 0
},
"lambda": {
"memory": 1024,
"timeout": 5
},
"dns": {
"zone": {
"apex.sh": {
"record": {
"blog.apex.sh": {
"type": "CNAME",
"ttl": 300
}
}
}
}
}
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/hashicorp/hcl"
)
type Config struct {
Name string `json:"name"`
Description string `json:"description"`
Hooks struct {
Build string `json:"build"`
Clean string `json:"clean"`
} `json:"hooks"`
Environment map[string]string `json:"environment"`
Cors struct {
AllowedOrigins []string `json:"allowed_origins"`
AllowedMethods []string `json:"allowed_methods"`
MaxAge int `json:"max_age"`
} `json:"cors"`
Lambda struct {
Memory int `json:"memory"`
Timeout int `json:"timeout"`
} `json:"lambda"`
Dns struct {
Zone map[string]struct {
Record map[string]struct {
Type string `json:"type"`
Ttl int `json:"ttl"`
} `json:"record"`
} `json:"zone"`
} `json:"dns"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
content, err := ioutil.ReadFile("./config.hcl")
check(err)
config := Config{}
err = hcl.Unmarshal(content, &config)
check(err)
out, err := json.MarshalIndent(config, "", " ")
check(err)
fmt.Printf("json equivalent:\n%v\n", string(out))
err = ioutil.WriteFile("./config.json", out, 0644)
check(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment