This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
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) | |
var runner Runner | |
switch step.Type { | |
case "mkdir": | |
runner = &MkdirStep{} | |
case "exec": | |
runner = &ExecStep{} | |
default: | |
fmt.Printf("Unknown step type %q\n", step.Type) | |
os.Exit(1) | |
} | |
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) | |
} | |
} | |
} | |
} | |
type Config struct { | |
Tasks []*Task `hcl:"task,block"` | |
} | |
type Task struct { | |
Name string `hcl:"name,label"` | |
Steps []*Step `hcl:"step,block"` | |
} | |
type Step struct { | |
Type string `hcl:"type,label"` | |
Name string `hcl:"name,label"` | |
Remain hcl.Body `hcl:",remain"` | |
} | |
type ExecStep struct { | |
Command string `hcl:"command"` | |
} | |
func (s *ExecStep) Run() error { | |
// Implement me | |
return nil | |
} | |
type MkdirStep struct { | |
Path string `hcl:"path"` | |
} | |
func (s *MkdirStep) Run() error { | |
// Implement me | |
return nil | |
} | |
type Runner interface { | |
Run() error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment