Skip to content

Commit 014b5f4

Browse files
authored
fix(librarian): read version from version.txt file (#2347)
Fixes #2348 Moves version.txt to the `internal/cli` package so it can be read by the `embed` package as a variable. When constructing the synthetic version number, use this release version as the base.
1 parent f7c0b84 commit 014b5f4

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

.librarian/state.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
image: us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/librarian-release-container@sha256:50dd9b59bcf4f55178f9a35dcd4f81a8db20609da386357fa05a457a8da3b4e9
1+
image: us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/librarian-release-container:latest
22
libraries:
33
- id: librarian
44
version: 0.2.0

infra/container/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ls -al /output
2323
new_version=$(jq -r '.libraries[0].version' /librarian/release-init-request.json)
2424
echo "release version: ${new_version}"
2525
mkdir /output/internal/
26-
echo "${new_version}" > /output/internal/version.txt
26+
echo "${new_version}" > /output/internal/cli/version.txt
2727

2828
ls -al /output
2929

internal/cli/version.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
package cli
1616

1717
import (
18+
_ "embed"
1819
"runtime/debug"
1920
"strings"
2021
"time"
2122
)
2223

24+
//go:embed version.txt
25+
var versionString string
26+
2327
// Version return the version information for the binary, which is constructed
2428
// following https://go.dev/ref/mod#versions.
2529
func Version() string {
@@ -52,7 +56,7 @@ func version(info *debug.BuildInfo) string {
5256
// Construct the pseudo-version string per
5357
// https://go.dev/ref/mod#pseudo-versions.
5458
var buf strings.Builder
55-
buf.WriteString("0.0.0")
59+
buf.WriteString(strings.TrimSpace(versionString))
5660
if revision != "" {
5761
buf.WriteString("-")
5862
// Per https://go.dev/ref/mod#pseudo-versions, only use the first 12
File renamed without changes.

internal/cli/version_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
package cli
1616

1717
import (
18+
"fmt"
1819
"runtime/debug"
20+
"strings"
1921
"testing"
2022
)
2123

2224
func TestVersion(t *testing.T) {
25+
baseVersion := strings.TrimSpace(versionString)
2326
for _, test := range []struct {
2427
name string
2528
want string
@@ -36,7 +39,7 @@ func TestVersion(t *testing.T) {
3639
},
3740
{
3841
name: "pseudoversion",
39-
want: "0.0.0-123456789000-20230125195754",
42+
want: fmt.Sprintf("%s-123456789000-20230125195754", baseVersion),
4043
buildinfo: &debug.BuildInfo{
4144
Settings: []debug.BuildSetting{
4245
{Key: "vcs.revision", Value: "1234567890001234"},
@@ -46,7 +49,7 @@ func TestVersion(t *testing.T) {
4649
},
4750
{
4851
name: "pseudoversion only revision",
49-
want: "0.0.0-123456789000",
52+
want: fmt.Sprintf("%s-123456789000", baseVersion),
5053
buildinfo: &debug.BuildInfo{
5154
Settings: []debug.BuildSetting{
5255
{Key: "vcs.revision", Value: "1234567890001234"},
@@ -55,7 +58,7 @@ func TestVersion(t *testing.T) {
5558
},
5659
{
5760
name: "pseudoversion only time",
58-
want: "0.0.0-20230102150405",
61+
want: fmt.Sprintf("%s-20230102150405", baseVersion),
5962
buildinfo: &debug.BuildInfo{
6063
Settings: []debug.BuildSetting{
6164
{Key: "vcs.time", Value: "2023-01-02T15:04:05Z"},
@@ -64,7 +67,7 @@ func TestVersion(t *testing.T) {
6467
},
6568
{
6669
name: "pseudoversion invalid time",
67-
want: "0.0.0-123456789000",
70+
want: fmt.Sprintf("%s-123456789000", baseVersion),
6871
buildinfo: &debug.BuildInfo{
6972
Settings: []debug.BuildSetting{
7073
{Key: "vcs.revision", Value: "123456789000"},
@@ -74,7 +77,7 @@ func TestVersion(t *testing.T) {
7477
},
7578
{
7679
name: "revision less than 12 chars",
77-
want: "0.0.0-shortrev-20230125195754",
80+
want: fmt.Sprintf("%s-shortrev-20230125195754", baseVersion),
7881
buildinfo: &debug.BuildInfo{
7982
Settings: []debug.BuildSetting{
8083
{Key: "vcs.revision", Value: "shortrev"},

0 commit comments

Comments
 (0)