Skip to content

Commit

Permalink
go.{mod,sum}, cmd/wit-bindgen-go, internal/witcli: use github.com/urf…
Browse files Browse the repository at this point in the history
…ave/cli/v3 for CLI programs
  • Loading branch information
ydnar committed Sep 21, 2023
1 parent 3305494 commit 1661361
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 51 deletions.
31 changes: 31 additions & 0 deletions cmd/wit-bindgen-go/cmd/describe/describe.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 11 additions & 49 deletions cmd/wit-bindgen-go/main.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand All @@ -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=
38 changes: 38 additions & 0 deletions internal/witcli/witcli.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1661361

Please sign in to comment.