|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "cmp" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "regexp" |
| 23 | + "runtime/debug" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/golangci/golangci-lint/v2/pkg/commands" |
| 27 | + "github.com/golangci/golangci-lint/v2/pkg/exitcodes" |
| 28 | +) |
| 29 | + |
| 30 | +//nolint:gochecknoglobals |
| 31 | +var ( |
| 32 | + goVersion = "unknown" |
| 33 | + |
| 34 | + // Populated by goreleaser during build. |
| 35 | + version = "unknown" |
| 36 | + commit = "?" |
| 37 | + date = "" |
| 38 | +) |
| 39 | + |
| 40 | +func main() { |
| 41 | + info := createBuildInfo() |
| 42 | + |
| 43 | + // set a KAL specific build version. |
| 44 | + setKALBuildVersion(&info) |
| 45 | + |
| 46 | + if err := commands.Execute(info); err != nil { |
| 47 | + _, _ = fmt.Fprintf(os.Stderr, "Failed executing command with error: %v\n", err) |
| 48 | + os.Exit(exitcodes.Failure) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func createBuildInfo() commands.BuildInfo { |
| 53 | + info := commands.BuildInfo{ |
| 54 | + Commit: commit, |
| 55 | + Version: version, |
| 56 | + GoVersion: goVersion, |
| 57 | + Date: date, |
| 58 | + } |
| 59 | + |
| 60 | + buildInfo, available := debug.ReadBuildInfo() |
| 61 | + if !available { |
| 62 | + return info |
| 63 | + } |
| 64 | + |
| 65 | + info.GoVersion = buildInfo.GoVersion |
| 66 | + |
| 67 | + if date != "" { |
| 68 | + return info |
| 69 | + } |
| 70 | + |
| 71 | + info.Version = buildInfo.Main.Version |
| 72 | + |
| 73 | + matched, _ := regexp.MatchString(`v\d+\.\d+\.\d+`, buildInfo.Main.Version) |
| 74 | + if matched { |
| 75 | + info.Version = strings.TrimPrefix(buildInfo.Main.Version, "v") |
| 76 | + } |
| 77 | + |
| 78 | + var revision, modified string |
| 79 | + |
| 80 | + for _, setting := range buildInfo.Settings { |
| 81 | + // The `vcs.xxx` information is only available with `go build`. |
| 82 | + // This information is not available with `go install` or `go run`. |
| 83 | + switch setting.Key { |
| 84 | + case "vcs.time": |
| 85 | + info.Date = setting.Value |
| 86 | + case "vcs.revision": |
| 87 | + revision = setting.Value |
| 88 | + case "vcs.modified": |
| 89 | + modified = setting.Value |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + info.Date = cmp.Or(info.Date, "(unknown)") |
| 94 | + |
| 95 | + info.Commit = fmt.Sprintf("(%s, modified: %s, mod sum: %q)", |
| 96 | + cmp.Or(revision, "unknown"), cmp.Or(modified, "?"), buildInfo.Main.Sum) |
| 97 | + |
| 98 | + return info |
| 99 | +} |
| 100 | + |
| 101 | +// Import path of golangci-lint. |
| 102 | +const golangciPath = "github.com/golangci/golangci-lint/v2" |
| 103 | + |
| 104 | +// setKALBuildVersion sets a KAL specific build version message. |
| 105 | +// It fetches the golangci-lint version from the build dependencies. |
| 106 | +// Then uses the local version for KAL. |
| 107 | +func setKALBuildVersion(info *commands.BuildInfo) { |
| 108 | + golangciVersion := "unknown" |
| 109 | + |
| 110 | + buildInfo, available := debug.ReadBuildInfo() |
| 111 | + if available { |
| 112 | + for _, dep := range buildInfo.Deps { |
| 113 | + if dep.Path == golangciPath { |
| 114 | + golangciVersion = dep.Version |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + matched, _ := regexp.MatchString(`v\d+\.\d+\.\d+`, golangciVersion) |
| 121 | + if matched { |
| 122 | + golangciVersion = strings.TrimPrefix(golangciVersion, "v") |
| 123 | + } |
| 124 | + |
| 125 | + info.Version = fmt.Sprintf("%s, kube-api-linter has version %s", golangciVersion, info.Version) |
| 126 | +} |
0 commit comments