Skip to content

Instantly share code, notes, and snippets.

@weakpixel
Created March 31, 2022 17:50
Show Gist options
  • Save weakpixel/5825b28175a7b6797d1def97af2287b3 to your computer and use it in GitHub Desktop.
Save weakpixel/5825b28175a7b6797d1def97af2287b3 to your computer and use it in GitHub Desktop.
import (
"fmt"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsimple"
)
var (
exampleHCL = `
task "first_task" {
step "mkdir" "build_dir" {
path = "./build/"
}
step "exec" "list_build_dir" {
command = "ls ./build/"
}
}
`
)
func main() {
config := &Config{}
err := hclsimple.Decode("example.hcl", []byte(exampleHCL), nil, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, task := range config.Tasks {
fmt.Printf("Task: %s\n", task.Name)
for _, step := range task.Steps {
fmt.Printf(" Step: %s %s\n", step.Type, step.Name)
// The actual step implementation
// which implements the Runner interface
var runner Runner
// Determine the step implementation
switch step.Type {
case "mkdir":
runner = &MkdirStep{}
case "exec":
runner = &ExecStep{}
default:
fmt.Printf("Unknown step type %q\n", step.Type)
os.Exit(1)
}
// Decode remaining fields into our step implementation.
diags := gohcl.DecodeBody(step.Remain, nil, runner)
if diags.HasErrors() {
fmt.Println(diags)
os.Exit(1)
}
err = runner.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment