Skip to content

Instantly share code, notes, and snippets.

@weakpixel
Created March 30, 2022 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weakpixel/8501a0dabeeb800ef38b6d1cc9fa1e72 to your computer and use it in GitHub Desktop.
Save weakpixel/8501a0dabeeb800ef38b6d1cc9fa1e72 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"github.com/hashicorp/hcl/v2/hclsimple"
)
var (
exampleHCL = `
task "first_task" {
exec "list_current_dir" {
command = "ls ."
}
exec "list_var_dir" {
command = "ls /var"
}
}
`
)
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 %q\n", step.Name, step.Command)
err = step.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}
type Config struct {
Tasks []*Task `hcl:"task,block"`
}
type Task struct {
Name string `hcl:"name,label"`
Steps []*ExecStep `hcl:"exec,block"`
}
type ExecStep struct {
Name string `hcl:"name,label"`
Command string `hcl:"command"`
}
func (s *ExecStep) Run() error {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment