Created
March 31, 2022 17:50
-
-
Save weakpixel/5825b28175a7b6797d1def97af2287b3 to your computer and use it in GitHub Desktop.
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
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