Skip to content

Commit 38d3c35

Browse files
authored
Merge pull request #243 from bytecodealliance/ydnar/stdio
cmd/wit-bindgen-go, internal/witcli, wit: refactor handling of - as stdin
2 parents e105312 + 5de2ee2 commit 38d3c35

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88

99
- [#240](https://github.com/bytecodealliance/go-modules/issues/240): correctly handle cyclical data structures when generating variant lowering code.
1010

11+
### Changed
12+
13+
- Breaking: package `wit` no longer interprets `-` to read from stdin when loading JSON or WIT using `wit.LoadJSON` or `wit.LoadWIT`. Use `wit.DecodeJSON` or `wit.DecodeWIT` to read JSON or WIT from an `io.Reader`.
14+
- Breaking: `wit.ParseWIT` has been removed. Use `wit.DecodeWIT(bytes.NewReader(b))` instead.
15+
1116
## [v0.4.0] — 2024-11-05
1217

1318
This module has been renamed. Going forward, please use `go.bytecodealliance.org` instead of `github.com/bytecodealliance/wasm-tools-go`.

cmd/wit-bindgen-go/cmd/generate/generate.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"path/filepath"
88
"strings"
99

10+
"github.com/urfave/cli/v3"
11+
1012
"go.bytecodealliance.org/internal/codec"
1113
"go.bytecodealliance.org/internal/go/gen"
1214
"go.bytecodealliance.org/internal/witcli"
1315
"go.bytecodealliance.org/wit/bindgen"
1416
"go.bytecodealliance.org/wit/logging"
15-
"github.com/urfave/cli/v3"
1617
)
1718

1819
// Command is the CLI command for wit.
@@ -80,12 +81,12 @@ type config struct {
8081
}
8182

8283
func action(ctx context.Context, cmd *cli.Command) error {
83-
cfg, err := parseFlags(cmd)
84+
cfg, err := parseFlags(ctx, cmd)
8485
if err != nil {
8586
return err
8687
}
8788

88-
res, err := witcli.LoadWIT(ctx, cfg.forceWIT, cfg.path)
89+
res, err := witcli.LoadWIT(ctx, cfg.path, cmd.Reader, cfg.forceWIT)
8990
if err != nil {
9091
return err
9192
}
@@ -102,10 +103,10 @@ func action(ctx context.Context, cmd *cli.Command) error {
102103
return err
103104
}
104105

105-
return writeGoPackages(packages, cfg)
106+
return writeGoPackages(ctx, cmd, cfg, packages)
106107
}
107108

