Skip to content

Commit 8090e66

Browse files
committed
ldflags for version
1 parent 65ecf31 commit 8090e66

5 files changed

Lines changed: 47 additions & 37 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ rules-cli
55
/rules
66
test-rule.yaml
77

8-
.github
8+
.github/instructions
99
.sourcegraph
1010
.continue
1111
.clinerules

build.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
#!/usr/bin/env node
22

3-
const { execSync } = require('child_process');
4-
const fs = require('fs');
5-
const path = require('path');
6-
const os = require('os');
3+
const { execSync } = require("child_process");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const os = require("os");
77

88
// Define target platforms and architectures
99
const targets = [
10-
{ platform: 'darwin', arch: 'amd64', goarch: 'amd64' },
11-
{ platform: 'darwin', arch: 'arm64', goarch: 'arm64' },
12-
{ platform: 'linux', arch: 'x64', goarch: 'amd64' },
13-
{ platform: 'linux', arch: 'arm64', goarch: 'arm64' },
14-
{ platform: 'win32', arch: 'x64', goarch: 'amd64' }
10+
{ platform: "darwin", arch: "amd64", goarch: "amd64" },
11+
{ platform: "darwin", arch: "arm64", goarch: "arm64" },
12+
{ platform: "linux", arch: "x64", goarch: "amd64" },
13+
{ platform: "linux", arch: "arm64", goarch: "arm64" },
14+
{ platform: "win32", arch: "x64", goarch: "amd64" },
1515
];
1616

1717
// Get package version from package.json
18-
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
18+
const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8"));
1919
const version = packageJson.version;
2020

2121
// Create bin directory if it doesn't exist
22-
if (!fs.existsSync('bin')) {
23-
fs.mkdirSync('bin');
22+
if (!fs.existsSync("bin")) {
23+
fs.mkdirSync("bin");
2424
}
2525

2626
// Build for each target
2727
for (const target of targets) {
2828
const { platform, arch, goarch } = target;
29-
29+
3030
// Set binary name based on platform
31-
const binaryName = platform === 'win32' ? 'rules-cli.exe' : 'rules-cli';
32-
31+
const binaryName = platform === "win32" ? "rules-cli.exe" : "rules-cli";
32+
3333
// Create output directory
34-
const outputDir = path.join('bin', `${platform}-${arch}`);
34+
const outputDir = path.join("bin", `${platform}-${arch}`);
3535
if (!fs.existsSync(outputDir)) {
3636
fs.mkdirSync(outputDir, { recursive: true });
3737
}
38-
38+
3939
const outputPath = path.join(outputDir, binaryName);
40-
40+
4141
// Set environment variables for cross-compilation
4242
const env = {
4343
...process.env,
44-
GOOS: platform === 'win32' ? 'windows' : platform,
44+
GOOS: platform === "win32" ? "windows" : platform,
4545
GOARCH: goarch,
46-
CGO_ENABLED: '0'
46+
CGO_ENABLED: "0",
4747
};
48-
48+
4949
console.log(`Building for ${platform}-${arch}...`);
50-
50+
5151
try {
5252
// Build the binary with version info
5353
execSync(
54-
`go build -ldflags="-s -w -X main.Version=${version}" -o ${outputPath}`,
55-
{ env, stdio: 'inherit' }
54+
`go build -ldflags="-s -w -X main.Version=${version} -X rules-cli/internal/utils.Version=${version}" -o ${outputPath}`,
55+
{ env, stdio: "inherit" }
5656
);
57-
57+
5858
console.log(`Built ${outputPath}`);
59-
59+
6060
// Make binary executable (not needed for Windows)
61-
if (platform !== 'win32') {
62-
fs.chmodSync(outputPath, '755');
61+
if (platform !== "win32") {
62+
fs.chmodSync(outputPath, "755");
6363
}
6464
} catch (error) {
6565
console.error(`Failed to build for ${platform}-${arch}: ${error.message}`);
6666
process.exit(1);
6767
}
6868
}
6969

70-
console.log('Build completed successfully!');
70+
console.log("Build completed successfully!");

internal/utils/user_agent.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"runtime"
77
)
88

9+
// Version will be set during build with ldflags
10+
var Version = "dev"
11+
912
// GetUserAgent returns the user agent string for HTTP requests
1013
// This includes the CLI name, version, and runtime information
1114
func GetUserAgent() string {
12-
// For now, use a hardcoded version. In the future, this could be injected
13-
// during the build process via ldflags
14-
version := "1.0.0"
15-
1615
// Include runtime information for better analytics
1716
return fmt.Sprintf("rules-cli/%s (%s; %s; %s)",
18-
version,
17+
Version,
1918
runtime.GOOS,
2019
runtime.GOARCH,
2120
runtime.Version())

scripts/generate_golden.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ trap cleanup EXIT
3131

3232
# Build the latest version of the CLI
3333
echo -e "${GREEN}Building the CLI...${NC}"
34-
go build -o ./rules-cli
34+
# Get version from package.json
35+
VERSION=$(node -p "require('./package.json').version")
36+
go build -ldflags="-X main.Version=${VERSION} -X rules-cli/internal/utils.Version=${VERSION}" -o ./rules-cli
3537

3638
# Copy the CLI to the temporary directory
3739
cp ./rules-cli "$TEMP_DIR/"

tests/golden_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -141,7 +142,15 @@ func TestGoldenFiles(t *testing.T) {
141142
cliPath := "../rules-cli"
142143
if _, err := os.Stat(cliPath); os.IsNotExist(err) {
143144
// Build the CLI if it doesn't exist
144-
cmd := exec.Command("go", "build", "-o", cliPath, "../main.go")
145+
// Get version from package.json
146+
versionCmd := exec.Command("node", "-p", "require('../package.json').version")
147+
versionBytes, err := versionCmd.Output()
148+
if err != nil {
149+
t.Fatalf("Failed to get version from package.json: %v", err)
150+
}
151+
version := strings.TrimSpace(string(versionBytes))
152+
153+
cmd := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.Version=%s -X rules-cli/internal/utils.Version=%s", version, version), "-o", cliPath, "../main.go")
145154
if err := cmd.Run(); err != nil {
146155
t.Fatalf("Failed to build CLI: %v", err)
147156
}

0 commit comments

Comments
 (0)