Skip to content

Commit 8536df2

Browse files
authored
Merge pull request #220 from bytecodealliance/ydnar/verbose
cmd/wit-bindgen-go, wit/logging: add logging package, verbose (-v) and debug (-vv) flags
2 parents 8db9ae5 + cf12889 commit 8536df2

File tree

9 files changed

+295
-57
lines changed

9 files changed

+295
-57
lines changed

CHANGELOG.md

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

55
## [Unreleased]
66

7+
### Added
8+
9+
- `wit-bindgen-go` now accepts arguments that control the level of logging output on stderr. Verbose mode (`-v` or `--verbose`) will print informational log lines, warnings, and errors. Debug mode (`-vv` or `--debug`) will emit finer-grained debugging information. By default, `wit-bindgen-go` will print warnings or errors.
10+
711
### Fixed
812

913
- [#215](https://github.com/bytecodealliance/wasm-tools-go/issues/215): generate variant accessor methods with the correct scope for Go names.
1014

15+
### New Contributors
16+
17+
- [@vados-cosmonic](https://github.com/vados-cosmonic): [#212](https://github.com/bytecodealliance/wasm-tools-go/issues/212) and [#214](https://github.com/bytecodealliance/wasm-tools-go/pull/214) — verbose logging controls in `wit-bindgen-go`.
18+
1119
## [v0.3.0] — 2024-10-11
1220

1321
### Added

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/bytecodealliance/wasm-tools-go/internal/go/gen"
1212
"github.com/bytecodealliance/wasm-tools-go/internal/witcli"
1313
"github.com/bytecodealliance/wasm-tools-go/wit/bindgen"
14+
"github.com/bytecodealliance/wasm-tools-go/wit/logging"
1415
"github.com/urfave/cli/v3"
1516
)
1617

@@ -66,6 +67,7 @@ var Command = &cli.Command{
6667

6768
// Config is the configuration for the `generate` command.
6869
type config struct {
70+
logger logging.Logger
6971
dryRun bool
7072
out string
7173
outPerm os.FileMode
@@ -90,6 +92,7 @@ func action(ctx context.Context, cmd *cli.Command) error {
9092

9193
packages, err := bindgen.Go(res,
9294
bindgen.GeneratedBy(cmd.Root().Name),
95+
bindgen.Logger(cfg.logger),
9396
bindgen.World(cfg.world),
9497
bindgen.PackageRoot(cfg.pkgRoot),
9598
bindgen.Versioned(cfg.versioned),
@@ -103,14 +106,15 @@ func action(ctx context.Context, cmd *cli.Command) error {
103106
}
104107

105108
func parseFlags(cmd *cli.Command) (*config, error) {
109+
logger := witcli.Logger(cmd.Bool("verbose"), cmd.Bool("debug"))
106110
dryRun := cmd.Bool("dry-run")
107111
out := cmd.String("out")
108112

109113
info, err := witcli.FindOrCreateDir(out)
110114
if err != nil {
111115
return nil, err
112116
}
113-
fmt.Fprintf(os.Stderr, "Output dir: %s\n", out)
117+
logger.Infof("Output dir: %s\n", out)
114118
outPerm := info.Mode().Perm()
115119

116120
pkgRoot := cmd.String("package-root")
@@ -120,14 +124,15 @@ func parseFlags(cmd *cli.Command) (*config, error) {
120124
return nil, err
121125
}
122126
}
123-
fmt.Fprintf(os.Stderr, "Package root: %s\n", pkgRoot)
127+
logger.Infof("Package root: %s\n", pkgRoot)
124128

125129
path, err := witcli.LoadPath(cmd.Args().Slice()...)
126130
if err != nil {
127131
return nil, err
128132
}
129133

130134
return &config{
135+
logger,
131136
dryRun,
132137
out,
133138
outPerm,
@@ -141,21 +146,21 @@ func parseFlags(cmd *cli.Command) (*config, error) {
141146
}
142147

143148
func writeGoPackages(packages []*gen.Package, cfg *config) error {
144-
fmt.Fprintf(os.Stderr, "Generated %d package(s)\n", len(packages))
149+
cfg.logger.Infof("Generated %d Go package(s)\n", len(packages))
145150
for _, pkg := range packages {
146151
if !pkg.HasContent() {
147-
fmt.Fprintf(os.Stderr, "Skipping empty package: %s\n", pkg.Path)
152+
cfg.logger.Debugf("Skipped empty package: %s\n", pkg.Path)
148153
continue
149154
}
150-
fmt.Fprintf(os.Stderr, "Generated package: %s\n", pkg.Path)
155+
cfg.logger.Infof("Generated package: %s\n", pkg.Path)
151156

152157
for _, filename := range codec.SortedKeys(pkg.Files) {
153158
file := pkg.Files[filename]
154159
dir := filepath.Join(cfg.out, strings.TrimPrefix(file.Package.Path, cfg.pkgRoot))
155160
path := filepath.Join(dir, file.Name)
156161

157162
if !file.HasContent() {
158-
fmt.Fprintf(os.Stderr, "Skipping empty file: %s\n", path)
163+
cfg.logger.Debugf("\tSkipping empty file: %s\n", path)
159164
continue
160165
}
161166

@@ -168,9 +173,9 @@ func writeGoPackages(packages []*gen.Package, cfg *config) error {
168173
if content == nil {
169174
return err
170175
}
171-
fmt.Fprintf(os.Stderr, "Error formatting file: %v\n", err)
176+
cfg.logger.Errorf("\tError formatting file: %v\n", err)
172177
} else {
173-
fmt.Fprintf(os.Stderr, "Generated file: %s\n", path)
178+
cfg.logger.Infof("\t%s\n", path)
174179
}
175180

176181
if cfg.dryRun {

cmd/wit-bindgen-go/main.go

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,67 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"runtime/debug"
87

98
"github.com/urfave/cli/v3"
109

1110
"github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go/cmd/generate"
1211
"github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go/cmd/wit"
12+
"github.com/bytecodealliance/wasm-tools-go/internal/witcli"
1313
)
1414

15-
var (
16-
version = ""
17-
revision = ""
18-
versionString = ""
19-
)
20-
21-
func init() {
22-
build, ok := debug.ReadBuildInfo()
23-
if !ok {
24-
return
25-
}
26-
version = build.Main.Version
27-
for _, s := range build.Settings {
28-
switch s.Key {
29-
case "vcs.revision":
30-
revision = s.Value
31-
}
32-
}
33-
if version == "" {
34-
version = "(none)"
35-
}
36-
versionString = version
37-
if revision != "" {
38-
versionString += " (" + revision + ")"
15+
func main() {
16+
err := Command.Run(context.Background(), os.Args)
17+
if err != nil {
18+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
19+
os.Exit(1)
3920
}
4021
}
4122

42-
func main() {
43-
cmd := &cli.Command{
44-
Name: "wit-bindgen-go",
45-
Usage: "inspect or manipulate WebAssembly Interface Types for Go",
46-
Commands: []*cli.Command{
47-
generate.Command,
48-
wit.Command,
23+
var Command = &cli.Command{
24+
Name: "wit-bindgen-go",
25+
Usage: "inspect or manipulate WebAssembly Interface Types for Go",
26+
Commands: []*cli.Command{
27+
generate.Command,
28+
wit.Command,
29+
version,
30+
},
31+
Flags: []cli.Flag{
32+
&cli.BoolFlag{
33+
Name: "version",
34+
Usage: "print the version",
35+
HideDefault: true,
36+
Local: true,
4937
},
50-
Flags: []cli.Flag{
51-
&cli.BoolFlag{
52-
Name: "force-wit",
53-
Usage: "force loading WIT via wasm-tools",
54-
},
38+
&cli.BoolFlag{
39+
Name: "force-wit",
40+
Usage: "force loading WIT via wasm-tools",
5541
},
56-
Version: versionString,
57-
}
42+
&cli.BoolFlag{
43+
Name: "verbose",
44+
Aliases: []string{"v"},
45+
Usage: "print verbose logging messages",
46+
},
47+
&cli.BoolFlag{
48+
Name: "debug",
49+
Aliases: []string{"vv"},
50+
Usage: "print debug logging messages",
51+
},
52+
},
53+
Action: action,
54+
}
5855

59-
err := cmd.Run(context.Background(), os.Args)
60-
if err != nil {
61-
fmt.Fprintf(os.Stderr, "error: %v\n", err)
62-
os.Exit(1)
56+
func action(ctx context.Context, cmd *cli.Command) error {
57+
if cmd.Bool("version") {
58+
return version.Run(ctx, nil)
6359
}
60+
return cli.ShowAppHelp(cmd)
61+
}
62+
63+
var version = &cli.Command{
64+
Name: "version",
65+
Usage: "print the version",
66+
Action: func(ctx context.Context, cmd *cli.Command) error {
67+
fmt.Printf("%s version %s\n", cmd.Root().Name, witcli.Version())
68+
return nil
69+
},
6470
}

cmd/wit-bindgen-go/main_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"testing"
7+
)
8+
9+
// TestSimpleGenVerbosity ensures that a basic generation case honors the verbose flag
10+
func TestSimpleGenVerbosity(t *testing.T) {
11+
var stdout bytes.Buffer
12+
var stderr bytes.Buffer
13+
cmd := Command
14+
cmd.Writer = &stdout
15+
cmd.ErrWriter = &stderr
16+
17+
args := []string{
18+
"wit-bindgen-go",
19+
"generate",
20+
"--world",
21+
"http-fetch-simple",
22+
"--dry-run",
23+
"../../testdata/codegen/simple-http.wit",
24+
}
25+
26+
// Run the app without verbose
27+
cmd.Run(context.Background(), args)
28+
if stderr.Len() != 0 {
29+
t.Errorf("output was written to stderr despite lack of --verbose")
30+
}
31+
32+
stdout.Reset()
33+
stderr.Reset()
34+
35+
cmd.Run(context.Background(), append(args, "-v"))
36+
if stderr.Len() == 0 {
37+
t.Errorf("no output was written to stderr when -v was used")
38+
}
39+
40+
cmd.Run(context.Background(), append(args, "-vv"))
41+
if stderr.Len() == 0 {
42+
t.Errorf("no output was written to stderr when -vv was used")
43+
}
44+
45+
cmd.Run(context.Background(), append(args, "--verbose"))
46+
if stderr.Len() == 0 {
47+
t.Errorf("no output was written to stderr when --verbose was used")
48+
}
49+
}

internal/witcli/logger.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package witcli
2+
3+
import (
4+
"os"
5+
6+
"github.com/bytecodealliance/wasm-tools-go/wit/logging"
7+
)
8+
9+
// Logger returns a [logging.Logger] that outputs to stdout.
10+
func Logger(verbose, debug bool) logging.Logger {
11+
level := logging.LevelWarn
12+
if debug {
13+
level = logging.LevelDebug
14+
} else if verbose {
15+
level = logging.LevelInfo
16+
}
17+
return logging.NewLogger(os.Stderr, level)
18+
}

internal/witcli/version.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package witcli
2+
3+
import (
4+
"runtime/debug"
5+
"sync"
6+
)
7+
8+
// Version returns the version string of this module.
9+
func Version() string {
10+
return versionString()
11+
}
12+
13+
var versionString = sync.OnceValue(func() string {
14+
build, ok := debug.ReadBuildInfo()
15+
if !ok {
16+
return "(none)"
17+
}
18+
version := build.Main.Version
19+
var revision string
20+
for _, s := range build.Settings {
21+
switch s.Key {
22+
case "vcs.revision":
23+
revision = s.Value
24+
}
25+
}
26+
if version == "" {
27+
version = "(none)"
28+
}
29+
versionString := version
30+
if revision != "" {
31+
versionString += " (" + revision + ")"
32+
}
33+
return versionString
34+
})

0 commit comments

Comments
 (0)