From 166136165fd74b7788e8cd9eee5dbdc10b939f3b Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Thu, 21 Sep 2023 14:57:17 -0700 Subject: [PATCH] go.{mod,sum}, cmd/wit-bindgen-go, internal/witcli: use github.com/urfave/cli/v3 for CLI programs --- cmd/wit-bindgen-go/cmd/describe/describe.go | 31 +++++++++++ cmd/wit-bindgen-go/main.go | 60 ++++----------------- go.mod | 4 ++ go.sum | 12 ++++- internal/witcli/witcli.go | 38 +++++++++++++ 5 files changed, 94 insertions(+), 51 deletions(-) create mode 100644 cmd/wit-bindgen-go/cmd/describe/describe.go create mode 100644 internal/witcli/witcli.go diff --git a/cmd/wit-bindgen-go/cmd/describe/describe.go b/cmd/wit-bindgen-go/cmd/describe/describe.go new file mode 100644 index 00000000..f615073e --- /dev/null +++ b/cmd/wit-bindgen-go/cmd/describe/describe.go @@ -0,0 +1,31 @@ +package describe + +import ( + "fmt" + + "github.com/k0kubun/pp/v3" + "github.com/urfave/cli/v3" + "github.com/ydnar/wasm-tools-go/internal/witcli" +) + +// Command is the CLI command for describe. +var Command = &cli.Command{ + Name: "describe", + Usage: "describe a WIT JSON file", + Action: action, +} + +func action(ctx *cli.Context) error { + res, err := witcli.LoadOne(ctx.Args().Slice()...) + if err != nil { + return err + } + + fmt.Printf("// %d worlds(s), %d packages(s), %d interfaces(s), %d types(s)\n", + len(res.Worlds), len(res.Packages), len(res.Interfaces), len(res.TypeDefs)) + p := pp.New() + p.SetExportedOnly(true) + p.Print(res) + + return nil +} diff --git a/cmd/wit-bindgen-go/main.go b/cmd/wit-bindgen-go/main.go index 02eb07d4..c21a37a4 100644 --- a/cmd/wit-bindgen-go/main.go +++ b/cmd/wit-bindgen-go/main.go @@ -1,63 +1,25 @@ package main import ( - "flag" + "context" "fmt" - "io" "os" - "github.com/k0kubun/pp/v3" - "github.com/ydnar/wasm-tools-go/wit" + "github.com/urfave/cli/v3" + "github.com/ydnar/wasm-tools-go/cmd/wit-bindgen-go/cmd/describe" ) func main() { - err := Main() - if err != nil { - fmt.Printf("error: %v\n", err) - os.Exit(1) + cmd := &cli.Command{ + Name: "wit-bindgen-go", + Commands: []*cli.Command{ + describe.Command, + }, } -} -func Main() error { - flag.Parse() - args := flag.Args() - if len(args) == 0 { - args = []string{"-"} - } - for _, arg := range args { - if arg == "-" { - err := describe(os.Stdin, "STDIN") - if err != nil { - return err - } - } - f, err := os.Open(arg) - if err != nil { - return err - } - err = describe(f, arg) - f.Close() - if err != nil { - return err - } - } - return nil -} - -func describe(r io.Reader, name string) error { - res, err := wit.DecodeJSON(r) + err := cmd.Run(context.Background(), os.Args) if err != nil { - return err + fmt.Printf("error: %v\n", err) + os.Exit(1) } - - fmt.Printf("// Describing WIT from %s\n", name) - fmt.Printf("// %d worlds(s), %d packages(s), %d interfaces(s), %d types(s)\n", - len(res.Worlds), len(res.Packages), len(res.Interfaces), len(res.TypeDefs)) - fmt.Println() - - p := pp.New() - p.SetExportedOnly(true) - p.Print(res) - - return nil } diff --git a/go.mod b/go.mod index 055973c3..1150f19f 100644 --- a/go.mod +++ b/go.mod @@ -6,11 +6,15 @@ require ( github.com/coreos/go-semver v0.3.1 github.com/k0kubun/pp/v3 v3.2.0 github.com/sergi/go-diff v1.3.1 + github.com/urfave/cli/v3 v3.0.0-alpha4 ) require ( + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index 0082c36a..063b7a4a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -16,11 +18,18 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/urfave/cli/v3 v3.0.0-alpha4 h1:RJFGIs3mcalmc2YgliDh0Pa4l79S+Dqdz7cW8Fcp7Rg= +github.com/urfave/cli/v3 v3.0.0-alpha4/go.mod h1:ZFqSEHhze0duJACOdz43I5IcnKhf4RoTlOoUMBUggOI= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/ydnar/pretty v0.0.0-20230919215528-0880c4e00e56/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -31,7 +40,6 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/witcli/witcli.go b/internal/witcli/witcli.go new file mode 100644 index 00000000..19e4a2a1 --- /dev/null +++ b/internal/witcli/witcli.go @@ -0,0 +1,38 @@ +package witcli + +import ( + "fmt" + "os" + + "github.com/ydnar/wasm-tools-go/wit" +) + +// LoadOne loads one WIT JSON file from paths. +// An error is returned if len(paths) > 1. +// If paths is empty, or paths[0] == "" or "-", then it reads from stdin. +func LoadOne(paths ...string) (*wit.Resolve, error) { + var path string + switch len(paths) { + case 0: + path = "-" + case 1: + path = paths[0] + default: + return nil, fmt.Errorf("found %d path arguments, expecting 0 or 1", len(paths)) + } + return LoadJSON(path) +} + +// LoadJSON loads a WIT JSON file from path. +// If path is "" or "-", it reads from os.Stdin. +func LoadJSON(path string) (*wit.Resolve, error) { + if path == "" || path == "-" { + return wit.DecodeJSON(os.Stdin) + } + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + return wit.DecodeJSON(f) +}