Update mapstructure-to-hcl2.go

This commit is contained in:
Adrien Delorme 2020-03-13 15:44:13 +01:00
parent 1c27d9d04b
commit ae4b158ca3
1 changed files with 8 additions and 6 deletions

View File

@ -41,6 +41,8 @@ import (
"golang.org/x/tools/imports"
)
const mapstructureToHCL2 = "mapstructure-to-hcl2"
var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
output = flag.String("output", "", "output file name; default srcdir/<type>_hcl2.go")
@ -49,15 +51,15 @@ var (
// Usage is a replacement usage function for the flags package.
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of mapstructure-to-hcl2:\n")
fmt.Fprintf(os.Stderr, "\tflatten-mapstructure [flags] -type T[,T...] pkg\n")
fmt.Fprintf(os.Stderr, "Usage of "+mapstructureToHCL2+":\n")
fmt.Fprintf(os.Stderr, "\t"+mapstructureToHCL2+" [flags] -type T[,T...] pkg\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix("mapstructure-to-hcl2: ")
log.SetPrefix(mapstructureToHCL2 + ": ")
flag.Usage = Usage
flag.Parse()
if len(*typeNames) == 0 {
@ -76,7 +78,7 @@ func main() {
if goFile := os.Getenv("GOFILE"); goFile != "" {
outputPath = goFile[:len(goFile)-2] + "hcl2spec.go"
}
log.SetPrefix(fmt.Sprintf("mapstructure-to-hcl2: %s.%v: ", os.Getenv("GOPACKAGE"), typeNames))
log.SetPrefix(fmt.Sprintf(mapstructureToHCL2+": %s.%v: ", os.Getenv("GOPACKAGE"), typeNames))
cfg := &packages.Config{
Mode: packages.LoadSyntax,
@ -138,7 +140,7 @@ func main() {
out := bytes.NewBuffer(nil)
fmt.Fprintf(out, `// Code generated by "mapstructure-to-hcl2 %s"; DO NOT EDIT.`, strings.Join(os.Args[1:], " "))
fmt.Fprintf(out, `// Code generated by "%s %s"; DO NOT EDIT.`, mapstructureToHCL2, strings.Join(os.Args[1:], " "))
fmt.Fprintf(out, "\npackage %s\n", topPkg.Name)
delete(usedImports, NamePath{topPkg.Name, topPkg.PkgPath})
@ -225,7 +227,7 @@ func outputStructHCL2SpecBody(w io.Writer, s *types.Struct) {
// outputHCL2SpecField writes the values of the `map[string]hcldec.Spec` map
// supposed to define the HCL spec of a struct.
func outputHCL2SpecField(w io.Writer, accessor string, fieldType types.Type, tag *structtag.Tags) {
if m2h, err := tag.Get(""); err == nil && m2h.HasOption("self-defined") {
if m2h, err := tag.Get(mapstructureToHCL2); err == nil && m2h.HasOption("self-defined") {
fmt.Fprintf(w, `(&%s{}).HCL2Spec()`, fieldType.String())
return
}