3
0

upd(v2 codegen): add perm generator, rename

This commit is contained in:
Tit Petric
2018-11-21 14:58:55 +01:00
parent e5ff3ae7b3
commit f8bdda0313
2 changed files with 61 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/crusttech/crust/internal/rbac"
)
func main() {
var (
pkg = flag.String("package", "main", "Package name")
input = flag.String("input", "", "Input .json filename")
output = flag.String("output", "", "Output .go filename")
fname = flag.String("function", "func Permissions() []rbac.OperationGroup", "Default function declaration")
)
flag.Parse()
export := func(s string) string {
s = strings.Replace(s, "true,", "true,\n", -1)
s = strings.Replace(s, "false,", "false,\n", -1)
s = strings.Replace(s, "{", "{\n", -1)
s = strings.Replace(s, "}", ",\n}", -1)
s = strings.Replace(s, "\", ", "\",\n", -1)
var w bytes.Buffer
fmt.Fprintln(&w, "package", *pkg)
fmt.Fprintln(&w)
fmt.Fprintln(&w, "import \"github.com/crusttech/crust/internal/rbac\"")
fmt.Fprintln(&w)
fmt.Fprintln(&w, "/* File is generated from", *input, "& main.go */")
fmt.Fprintln(&w)
fmt.Fprintln(&w, *fname, "{")
fmt.Fprintln(&w, "\treturn", s)
fmt.Fprintln(&w, "}")
return w.String()
}
var result []rbac.OperationGroup
f, err := os.Open(*input)
if err != nil {
log.Fatal(err)
}
if err := json.NewDecoder(f).Decode(&result); err != nil {
log.Fatal(err)
}
source := export(fmt.Sprintf("%#v", result))
if err := ioutil.WriteFile(*output, []byte(source), 0644); err != nil {
log.Fatal(err)
}
fmt.Println(*output)
}
@@ -142,6 +142,7 @@ func main() {
if output, err = os.Create(outputFile); err != nil {
exit(err)
}
fmt.Println(outputFile)
defer output.(io.Closer).Close()
}