@@ -19,21 +19,32 @@ package initcmd
1919import (
2020 "context"
2121 "fmt"
22- "os"
23-
22+ "github.com/bmatcuk/doublestar/v4"
2423 "github.com/cisco-open/go-lanai/cmd/lanai-cli/cmdutils"
24+ "os"
25+ "strings"
2526)
2627
2728var defaultBinaries = map [string ]string {
28- "github.com/axw/gocov/gocov" : "v1.1.0" ,
29- "github.com/AlekSi/gocov-xml" : "v1.1.0" ,
30- "gotest.tools/gotestsum" : "v1.12.0" ,
31- "github.com/golangci/golangci-lint/cmd/golangci-lint" : "v1.64.8" ,
29+ "github.com/axw/gocov/gocov" : "v1.1.0" ,
30+ "github.com/AlekSi/gocov-xml" : "v1.1.0" ,
31+ "gotest.tools/gotestsum" : "v1.12.0" ,
32+ //"github.com/golangci/golangci-lint/cmd/golangci-lint": "v1.64.8",
33+ "github.com/golangci/golangci-lint/v2/cmd/golangci-lint" : "v2.2.2" ,
3234}
3335
34- func installBinaries (ctx context.Context ) error {
35- opts := []cmdutils.ShCmdOptions {cmdutils .ShellShowCmd (true ), cmdutils .ShellUseWorkingDir (), cmdutils .ShellStdOut (os .Stdout )}
36+ var exclusiveBinaries = [][]string {
37+ {"github.com/golangci/golangci-lint/**" },
38+ }
3639
40+ func resolveBinariesFromCache (ctx context.Context ) (map [string ]string , error ) {
41+ if binaries , ok := Module .Data [kDataBinaries ].(map [string ]string ); ok {
42+ return binaries , nil
43+ }
44+ return resolveBinaries (ctx )
45+ }
46+
47+ func resolveBinaries (_ context.Context ) (map [string ]string , error ) {
3748 binaries := make (map [string ]string )
3849
3950 for p , v := range defaultBinaries {
@@ -53,11 +64,57 @@ func installBinaries(ctx context.Context) error {
5364 }
5465 }
5566
67+ if conflicts := findConflictingBinaries (binaries ); len (conflicts ) != 0 {
68+ for _ , conflict := range conflicts {
69+ logger .Errorf ("Detected conflicting binaries in Module.yml. Following tools should not be installed together:\n - %s" , strings .Join (conflict , "\n " ))
70+ }
71+ return nil , fmt .Errorf ("detected %d group(s) of conflicting binaries. please check Module.yml" , len (conflicts ))
72+ }
73+ return binaries , nil
74+ }
75+
76+ func installBinaries (ctx context.Context ) error {
77+
78+ opts := []cmdutils.ShCmdOptions {cmdutils .ShellShowCmd (true ), cmdutils .ShellUseWorkingDir (), cmdutils .ShellStdOut (os .Stdout )}
79+
80+ binaries , e := resolveBinariesFromCache (ctx )
81+ if e != nil {
82+ return e
83+ }
84+
5685 for p , v := range binaries {
5786 installCmd := fmt .Sprintf ("go install %s@%s" , p , v )
5887 opts = append (opts , cmdutils .ShellCmd (installCmd ))
5988 }
6089
61- _ , e : = cmdutils .RunShellCommands (ctx , opts ... )
90+ _ , e = cmdutils .RunShellCommands (ctx , opts ... )
6291 return e
6392}
93+
94+ func findConflictingBinaries (binaries map [string ]string ) [][]string {
95+ var conflicts [][]string
96+ for _ , patterns := range exclusiveBinaries {
97+ found := make ([]string , 0 , 2 )
98+ for pkg := range binaries {
99+ if patternsMatchString (patterns , pkg ) {
100+ found = append (found , pkg )
101+ }
102+ }
103+ if len (found ) > 1 {
104+ conflicts = append (conflicts , found )
105+ }
106+ }
107+ if len (conflicts ) > 0 {
108+ return conflicts
109+ }
110+ return nil
111+ }
112+
113+ func patternsMatchString (patterns []string , str string ) bool {
114+ for _ , pattern := range patterns {
115+ if ok , e := doublestar .Match (pattern , str ); e == nil && ok {
116+ return true
117+ }
118+ }
119+ return false
120+ }
0 commit comments