2020-05-28 18:49:50 -04:00
package main
import (
2020-10-09 20:01:55 -04:00
"bytes"
2020-05-28 18:49:50 -04:00
"flag"
"fmt"
2020-10-09 20:01:55 -04:00
"go/format"
2020-05-28 18:49:50 -04:00
"os"
"path/filepath"
"strings"
"text/template"
"github.com/hashicorp/packer/fix"
)
2020-10-09 20:01:55 -04:00
var deprecatedOptsTemplate = template . Must ( template . New ( "deprecatedOptsTemplate" ) . Funcs ( template . FuncMap { "StringsJoin" : strings . Join } ) . Parse ( ` //<!-- Code generated by generate-fixer-deprecations; DO NOT EDIT MANUALLY -->
2020-05-28 18:49:50 -04:00
package config
2020-10-09 20:01:55 -04:00
var DeprecatedOptions = map [ string ] [ ] string {
{ { - range $ key , $ value := . DeprecatedOpts } }
"{{$key}}" : [ ] string { "{{ StringsJoin . " \ ", \"" } } " } ,
2020-05-28 18:49:50 -04:00
{ { - end } }
2020-05-28 19:22:55 -04:00
}
` ) )
2020-05-28 18:49:50 -04:00
type executeOpts struct {
2020-10-09 20:01:55 -04:00
DeprecatedOpts map [ string ] [ ] string
2020-05-28 18:49:50 -04:00
}
func main ( ) {
// Figure out location in directory structure
args := flag . Args ( )
if len ( args ) == 0 {
// Default: process the file
args = [ ] string { os . Getenv ( "GOFILE" ) }
}
fname := args [ 0 ]
absFilePath , err := filepath . Abs ( fname )
if err != nil {
panic ( err )
}
2020-08-06 16:24:15 -04:00
paths := strings . Split ( absFilePath , "cmd" + string ( os . PathSeparator ) +
"generate-fixer-deprecations" + string ( os . PathSeparator ) + "main.go" )
2020-05-28 18:49:50 -04:00
packerDir := paths [ 0 ]
// Load all deprecated options from all active fixers
2020-10-09 20:01:55 -04:00
allDeprecatedOpts := map [ string ] [ ] string { }
2020-05-28 18:49:50 -04:00
for _ , name := range fix . FixerOrder {
fixer , ok := fix . Fixers [ name ]
if ! ok {
panic ( "fixer not found: " + name )
}
deprecated := fixer . DeprecatedOptions ( )
2020-10-09 20:01:55 -04:00
for k , v := range deprecated {
if allDeprecatedOpts [ k ] == nil {
allDeprecatedOpts [ k ] = v
} else {
allDeprecatedOpts [ k ] = append ( allDeprecatedOpts [ k ] , v ... )
}
}
2020-05-28 18:49:50 -04:00
}
deprecated_path := filepath . Join ( packerDir , "helper" , "config" ,
"deprecated_options.go" )
2020-10-09 20:01:55 -04:00
buf := bytes . Buffer { }
// execute template into buffer
deprecated := & executeOpts { DeprecatedOpts : allDeprecatedOpts }
err = deprecatedOptsTemplate . Execute ( & buf , deprecated )
2020-10-12 19:26:12 -04:00
if err != nil {
panic ( err )
}
2020-10-09 20:01:55 -04:00
// we've written unformatted go code to the file. now we have to format it.
out , err := format . Source ( buf . Bytes ( ) )
if err != nil {
panic ( err )
}
2020-05-28 18:49:50 -04:00
outputFile , err := os . Create ( deprecated_path )
if err != nil {
panic ( err )
}
2020-10-09 20:01:55 -04:00
_ , err = outputFile . Write ( out )
2020-05-28 18:49:50 -04:00
defer outputFile . Close ( )
if err != nil {
fmt . Printf ( "%v" , err )
os . Exit ( 1 )
}
}