-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go.{mod,sum}, cmd/wit-bindgen-go, internal/witcli: use github.com/urf…
…ave/cli/v3 for CLI programs
- Loading branch information
Showing
5 changed files
with
94 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |