Created
November 12, 2019 13:49
-
-
Save ont/bbcf37361c597cabe174b2e88d919b8e to your computer and use it in GitHub Desktop.
Example of parsing part of nomad's job hcl file.
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" | |
hcl "github.com/hashicorp/hcl/v2" | |
"github.com/hashicorp/hcl/v2/hclsimple" | |
//"github.com/zclconf/go-cty/cty" | |
) | |
type NomadJob struct { | |
Name string | |
Text string | |
} | |
func (j *NomadJob) GetImages() []string { | |
var data struct { | |
Job struct { | |
Name string `hcl:"job-name,label"` | |
Other hcl.Body `hcl:"other,remain"` | |
Groups []struct { | |
Name string `hcl:"group-name,label"` | |
Other hcl.Body `hcl:"other,remain"` | |
Tasks []struct { | |
Name string `hcl:"task-name,label"` | |
Other hcl.Body `hcl:"other,remain"` | |
Config struct { | |
Image hcl.Expression `hcl:"image,attr"` | |
Other hcl.Body `hcl:"other,remain"` | |
} `hcl:"config,block"` | |
} `hcl:"task,block"` | |
} `hcl:"group,block"` | |
} `hcl:"job,block"` | |
} | |
fmt.Println(j.Text) | |
err := hclsimple.Decode("test.hcl", []byte(j.Text), nil, &data) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("=====> %v\n", data.Job.Name) | |
for _, group := range data.Job.Groups { | |
for _, task := range group.Tasks { | |
//ctx := &hcl.EvalContext{ | |
// Variables: map[string]cty.Value{ | |
// "NOMAD_META_VERSION": cty.StringVal("latest"), | |
// }, | |
//} | |
//value, errs := task.Config.Image.Value(ctx) | |
//if errs != nil { | |
// panic(errs) | |
//} | |
//fmt.Printf("group %v task %v ----> %v \n", group.Name, task.Name, value.AsString()) | |
rg := task.Config.Image.Range() | |
fmt.Printf("group %v task %v ----> %v \n", group.Name, task.Name, string(rg.SliceBytes([]byte(j.Text)))) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment