From ae4b158ca3caebb42daf185c1cf3ef8e081a69d5 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 13 Mar 2020 15:44:13 +0100 Subject: [PATCH] Update mapstructure-to-hcl2.go --- cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go b/cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go index 92ad43dda..dde8b55a4 100644 --- a/cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go +++ b/cmd/mapstructure-to-hcl2/mapstructure-to-hcl2.go @@ -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/_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 }