-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from stevenh/feat/indirect-packages
fix(check-plugin): format output for indirect dependencies
- Loading branch information
Showing
8 changed files
with
191 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,90 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/krakendio/krakend-cobra/v2/plugin" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
func pluginFunc(cmd *cobra.Command, _ []string) { | ||
// indirectRequires returns the indirect dependencies of the go.sum file. | ||
func indirectRequires(goSum string) (map[string]struct{}, error) { | ||
dir := filepath.Dir(goSum) | ||
filename := filepath.Join(dir, "go.mod") | ||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("read go.mod: %w", err) | ||
} | ||
|
||
f, err := modfile.Parse(filename, data, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse go.mod: %w", err) | ||
} | ||
|
||
indirects := map[string]struct{}{} | ||
for _, r := range f.Require { | ||
if r.Indirect { | ||
indirects[r.Mod.Path] = struct{}{} | ||
} | ||
} | ||
|
||
return indirects, nil | ||
} | ||
|
||
// getBuildInfo returns the dependencies of the binary calling it. | ||
// It is a var to allow the replacement of the function in the tests | ||
// as the debug.ReadBuildInfo function is not available in the tests | ||
// https://github.com/golang/go/issues/68045 | ||
var localDescriber = plugin.Local | ||
|
||
func pluginFunc(cmd *cobra.Command, _ []string) error { | ||
f, err := os.Open(goSum) | ||
if err != nil { | ||
cmd.Println(err) | ||
os.Exit(1) | ||
return | ||
return err | ||
} | ||
|
||
defer func() { _ = f.Close() }() // Workaround false positive for GO-S2307. | ||
|
||
desc, err := plugin.Describe(f, goVersion, libcVersion) | ||
if err != nil { | ||
cmd.Println(err) | ||
f.Close() | ||
os.Exit(1) | ||
return | ||
return err | ||
} | ||
|
||
diffs := plugin.Local().Compare(desc) | ||
diffs := localDescriber().Compare(desc) | ||
if len(diffs) == 0 { | ||
cmd.Println("No incompatibilities found!") | ||
f.Close() | ||
return | ||
return nil | ||
} | ||
|
||
cmd.Println(len(diffs), "incompatibility(ies) found...") | ||
if gogetEnabled { | ||
indirects, err := indirectRequires(goSum) | ||
if err != nil { | ||
return err | ||
} | ||
for _, diff := range diffs { | ||
if diff.Name != "go" && diff.Name != "libc" { | ||
cmd.Printf("go get %s@%s\n", diff.Name, diff.Expected) | ||
if _, ok := indirects[diff.Name]; ok { | ||
cmd.Printf("go mod edit --replace %s=%s@%s\n", diff.Name, diff.Name, diff.Expected) | ||
} else { | ||
cmd.Printf("go get %s@%s\n", diff.Name, diff.Expected) | ||
} | ||
continue | ||
} | ||
|
||
cmd.Println(diff.Name) | ||
cmd.Println("\thave:", diff.Have) | ||
cmd.Println("\twant:", diff.Expected) | ||
} | ||
f.Close() | ||
os.Exit(1) | ||
} else { | ||
for _, diff := range diffs { | ||
cmd.Println(diff.Name) | ||
cmd.Println("\thave:", diff.Have) | ||
cmd.Println("\twant:", diff.Expected) | ||
} | ||
} | ||
|
||
for _, diff := range diffs { | ||
cmd.Println(diff.Name) | ||
cmd.Println("\thave:", diff.Have) | ||
cmd.Println("\twant:", diff.Expected) | ||
} | ||
f.Close() | ||
os.Exit(1) | ||
return fmt.Errorf("%d incompatibilities found", len(diffs)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/krakendio/krakend-cobra/v2/plugin" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_pluginFunc(t *testing.T) { | ||
var buf bytes.Buffer | ||
cmd := &cobra.Command{} | ||
cmd.SetOutput(&buf) | ||
|
||
localDescriber = func() plugin.Descriptor { | ||
return plugin.Descriptor{ | ||
Go: goVersion, | ||
Libc: libcVersion, | ||
Deps: map[string]string{ | ||
"golang.org/x/mod": "v0.6.0-dev.0.20220419223038-86c51ed26bb4", | ||
"github.com/Azure/azure-sdk-for-go": "v59.3.0+incompatible", | ||
"cloud.google.com/go": "v0.100.2", | ||
}, | ||
} | ||
} | ||
|
||
defer func() { localDescriber = plugin.Local }() | ||
|
||
tests := map[string]struct { | ||
goSum string | ||
expected string | ||
fix bool | ||
err string | ||
}{ | ||
"missing": { | ||
goSum: "./testdata/missing-go.sum", | ||
err: "open ./testdata/missing-go.sum: no such file or directory", | ||
}, | ||
"matching": { | ||
goSum: "./testdata/match-go.sum", | ||
expected: "No incompatibilities found!\n", | ||
}, | ||
"changes": { | ||
goSum: "./testdata/changes-go.sum", | ||
expected: `cloud.google.com/go | ||
have: v0.100.3 | ||
want: v0.100.2 | ||
github.com/Azure/azure-sdk-for-go | ||
have: v59.3.1+incompatible | ||
want: v59.3.0+incompatible | ||
golang.org/x/mod | ||
have: v0.6.10-dev.0.20220419223038-86c51ed26bb4 | ||
want: v0.6.0-dev.0.20220419223038-86c51ed26bb4 | ||
`, | ||
err: "3 incompatibilities found", | ||
}, | ||
"fix": { | ||
goSum: "./testdata/changes-go.sum", | ||
fix: true, | ||
expected: `go mod edit --replace cloud.google.com/go=cloud.google.com/[email protected] | ||
go mod edit --replace github.com/Azure/azure-sdk-for-go=github.com/Azure/[email protected]+incompatible | ||
go get golang.org/x/[email protected] | ||
`, | ||
err: "3 incompatibilities found", | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
buf.Reset() | ||
|
||
orig := goSum | ||
goSum = tc.goSum | ||
defer func() { goSum = orig }() | ||
|
||
fix := gogetEnabled | ||
gogetEnabled = tc.fix | ||
defer func() { gogetEnabled = fix }() | ||
|
||
err := pluginFunc(cmd, nil) | ||
if tc.err != "" { | ||
require.EqualError(t, err, tc.err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, tc.expected, buf.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Udc= | ||
cloud.google.com/go v0.99.0/go.mod h1:w0Xx2nLzqWJPuozYQX+hFfCSI8WioryfRDzkoI/Y2ZA= | ||
cloud.google.com/go v0.100.1/go.mod h1:fs4QogzfH5n2pBXBP9vRiU+eCny7lD2vmFZy79Iuw1U= | ||
cloud.google.com/go v0.100.3 h1:t9Iw5QH5v4XtlEQaCtUY7x6sCABps8sW0acw7e2WQ6Y= | ||
cloud.google.com/go v0.100.3/go.mod h1:4Xra9TjzAeYHrl5+oeLlzbM2k3mjVhZh4UqTZ//w99A= | ||
github.com/Azure/azure-sdk-for-go v51.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= | ||
github.com/Azure/azure-sdk-for-go v59.3.1+incompatible h1:dPIm0BO4jsMXFcCI/sLTPkBtE7mk8WMuRHA0JeWhlcQ= | ||
github.com/Azure/azure-sdk-for-go v59.3.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= | ||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= | ||
golang.org/x/mod v0.6.10-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= | ||
golang.org/x/mod v0.6.10-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/krakendio/krakend-cobra/v2 | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/gin-gonic/gin v1.8.2 | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 | ||
) | ||
|
||
require ( | ||
cloud.google.com/go v0.100.2 // indirect | ||
github.com/Azure/azure-sdk-for-go v59.3.0+incompatible // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Udc= | ||
cloud.google.com/go v0.99.0/go.mod h1:w0Xx2nLzqWJPuozYQX+hFfCSI8WioryfRDzkoI/Y2ZA= | ||
cloud.google.com/go v0.100.1/go.mod h1:fs4QogzfH5n2pBXBP9vRiU+eCny7lD2vmFZy79Iuw1U= | ||
cloud.google.com/go v0.100.2 h1:t9Iw5QH5v4XtlEQaCtUY7x6sCABps8sW0acw7e2WQ6Y= | ||
cloud.google.com/go v0.100.2/go.mod h1:4Xra9TjzAeYHrl5+oeLlzbM2k3mjVhZh4UqTZ//w99A= | ||
github.com/Azure/azure-sdk-for-go v51.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= | ||
github.com/Azure/azure-sdk-for-go v59.3.0+incompatible h1:dPIm0BO4jsMXFcCI/sLTPkBtE7mk8WMuRHA0JeWhlcQ= | ||
github.com/Azure/azure-sdk-for-go v59.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= | ||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= |