Update mapstructure-to-hcl2.go
This commit is contained in:
parent
1c27d9d04b
commit
ae4b158ca3
|
@ -41,6 +41,8 @@ import (
|
||||||
"golang.org/x/tools/imports"
|
"golang.org/x/tools/imports"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const mapstructureToHCL2 = "mapstructure-to-hcl2"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
|
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")
|
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.
|
// Usage is a replacement usage function for the flags package.
|
||||||
func Usage() {
|
func Usage() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage of mapstructure-to-hcl2:\n")
|
fmt.Fprintf(os.Stderr, "Usage of "+mapstructureToHCL2+":\n")
|
||||||
fmt.Fprintf(os.Stderr, "\tflatten-mapstructure [flags] -type T[,T...] pkg\n")
|
fmt.Fprintf(os.Stderr, "\t"+mapstructureToHCL2+" [flags] -type T[,T...] pkg\n")
|
||||||
fmt.Fprintf(os.Stderr, "Flags:\n")
|
fmt.Fprintf(os.Stderr, "Flags:\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
log.SetPrefix("mapstructure-to-hcl2: ")
|
log.SetPrefix(mapstructureToHCL2 + ": ")
|
||||||
flag.Usage = Usage
|
flag.Usage = Usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if len(*typeNames) == 0 {
|
if len(*typeNames) == 0 {
|
||||||
|
@ -76,7 +78,7 @@ func main() {
|
||||||
if goFile := os.Getenv("GOFILE"); goFile != "" {
|
if goFile := os.Getenv("GOFILE"); goFile != "" {
|
||||||
outputPath = goFile[:len(goFile)-2] + "hcl2spec.go"
|
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{
|
cfg := &packages.Config{
|
||||||
Mode: packages.LoadSyntax,
|
Mode: packages.LoadSyntax,
|
||||||
|
@ -138,7 +140,7 @@ func main() {
|
||||||
|
|
||||||
out := bytes.NewBuffer(nil)
|
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)
|
fmt.Fprintf(out, "\npackage %s\n", topPkg.Name)
|
||||||
|
|
||||||
delete(usedImports, NamePath{topPkg.Name, topPkg.PkgPath})
|
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
|
// outputHCL2SpecField writes the values of the `map[string]hcldec.Spec` map
|
||||||
// supposed to define the HCL spec of a struct.
|
// supposed to define the HCL spec of a struct.
|
||||||
func outputHCL2SpecField(w io.Writer, accessor string, fieldType types.Type, tag *structtag.Tags) {
|
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())
|
fmt.Fprintf(w, `(&%s{}).HCL2Spec()`, fieldType.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue