Skip to content

Commit

Permalink
cmd/wit-bindgen-wrpc: adds -v flag for enabling stderr output
Browse files Browse the repository at this point in the history
This commit adds a `--verbose`/`-v` for enable output to stderr for
the `wit-bindgen-wrpc` binary

Fixes #214

Signed-off-by: Victor Adossi <[email protected]>
  • Loading branch information
vados-cosmonic committed Oct 25, 2024
1 parent c7ba768 commit 43a8b7f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/wit-bindgen-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wit-bindgen-go
24 changes: 18 additions & 6 deletions cmd/wit-bindgen-go/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generate
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -75,6 +76,7 @@ type config struct {
versioned bool
forceWIT bool
path string
verbose bool
}

func action(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -140,25 +142,35 @@ func parseFlags(cmd *cli.Command) (*config, error) {
cmd.Bool("versioned"),
cmd.Bool("force-wit"),
path,
cmd.Bool("verbose"),
}, nil
}

// Generate and write the specified Go packages w/ provided bindgen config
//
// Parameters:
// - packages: A slice of pointers to `gen.Package`s to generate
// - cfg: A pointer to a `config` struct
//
// Returns:
// - error: An error if the package writing process fails, or nil if all
// packages are written successfully.
func writeGoPackages(packages []*gen.Package, cfg *config) error {
fmt.Fprintf(os.Stderr, "Generated %d package(s)\n", len(packages))
log.Printf("Generated %d package(s)\n", len(packages))
for _, pkg := range packages {
if !pkg.HasContent() {
fmt.Fprintf(os.Stderr, "Skipping empty package: %s\n", pkg.Path)
log.Printf("Skipping empty package: %s\n", pkg.Path)
continue
}
fmt.Fprintf(os.Stderr, "Generated package: %s\n", pkg.Path)
log.Printf("Generated package: %s\n", pkg.Path)

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

if !file.HasContent() {
fmt.Fprintf(os.Stderr, "Skipping empty file: %s\n", path)
log.Printf("Skipping empty file: %s\n", path)
continue
}

Expand All @@ -171,9 +183,9 @@ func writeGoPackages(packages []*gen.Package, cfg *config) error {
if content == nil {
return err
}
fmt.Fprintf(os.Stderr, "Error formatting file: %v\n", err)
log.Printf("Error formatting file: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "Generated file: %s\n", path)
log.Printf("Generated file: %s\n", path)
}

if cfg.dryRun {
Expand Down
19 changes: 17 additions & 2 deletions cmd/wit-bindgen-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"os"
"log"
"io"
"runtime/debug"

"github.com/urfave/cli/v3"
Expand Down Expand Up @@ -39,8 +41,9 @@ func init() {
}
}

func main() {
cmd := &cli.Command{
// Generate the app
func makeCmd() cli.Command {
cmd := cli.Command{
Name: "wit-bindgen-go",
Usage: "inspect or manipulate WebAssembly Interface Types for Go",
Commands: []*cli.Command{
Expand All @@ -55,6 +58,18 @@ func main() {
},
Version: versionString,
}
return cmd
}

func main() {
cmd := makeCmd()

// If verbose was specified, use set global logger appropriately
if cmd.Bool("verbose") {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(io.Discard)
}

err := cmd.Run(context.Background(), os.Args)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cmd/wit-bindgen-go/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"bytes"
"context"
"testing"
)

// TestSimpleGenVerbosity ensures that a basic generation case honors the verbose flag
func TestSimpleGenVerbosity(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := makeCmd()
cmd.Writer = &stdout
cmd.ErrWriter = &stderr

args := []string{
"wit-bindgen-go",
"generate",
"--world",
"http-fetch-simple",
"--dry-run",
"../../testdata/codegen/simple-http.wit",
}

// Run the app without verbose
cmd.Run(context.Background(), args)
if stderr.Len() != 0 {
t.Errorf("output was written to stderr despite lack of --verbose")
}

stdout.Reset()
stderr.Reset()

// Run the app WITH verbose
cmd.Run(context.Background(), append(args, "--verbose"))
if stderr.Len() == 0 {
t.Errorf("no output was written to stderr when --verbose was used")
}
}

0 comments on commit 43a8b7f

Please sign in to comment.