wire: add a diff command (google/go-cloud#745)
This commit is contained in:
committed by
Ross Light
parent
65eb134857
commit
58d6a2c2c8
@@ -23,6 +23,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -30,10 +31,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/wire/internal/wire"
|
"github.com/google/wire/internal/wire"
|
||||||
|
"github.com/pmezard/go-difflib/difflib"
|
||||||
"golang.org/x/tools/go/types/typeutil"
|
"golang.org/x/tools/go/types/typeutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = "usage: wire [gen] [PKG] | wire show [...] | wire check [...]"
|
const usage = "usage: wire [gen|diff|show|check] [...]"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
@@ -49,6 +51,10 @@ func main() {
|
|||||||
err = check(".")
|
err = check(".")
|
||||||
case len(os.Args) > 2 && os.Args[1] == "check":
|
case len(os.Args) > 2 && os.Args[1] == "check":
|
||||||
err = check(os.Args[2:]...)
|
err = check(os.Args[2:]...)
|
||||||
|
case len(os.Args) == 2 && os.Args[1] == "diff":
|
||||||
|
err = diff(".")
|
||||||
|
case len(os.Args) > 2 && os.Args[1] == "diff":
|
||||||
|
err = diff(os.Args[2:]...)
|
||||||
case len(os.Args) == 2 && os.Args[1] == "gen":
|
case len(os.Args) == 2 && os.Args[1] == "gen":
|
||||||
err = generate(".")
|
err = generate(".")
|
||||||
case len(os.Args) > 2 && os.Args[1] == "gen":
|
case len(os.Args) > 2 && os.Args[1] == "gen":
|
||||||
@@ -108,6 +114,54 @@ func generate(pkgs ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// diff runs the diff subcommand.
|
||||||
|
//
|
||||||
|
// Given one or more packages, diff will generate the content for the
|
||||||
|
// wire_gen.go file, and output the diff against the existing file.
|
||||||
|
func diff(pkgs ...string) error {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
outs, errs := wire.Generate(context.Background(), wd, os.Environ(), pkgs)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
logErrors(errs)
|
||||||
|
return errors.New("generate failed")
|
||||||
|
}
|
||||||
|
if len(outs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
success := true
|
||||||
|
for _, out := range outs {
|
||||||
|
if len(out.Errs) > 0 {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s: generate failed\n", out.PkgPath)
|
||||||
|
logErrors(out.Errs)
|
||||||
|
success = false
|
||||||
|
}
|
||||||
|
if len(out.Content) == 0 {
|
||||||
|
// No Wire output. Maybe errors, maybe no Wire directives.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Assumes the current file is empty if we can't read it.
|
||||||
|
cur, _ := ioutil.ReadFile(out.OutputPath)
|
||||||
|
if diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
|
||||||
|
A: difflib.SplitLines(string(cur)),
|
||||||
|
B: difflib.SplitLines(string(out.Content)),
|
||||||
|
}); err == nil {
|
||||||
|
if diff != "" {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s: diff from %s:\n%s", out.PkgPath, out.OutputPath, diff)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s: failed to diff %s: %v\n", out.PkgPath, out.OutputPath, err)
|
||||||
|
success = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !success {
|
||||||
|
return errors.New("at least one generate failure")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// show runs the show subcommand.
|
// show runs the show subcommand.
|
||||||
//
|
//
|
||||||
// Given one or more packages, show will find all the provider sets
|
// Given one or more packages, show will find all the provider sets
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -2,5 +2,6 @@ module github.com/google/wire
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/go-cmp v0.2.0
|
github.com/google/go-cmp v0.2.0
|
||||||
|
github.com/pmezard/go-difflib v1.0.0
|
||||||
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28
|
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,4 +1,6 @@
|
|||||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28 h1:vnbqcYKfOxPnXXUlBo7t+R4pVIh0wInyOSNxih1S9Dc=
|
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28 h1:vnbqcYKfOxPnXXUlBo7t+R4pVIh0wInyOSNxih1S9Dc=
|
||||||
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20181017214349-06f26fdaaa28/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
|||||||
Reference in New Issue
Block a user