2019-03-06 15:04:18 +00:00
|
|
|
package main
|
|
|
|
|
2019-08-04 14:36:44 +00:00
|
|
|
//go:generate go run assets_generate.go
|
|
|
|
|
2019-03-06 15:04:18 +00:00
|
|
|
import (
|
2019-08-03 16:18:48 +00:00
|
|
|
"encoding/json"
|
2019-08-04 04:35:44 +00:00
|
|
|
"fmt"
|
2019-03-06 15:04:18 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-08-04 04:35:44 +00:00
|
|
|
"io/ioutil"
|
2019-03-06 15:04:18 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2019-08-03 16:18:48 +00:00
|
|
|
"text/template"
|
2019-03-06 15:04:18 +00:00
|
|
|
)
|
|
|
|
|
2019-08-03 16:18:48 +00:00
|
|
|
type CliFlagInfo struct {
|
|
|
|
PackageName string
|
|
|
|
Flags []FlagType
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlagType struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value bool `json:"value"`
|
|
|
|
Destination bool `json:"dest"`
|
|
|
|
Doctail string `json:"doctail"`
|
|
|
|
ContextDefault string `json:"context_default"`
|
|
|
|
ContextType string `json:"context_type"`
|
|
|
|
Parser string `json:"parser"`
|
|
|
|
ParserCast string `json:"parser_cast"`
|
2019-08-03 10:41:50 +00:00
|
|
|
ValueString string `json:"valueString"`
|
2019-08-09 07:05:55 +00:00
|
|
|
TakesFile bool `json:"takes_file"`
|
2019-08-03 16:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2019-08-03 16:44:58 +00:00
|
|
|
|
2019-08-04 05:39:34 +00:00
|
|
|
app.Name = "flag-generator"
|
2019-08-04 04:35:44 +00:00
|
|
|
app.Usage = "Generate flag type code!"
|
2019-08-03 16:44:58 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
app.Action = ActionFunc
|
2019-08-03 16:44:58 +00:00
|
|
|
|
2019-08-04 04:58:08 +00:00
|
|
|
err := app.Run(os.Args)
|
2019-08-04 04:35:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-08-03 16:44:58 +00:00
|
|
|
}
|
2019-08-03 16:18:48 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
func ActionFunc(_ *cli.Context) error {
|
|
|
|
var info CliFlagInfo
|
|
|
|
var tpl *template.Template
|
2019-03-06 15:04:18 +00:00
|
|
|
|
2019-08-04 04:58:08 +00:00
|
|
|
inFile, err := assets.Open("/source/flag-types.json")
|
2019-08-04 04:35:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2019-03-06 15:04:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
decoder := json.NewDecoder(inFile)
|
|
|
|
err = decoder.Decode(&info.Flags)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-08-03 16:44:58 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
err = inFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, packageName := range []string{"cli", "altsrc"} {
|
2019-08-03 16:18:48 +00:00
|
|
|
info.PackageName = packageName
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
bytes, err := ReadTemplate(packageName)
|
2019-08-03 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
tpl = template.Must(template.New("").Parse(string(bytes)))
|
2019-08-03 16:18:48 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
var outFile *os.File
|
2019-08-03 16:18:48 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
if packageName == "cli" {
|
|
|
|
outFile, err = os.Create("flag_generated.go")
|
|
|
|
} else {
|
|
|
|
outFile, err = os.Create("altsrc/flag_generated.go")
|
|
|
|
}
|
2019-08-03 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
err = tpl.Execute(outFile, info)
|
2019-08-03 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
err = outFile.Close()
|
2019-08-03 16:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-08-04 04:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-03 16:18:48 +00:00
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
func ReadTemplate(packageName string) ([]byte, error) {
|
2019-08-04 04:58:08 +00:00
|
|
|
templateFile, err := assets.Open(fmt.Sprintf("/templates/%s_flags_generated.gotpl", packageName))
|
2019-08-04 04:35:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-03 16:18:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 04:35:44 +00:00
|
|
|
templateFileBytes, err := ioutil.ReadAll(templateFile)
|
2019-03-06 15:04:18 +00:00
|
|
|
if err != nil {
|
2019-08-04 04:35:44 +00:00
|
|
|
return nil, err
|
2019-03-06 15:04:18 +00:00
|
|
|
}
|
2019-08-04 04:35:44 +00:00
|
|
|
|
|
|
|
return templateFileBytes, nil
|
2019-03-06 15:04:18 +00:00
|
|
|
}
|