108-
func parseFlags(cmd *cli.Command) (*config, error) {
109+
func parseFlags(_ context.Context, cmd *cli.Command) (*config, error) {
109110
logger := witcli.Logger(cmd.Bool("verbose"), cmd.Bool("debug"))
110111
dryRun := cmd.Bool("dry-run")
111112
out := cmd.String("out")
@@ -145,7 +146,7 @@ func parseFlags(cmd *cli.Command) (*config, error) {
145146
}, nil
146147
}
147148

148-
func writeGoPackages(packages []*gen.Package, cfg *config) error {
149+
func writeGoPackages(_ context.Context, cmd *cli.Command, cfg *config, packages []*gen.Package) error {
149150
cfg.logger.Infof("Generated %d Go package(s)\n", len(packages))
150151
for _, pkg := range packages {
151152
if !pkg.HasContent() {
@@ -179,8 +180,8 @@ func writeGoPackages(packages []*gen.Package, cfg *config) error {
179180
}
180181

181182
if cfg.dryRun {
182-
fmt.Println(string(content))
183-
fmt.Println()
183+
fmt.Fprintln(cmd.Writer, string(content))
184+
fmt.Fprintln(cmd.Writer)
184185
continue
185186
}
186187

cmd/wit-bindgen-go/cmd/wit/wit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/urfave/cli/v3"
8+
79
"go.bytecodealliance.org/internal/witcli"
810
"go.bytecodealliance.org/wit"
9-
"github.com/urfave/cli/v3"
1011
)
1112

1213
// Command is the CLI command for wit.
@@ -31,7 +32,7 @@ func action(ctx context.Context, cmd *cli.Command) error {
3132
if err != nil {
3233
return err
3334
}
34-
res, err := witcli.LoadWIT(ctx, cmd.Bool("force-wit"), path)
35+
res, err := witcli.LoadWIT(ctx, path, cmd.Reader, cmd.Bool("force-wit"))
3536
if err != nil {
3637
return err
3738
}

cmd/wit-bindgen-go/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var version = &cli.Command{
6464
Name: "version",
6565
Usage: "print the version",
6666
Action: func(ctx context.Context, cmd *cli.Command) error {
67-
fmt.Printf("%s version %s\n", cmd.Root().Name, witcli.Version())
67+
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, witcli.Version())
6868
return nil
6969
},
7070
}

cmd/wit-bindgen-go/main_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package main
33
import (
44
"bytes"
55
"context"
6+
"strings"
67
"testing"
78
)
89

910
// TestSimpleGenVerbosity ensures that a basic generation case honors the verbose flag
1011
func TestSimpleGenVerbosity(t *testing.T) {
12+
inWIT := `package tests:test;`
13+
stdin := strings.NewReader(inWIT)
1114
var stdout bytes.Buffer
1215
var stderr bytes.Buffer
1316
cmd := Command
17+
cmd.Reader = stdin
1418
cmd.Writer = &stdout
1519
cmd.ErrWriter = &stderr
1620

@@ -20,7 +24,7 @@ func TestSimpleGenVerbosity(t *testing.T) {
2024
"--world",
2125
"http-fetch-simple",
2226
"--dry-run",
23-
"../../testdata/codegen/simple-http.wit",
27+
"-",
2428
}
2529

2630
// Run the app without verbose
@@ -29,6 +33,7 @@ func TestSimpleGenVerbosity(t *testing.T) {
2933
t.Errorf("output was written to stderr despite lack of --verbose")
3034
}
3135

36+
stdin.Reset(inWIT)
3237
stdout.Reset()
3338
stderr.Reset()
3439

internal/witcli/witcli.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package witcli
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
7+
"io"
68
"os"
79
"strings"
810

@@ -17,18 +19,25 @@ import (
1719
// If the resolved path doesn’t end in ".json", it will attempt to load
1820
// WIT indirectly by processing the input through wasm-tools.
1921
// If forceWIT is true, it will always process input through wasm-tools.
20-
func LoadWIT(ctx context.Context, forceWIT bool, path string) (*wit.Resolve, error) {
22+
func LoadWIT(ctx context.Context, path string, r io.Reader, forceWIT bool) (*wit.Resolve, error) {
2123
if oci.IsOCIPath(path) {
2224
fmt.Fprintf(os.Stderr, "Fetching OCI artifact %s\n", path)
23-
if bytes, err := oci.PullWIT(ctx, path); err != nil {
25+
if b, err := oci.PullWIT(ctx, path); err != nil {
2426
return nil, err
2527
} else {
26-
return wit.ParseWIT(bytes)
28+
return wit.DecodeWIT(bytes.NewReader(b))
2729
}
2830
}
29-
if forceWIT || (path != "" && path != "-" && !strings.HasSuffix(path, ".json")) {
31+
forceReader := path == "" || path == "-"
32+
if forceWIT || (!forceReader && !strings.HasSuffix(path, ".json")) {
33+
if forceReader {
34+
return wit.DecodeWIT(r)
35+
}
3036
return wit.LoadWIT(path)
3137
}
38+
if forceReader {
39+
return wit.DecodeJSON(r)
40+
}
3241
return wit.LoadJSON(path)
3342
}
3443

wit/load.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import (
1010
)
1111

1212
// LoadJSON loads a [WIT] JSON file from path.
13-
// If path is "" or "-", it reads from os.Stdin.
1413
//
1514
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
1615
func LoadJSON(path string) (*Resolve, error) {
17-
r := reader(path)
18-
if r != nil {
19-
return DecodeJSON(r)
20-
}
2116
f, err := os.Open(path)
2217
if err != nil {
2318
return nil, err
@@ -28,22 +23,19 @@ func LoadJSON(path string) (*Resolve, error) {
2823

2924
// LoadWIT loads [WIT] data from path by processing it through [wasm-tools].
3025
// This will fail if wasm-tools is not in $PATH.
31-
// If path is "" or "-", it reads from os.Stdin.
3226
//
3327
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
3428
// [wasm-tools]: https://crates.io/crates/wasm-tools
3529
func LoadWIT(path string) (*Resolve, error) {
36-
r := reader(path)
37-
return loadWIT(path, r)
30+
return loadWIT(path, nil)
3831
}
3932

40-
// ParseWIT parses [WIT] data from a buffer by processing it through [wasm-tools].
33+
// DecodeWIT decodes [WIT] data from Reader r by processing it through [wasm-tools].
4134
// This will fail if wasm-tools is not in $PATH.
4235
//
4336
// [WIT]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
4437
// [wasm-tools]: https://crates.io/crates/wasm-tools
45-
func ParseWIT(buffer []byte) (*Resolve, error) {
46-
r := bytes.NewReader(buffer)
38+
func DecodeWIT(r io.Reader) (*Resolve, error) {
4739
return loadWIT("", r)
4840
}
4941

@@ -52,7 +44,7 @@ func ParseWIT(buffer []byte) (*Resolve, error) {
5244
// If the path is not "" and "-", it will be used as the input file.
5345
// Otherwise, the reader will be used as the input.
5446
func loadWIT(path string, reader io.Reader) (*Resolve, error) {
55-
if (path != "" && path != "-") && reader != nil {
47+
if path != "" && reader != nil {
5648
return nil, errors.New("cannot set both path and reader; provide only one")
5749
}
5850

@@ -64,7 +56,7 @@ func loadWIT(path string, reader io.Reader) (*Resolve, error) {
6456
var stdout, stderr bytes.Buffer
6557

6658
cmdArgs := []string{"component", "wit", "-j", "--all-features"}
67-
if path != "" && path != "-" {
59+
if path != "" {
6860
cmdArgs = append(cmdArgs, path)
6961
}
7062

@@ -80,10 +72,3 @@ func loadWIT(path string, reader io.Reader) (*Resolve, error) {
8072

8173
return DecodeJSON(&stdout)
8274
}
83-
84-
func reader(path string) io.ReadCloser {
85-
if path == "" || path == "-" {
86-
return os.Stdin
87-
}
88-
return nil
89-
}

0 commit comments

Comments
 (0)