good morning!!!!

Skip to content
Snippets Groups Projects
Commit 5d3082b5 authored by a's avatar a
Browse files

change cli flag

parent b3d984c1
No related branches found
No related tags found
No related merge requests found
Showing
with 2100 additions and 129 deletions
...@@ -10,7 +10,6 @@ require ( ...@@ -10,7 +10,6 @@ require (
github.com/ethereum/go-ethereum v1.10.22 github.com/ethereum/go-ethereum v1.10.22
github.com/go-openapi/spec v0.20.7 github.com/go-openapi/spec v0.20.7
github.com/gobuffalo/packr/v2 v2.8.3 github.com/gobuffalo/packr/v2 v2.8.3
github.com/gregdhill/go-openrpc v0.0.0-20220114144539-ae6f44720487
github.com/imdario/mergo v0.3.13 github.com/imdario/mergo v0.3.13
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/test-go/testify v1.1.4 github.com/test-go/testify v1.1.4
...@@ -22,6 +21,7 @@ require ( ...@@ -22,6 +21,7 @@ require (
require ( require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/alecthomas/kong v0.6.1 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect github.com/go-openapi/jsonreference v0.20.0 // indirect
......
This diff is collapsed.
...@@ -47,8 +47,9 @@ type jsonrpcMessage struct { ...@@ -47,8 +47,9 @@ type jsonrpcMessage struct {
ID *ID `json:"id,omitempty"` ID *ID `json:"id,omitempty"`
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"` Params json.RawMessage `json:"params,omitempty"`
Error *jsonError `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"` Result json.RawMessage `json:"result,omitempty"`
Error *jsonError `json:"error,omitempty"`
} }
func MakeCall(id int, method string, params []any) *JsonRpcMessage { func MakeCall(id int, method string, params []any) *JsonRpcMessage {
......
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"gfx.cafe/open/jrpc/openrpc/generate"
"gfx.cafe/open/jrpc/openrpc/parse"
"gfx.cafe/open/jrpc/openrpc/types"
"github.com/alecthomas/kong"
"github.com/gobuffalo/packr/v2"
)
var CLI struct {
Compile CompileCommand `cmd:"" help:"Compile a folder into a single openrpc spec"`
Generate GenerateCommand `cmd:"" help:"Compile a folder into a single openrpc spec"`
}
type CompileCommand struct {
Methods []string `name:"methods" short:"m" help:"root of method dirs" type:"path"`
Schemas []string `name:"schemas" short:"s" help:"root schema dirs" type:"path"`
Output string `name:"output" short:"o" help:"path to output file"`
}
func (c *CompileCommand) Run() error {
openrpc := types.NewOpenRPCSpec1()
var err error
for _, v := range c.Methods {
err = openrpc.AddMethods(v)
if err != nil {
return err
}
}
for _, v := range c.Schemas {
err = openrpc.AddSchemas(v)
if err != nil {
return err
}
}
jzn, err := json.MarshalIndent(openrpc, "", " ")
if err != nil {
return err
}
err = os.WriteFile(c.Output, jzn, 0644)
if err != nil {
return err
}
return nil
}
type GenerateCommand struct {
Spec string `name:"spec" short:"s" help:"path to jopenrpc spec"`
Output string `name:"output" short:"o" help:"output directory and package"`
Templates []string `name:"templates" short:"t" help:"list of template types to generate for"`
}
func (c *GenerateCommand) Run() error {
if c.Spec == "" {
return fmt.Errorf("spec file is required")
}
openrpc, err := readSpec(c.Spec)
if err != nil {
return err
}
parse.GetTypes(openrpc, openrpc.Objects)
box := packr.New("template", "./templates")
if err = generate.WriteFile(box, "server", c.Output, openrpc); err != nil {
return err
}
if err = generate.WriteFile(box, "types", c.Output, openrpc); err != nil {
return err
}
return nil
}
func readSpec(file string) (*types.OpenRPCSpec1, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
spec := types.NewOpenRPCSpec1()
err = json.Unmarshal(data, spec)
if err != nil {
return nil, err
}
return spec, nil
}
func NewCLI() *kong.Context {
ctx := kong.Parse(&CLI)
return ctx
}
package main package main
import ( import (
"encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"gfx.cafe/open/jrpc/openrpc/generate"
"gfx.cafe/open/jrpc/openrpc/parse"
"gfx.cafe/open/jrpc/openrpc/types"
"github.com/gobuffalo/packr/v2"
)
var (
pkgDir string
specFile string
cliGen bool
cliCommandName string
) )
func init() {
flag.StringVar(&pkgDir, "dir", "rpc", "set the target directory")
flag.StringVar(&specFile, "spec", "", "the openrpc compliant spec")
flag.StringVar(&cliCommandName, "cli.name", "CHANGEME", "With -cli, names binary program. Default is FIXME.")
flag.BoolVar(&cliGen, "cli", false, "Toggle CLI program generation")
}
func readSpec(file string) (*types.OpenRPCSpec1, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
spec := types.NewOpenRPCSpec1()
err = json.Unmarshal(data, spec)
if err != nil {
return nil, err
}
return spec, nil
}
func run() error {
flag.Parse()
if specFile == "" {
return fmt.Errorf("spec file is required")
}
openrpc, err := readSpec(specFile)
if err != nil {
return err
}
parse.GetTypes(openrpc, openrpc.Objects)
box := packr.New("template", "./templates")
if err = generate.WriteFile(box, "server", pkgDir, openrpc); err != nil {
return err
}
if err = generate.WriteFile(box, "types", pkgDir, openrpc); err != nil {
return err
}
if cliGen {
generate.ProgramName = cliCommandName
if err = generate.WriteFile(box, "cli", "main", openrpc); err != nil {
return err
}
if err = generate.WriteFile(box, "cli_cmd", "cmd", openrpc); err != nil {
return err
}
}
return nil
}
func main() { func main() {
if err := run(); err != nil { ctx := NewCLI()
if err := ctx.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
......
...@@ -4,9 +4,9 @@ import ( ...@@ -4,9 +4,9 @@ import (
"os" "os"
"testing" "testing"
"gfx.cafe/open/jrpc/openrpc/generate"
"gfx.cafe/open/jrpc/openrpc/parse"
packr "github.com/gobuffalo/packr/v2" packr "github.com/gobuffalo/packr/v2"
"github.com/gregdhill/go-openrpc/generate"
"github.com/gregdhill/go-openrpc/parse"
) )
func generateExampleProxyServer() error { func generateExampleProxyServer() error {
......
This diff is collapsed.
...@@ -7,8 +7,8 @@ import ( ...@@ -7,8 +7,8 @@ import (
"testing" "testing"
"text/template" "text/template"
"github.com/gregdhill/go-openrpc/parse" "gfx.cafe/open/jrpc/openrpc/parse"
"github.com/gregdhill/go-openrpc/types" "gfx.cafe/open/jrpc/openrpc/types"
"github.com/test-go/testify/require" "github.com/test-go/testify/require"
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment