Skip to content

Commit 5de2ee2

Browse files
committed
cmd/wit-bindgen-go, wit, internal/witcli: move handling of - to mean stdin to package witcli
1 parent bdaacf2 commit 5de2ee2

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/urfave/cli/v3"
11+
1112
"go.bytecodealliance.org/internal/codec"
1213
"go.bytecodealliance.org/internal/go/gen"
1314
"go.bytecodealliance.org/internal/witcli"
@@ -85,7 +86,7 @@ func action(ctx context.Context, cmd *cli.Command) error {
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
}

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
}

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)