Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 58d6a2c

Browse files
vangentzombiezen
authored andcommitted
wire: add a diff command (google/go-cloud#745)
1 parent 65eb134 commit 58d6a2c

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

cmd/wire/main.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ import (
2323
"fmt"
2424
"go/token"
2525
"go/types"
26+
"io/ioutil"
2627
"os"
2728
"reflect"
2829
"sort"
2930
"strconv"
3031
"strings"
3132

3233
"github.com/google/wire/internal/wire"
34+
"github.com/pmezard/go-difflib/difflib"
3335
"golang.org/x/tools/go/types/typeutil"
3436
)
3537

36-
const usage = "usage: wire [gen] [PKG] | wire show [...] | wire check [...]"
38+
const usage = "usage: wire [gen|diff|show|check] [...]"
3739

3840
func main() {
3941
var err error
@@ -49,6 +51,10 @@ func main() {
4951
err = check(".")
5052
case len(os.Args) > 2 && os.Args[1] == "check":
5153
err = check(os.Args[2:]...)
54+
case len(os.Args) == 2 && os.Args[1] == "diff":
55+
err = diff(".")
56+
case len(os.Args) > 2 && os.Args[1] == "diff":
57+
err = diff(os.Args[2:]...)
5258
case len(os.Args) == 2 && os.Args[1] == "gen":
5359
err = generate(".")
5460
case len(os.Args) > 2 && os.Args[1] == "gen":
@@ -108,6 +114,54 @@ func generate(pkgs ...string) error {
108114
return nil
109115
}
110116

117+
// diff runs the diff subcommand.
118+
//
119+
// Given one or more packages, diff will generate the content for the
120+
// wire_gen.go file, and output the diff against the existing file.
121+
func diff(pkgs ...string) error {
122+
wd, err := os.Getwd()
123+
if err != nil {
124+
return err
125+
}
126+
outs, errs := wire.Generate(context.Background(), wd, os.Environ(), pkgs)
127+
if len(errs) > 0 {
128+
logErrors(errs)
129+
return errors.New("generate failed")
130+
}
131+
if len(outs) == 0 {
132+
return nil
133+
}
134+
success := true
135+
for _, out := range outs {
136+
if len(out.Errs) > 0 {
137+
fmt.Fprintf(os.Stderr, "%s: generate failed\n", out.PkgPath)
138+
logErrors(out.Errs)
139+
success = false
140+
}
141+
if len(out.Content) == 0 {
142+
// No Wire output. Maybe errors, maybe no Wire directives.
143+
continue
144+
}
145+
// Assumes the current file is empty if we can't read it.
146+
cur, _ := ioutil.ReadFile(out.OutputPath)
147+
if diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
148+
A: difflib.SplitLines(string(cur)),
149+
B: difflib.SplitLines(string(out.Content)),
150+
}); err == nil {
151+
if diff != "" {
152+
fmt.Fprintf(os.Stderr, "%s: diff from %s:\n%s", out.PkgPath, out.OutputPath, diff)
153+
}
154+
} else {
155+
fmt.Fprintf(os.Stderr, "%s: failed to diff %s: %v\n", out.PkgPath, out.OutputPath, err)
156+
success = false
157+
}
158+
}
159+
if !success {
160+
return errors.New("at least one generate failure")
161+
}
162+
return nil
163+
}
164+
111165
// show runs the show subcommand.
112166
//
113167
// Given one or more packages, show will find all the provider sets

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module github.com/google/wire
22

33
require (
44
github.com/google/go-cmp v0.2.0
5+
github.com/pmezard/go-difflib v1.0.0
56
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28
67
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
22
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28 h1:vnbqcYKfOxPnXXUlBo7t+R4pVIh0wInyOSNxih1S9Dc=
46
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)