policygen: use format.Source() directly in code.

This commit is contained in:
Hector Sanjuan 2019-05-15 10:46:03 +02:00
parent b1996970c5
commit 59fdff97f2
3 changed files with 18 additions and 16 deletions

View File

@ -2,7 +2,6 @@ package ipfscluster
import ( import (
"context" "context"
"fmt"
"github.com/ipfs/ipfs-cluster/api" "github.com/ipfs/ipfs-cluster/api"
"github.com/ipfs/ipfs-cluster/version" "github.com/ipfs/ipfs-cluster/version"

View File

@ -1,5 +0,0 @@
all:
@go run policygen.go
install:
@go run policygen.go | gofmt > policy.txt
@mv policy.txt ../../rpc_policy.go

View File

@ -2,8 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"go/format"
"os" "os"
"reflect" "reflect"
"strings"
cluster "github.com/ipfs/ipfs-cluster" cluster "github.com/ipfs/ipfs-cluster"
) )
@ -57,20 +59,20 @@ func main() {
============================================================================`) ============================================================================`)
fmt.Fprintln(os.Stderr) fmt.Fprintln(os.Stderr)
fmt.Println("package ipfscluster") var rpcPolicyDotGo strings.Builder
fmt.Println()
fmt.Println("// This file can be generated with rpcutil/policygen.") rpcPolicyDotGo.WriteString("package ipfscluster\n\n")
fmt.Println() rpcPolicyDotGo.WriteString("// This file can be generated with rpcutil/policygen.\n\n")
fmt.Println(` rpcPolicyDotGo.WriteString(`
// DefaultRPCPolicy associates all rpc endpoints offered by cluster peers to an // DefaultRPCPolicy associates all rpc endpoints offered by cluster peers to an
// endpoint type. See rpcutil/policygen.go as a quick way to generate this // endpoint type. See rpcutil/policygen.go as a quick way to generate this
// without missing any endpoint.`) // without missing any endpoint.`)
fmt.Println("var DefaultRPCPolicy = map[string]RPCEndpointType{") rpcPolicyDotGo.WriteString("\nvar DefaultRPCPolicy = map[string]RPCEndpointType{\n")
for _, c := range rpcComponents { for _, c := range rpcComponents {
t := reflect.TypeOf(c) t := reflect.TypeOf(c)
fmt.Println(" //", cluster.RPCServiceID(c), "methods") rpcPolicyDotGo.WriteString("// " + cluster.RPCServiceID(c) + " methods\n")
for i := 0; i < t.NumMethod(); i++ { for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i) method := t.Method(i)
name := cluster.RPCServiceID(c) + "." + method.Name name := cluster.RPCServiceID(c) + "." + method.Name
@ -84,10 +86,16 @@ func main() {
comment = "// " + comment comment = "// " + comment
} }
fmt.Printf(" \"%s\": %s, %s\n", name, rpcTStr, comment) fmt.Fprintf(&rpcPolicyDotGo, "\"%s\": %s, %s\n", name, rpcTStr, comment)
} }
fmt.Println() rpcPolicyDotGo.WriteString("\n")
} }
fmt.Println("}") rpcPolicyDotGo.WriteString("}\n")
src, err := format.Source([]byte(rpcPolicyDotGo.String()))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(string(src))
} }