Skip to content

Commit f3039cf

Browse files
authored
Add custom version flag (#189)
* Add custom version flag * Add scripts and workflows to check version * Fix `revive` error
1 parent a03bee6 commit f3039cf

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

.github/scripts/check-version.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is run as a pre-push hook to avoid pushing tags that does not
4+
# match the version defined in the code.
5+
#
6+
# This is the pre-push script in .git/hooks/pre-push:
7+
#
8+
# #!/bin/bash
9+
# set -euo pipefail
10+
#
11+
# # Capture pushed refs from stdin
12+
# while read -r _ _ remote_ref _; do
13+
# if [[ "$remote_ref" =~ refs/tags/(v[0-9]+\.[0-9]+\.[0-9]+) ]]; then
14+
# tag="${BASH_REMATCH[1]}"
15+
#
16+
# echo "🔍 Checking version for tag: $tag"
17+
# .github/scripts/check-version.sh "$tag"
18+
# fi
19+
# done
20+
21+
set -euo pipefail
22+
23+
TAG=${1:-}
24+
25+
if [[ -z "$TAG" ]]; then
26+
echo "Error: No tag provided."
27+
exit 1
28+
fi
29+
30+
# Look for the tag string in the file that defines the version
31+
if ! grep -q "wsl version $TAG" ./*.go; then
32+
echo "❌ Version constant does not match tag: $TAG"
33+
exit 1
34+
fi
35+
36+
echo "✅ Version constant matches tag: $TAG"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
name: Check version constant matches tag
3+
4+
on:
5+
push:
6+
tags:
7+
- "v*" # Trigger only on tag pushes
8+
9+
jobs:
10+
version-check:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Check version constant
18+
run: .github/scripts/check-version.sh "${GITHUB_REF#refs/tags/}"

analyzer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package wsl
22

33
import (
44
"flag"
5+
"fmt"
56
"go/ast"
67
"go/token"
8+
"os"
79
"strings"
810
"sync"
911

1012
"golang.org/x/tools/go/analysis"
1113
)
1214

15+
const version = "wsl version v5.1.0"
16+
1317
func NewAnalyzer(config *Configuration) *analysis.Analyzer {
1418
wa := &wslAnalyzer{config: config}
1519

@@ -64,6 +68,7 @@ func (wa *wslAnalyzer) flags() flag.FlagSet {
6468
flags.StringVar(&wa.defaultChecks, "default", "", "Can be 'all' for all checks or 'none' for no checks or empty for default checks")
6569
flags.Var(&multiStringValue{slicePtr: &wa.enable}, "enable", "Comma separated list of checks to enable")
6670
flags.Var(&multiStringValue{slicePtr: &wa.disable}, "disable", "Comma separated list of checks to disable")
71+
flags.Var(new(versionFlag), "V", "print version and exit")
6772

6873
return *flags
6974
}
@@ -169,6 +174,20 @@ func (m *multiStringValue) String() string {
169174
return strings.Join(*m.slicePtr, ", ")
170175
}
171176

177+
// https://cs.opensource.google/go/x/tools/+/refs/tags/v0.35.0:go/analysis/internal/analysisflags/flags.go;l=188-237;drc=99337ebe7b90918701a41932abf121600b972e34
178+
type versionFlag string
179+
180+
func (*versionFlag) IsBoolFlag() bool { return true }
181+
func (*versionFlag) Get() any { return nil }
182+
func (*versionFlag) String() string { return "" }
183+
184+
func (*versionFlag) Set(_ string) error {
185+
fmt.Println(version)
186+
os.Exit(0)
187+
188+
return nil
189+
}
190+
172191
func getFilename(fset *token.FileSet, file *ast.File) string {
173192
filename := fset.PositionFor(file.Pos(), true).Filename
174193
if !strings.HasSuffix(filename, ".go") {

0 commit comments

Comments
 (0)