Skip to content

Commit 20743cb

Browse files
committed
add BuildDate to version
1 parent 382563d commit 20743cb

8 files changed

Lines changed: 46 additions & 27 deletions

File tree

.goreleaser.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ builds:
2323
flags:
2424
- -mod=readonly
2525
ldflags:
26-
- -s -w -X main.Version={{.Version}}
26+
- -s -w -X main.Version={{.Version}} -X main.BuildDate={{.Date}}
2727
- id: darwin-arm64
2828
main: ./cmd/lets
2929
goos:
@@ -38,7 +38,7 @@ builds:
3838
flags:
3939
- -mod=readonly
4040
ldflags:
41-
- -s -w -X main.Version={{.Version}}
41+
- -s -w -X main.Version={{.Version}} -X main.BuildDate={{.Date}}
4242
- id: linux-amd64
4343
main: ./cmd/lets
4444
goos:
@@ -51,7 +51,7 @@ builds:
5151
flags:
5252
- -mod=readonly
5353
ldflags:
54-
- -s -w -X main.Version={{.Version}}
54+
- -s -w -X main.Version={{.Version}} -X main.BuildDate={{.Date}}
5555

5656
archives:
5757
- formats: [tar.gz]

AGENTS.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ lets publish-docs # deploy docs site
3232

3333
## Package Structure
3434

35-
- `main.go` — entry point, flag parsing, signal handling
36-
- `cmd/` — Cobra commands (root, subcommands, completion, LSP, self-update)
37-
- `config/` — config file discovery, loading, validation; `config/config/` defines Config/Command/Mixin structs and YAML unmarshaling
38-
- `executor/` — command execution, dependency resolution, env setup, checksum verification
39-
- `env/` — debug level state (`LETS_DEBUG`, levels 0-2)
40-
- `logging/` — logrus-based logging with command chain formatting
41-
- `lsp/` — Language Server Protocol: definition lookup, completion for depends, tree-sitter YAML parsing; `lets lsp` runs stdio-based server for IDE integration
42-
- `checksum/` — SHA1 file checksumming with glob patterns
43-
- `docopt/` — docopt argument parsing, produces `LETSOPT_*` and `LETSCLI_*` env vars
44-
- `upgrade/` — binary self-update from GitHub releases
45-
- `util/` — file/dir/version helpers
46-
- `workdir/``--init` scaffolding
47-
- `set/` — generic Set data structure
48-
- `test/` — test utilities (temp files, args helpers)
35+
- `cmd/lets/main.go` CLI entry point, flag parsing, signal handling
36+
- `internal/cmd/` — Cobra command setup (root, subcommands, completion, LSP, self-update)
37+
- `internal/config/` — config file discovery, loading, validation; `internal/config/config/` defines Config/Command/Mixin structs and YAML unmarshaling; `internal/config/path/` contains config path helpers
38+
- `internal/executor/` — command execution, dependency resolution, env setup, checksum verification
39+
- `internal/env/` — debug level state (`LETS_DEBUG`, levels 0-2)
40+
- `internal/logging/` — logrus-based logging with command chain formatting
41+
- `internal/lsp/` — Language Server Protocol: definition lookup, completion for depends, tree-sitter YAML parsing; `lets lsp` runs stdio-based server for IDE integration
42+
- `internal/checksum/` — SHA1 file checksumming with glob patterns
43+
- `internal/docopt/` — docopt argument parsing, produces `LETSOPT_*` and `LETSCLI_*` env vars
44+
- `internal/upgrade/` — binary self-update from GitHub releases; `internal/upgrade/registry/` contains release registry implementation
45+
- `internal/util/` — file/dir/version helpers
46+
- `internal/workdir/``--init` scaffolding
47+
- `internal/set/` — generic Set data structure
48+
- `internal/test/` — test utilities (temp files, args helpers)
4949

5050
## Key lets.yaml Fields
5151

cmd/lets/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
var Version = "0.0.0-dev"
24+
var BuildDate = ""
2425

2526
func main() {
2627
ctx := getContext()
@@ -29,7 +30,7 @@ func main() {
2930

3031
logging.InitLogging(os.Stdout, os.Stderr)
3132

32-
rootCmd := cmd.CreateRootCommand(Version)
33+
rootCmd := cmd.CreateRootCommand(Version, BuildDate)
3334
rootCmd.InitDefaultHelpFlag()
3435
rootCmd.InitDefaultVersionFlag()
3536
reinitCompletionCmd := cmd.InitCompletionCmd(rootCmd, nil)

internal/cmd/root.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ func newRootCmd(version string) *cobra.Command {
8080
}
8181

8282
// CreateRootCommand used to run only root command without config.
83-
func CreateRootCommand(version string) *cobra.Command {
83+
func CreateRootCommand(version string, buildDate string) *cobra.Command {
8484
rootCmd := newRootCmd(version)
85+
rootCmd.Annotations = map[string]string{"buildDate": buildDate}
8586

8687
initRootFlags(rootCmd)
8788

@@ -230,6 +231,10 @@ func PrintRootHelpMessage(cmd *cobra.Command) error {
230231
}
231232

232233
func PrintVersionMessage(cmd *cobra.Command) error {
233-
_, err := fmt.Fprintf(cmd.OutOrStdout(), "lets version %s\n", cmd.Version)
234+
msg := fmt.Sprintf("lets version %s", cmd.Version)
235+
if buildDate := cmd.Annotations["buildDate"]; buildDate != "" {
236+
msg += fmt.Sprintf(" (%s)", buildDate)
237+
}
238+
_, err := fmt.Fprintln(cmd.OutOrStdout(), msg)
234239
return err
235240
}

internal/cmd/root_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func newTestRootCmd(args []string) (rootCmd *cobra.Command) {
14-
root := CreateRootCommand("v0.0.0-test")
14+
root := CreateRootCommand("v0.0.0-test", "")
1515
root.SetArgs(args)
1616
InitCompletionCmd(root, nil)
1717

@@ -27,7 +27,7 @@ func newTestRootCmdWithConfig(args []string) (rootCmd *cobra.Command, out *bytes
2727
cfg.Commands["foo"] = &config.Command{Name: "foo"}
2828
cfg.Commands["bar"] = &config.Command{Name: "bar"}
2929

30-
root := CreateRootCommand("v0.0.0-test")
30+
root := CreateRootCommand("v0.0.0-test", "")
3131
root.SetArgs(args)
3232
root.SetOut(bufOut)
3333
root.SetErr(bufOut)
@@ -142,7 +142,7 @@ func TestSelfCmd(t *testing.T) {
142142
t.Run("should return exit code 2 for unknown self subcommand", func(t *testing.T) {
143143
bufOut := new(bytes.Buffer)
144144

145-
rootCmd := CreateRootCommand("v0.0.0-test")
145+
rootCmd := CreateRootCommand("v0.0.0-test", "")
146146
rootCmd.SetArgs([]string{"self", "ls"})
147147
rootCmd.SetOut(bufOut)
148148
rootCmd.SetErr(bufOut)
@@ -178,7 +178,7 @@ func TestSelfCmd(t *testing.T) {
178178
t.Run("should return exit code 2 for unknown self subcommand with no suggestions", func(t *testing.T) {
179179
bufOut := new(bytes.Buffer)
180180

181-
rootCmd := CreateRootCommand("v0.0.0-test")
181+
rootCmd := CreateRootCommand("v0.0.0-test", "")
182182
rootCmd.SetArgs([]string{"self", "zzzznotacommand"})
183183
rootCmd.SetOut(bufOut)
184184
rootCmd.SetErr(bufOut)

lets.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ commands:
109109
PATH2LETSDEV=$LETSOPT_PATH
110110
fi
111111
112-
go build -ldflags="-X main.Version=${VERSION:1}-dev" -o "${BIN}" ./cmd/lets && \
112+
go build -ldflags="-X main.Version=${VERSION:1}-dev -X main.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" -o "${BIN}" ./cmd/lets && \
113113
sudo mv ./${BIN} $PATH2LETSDEV/${BIN} && \
114114
echo " - binary ${BIN} version $($PATH2LETSDEV/${BIN} --version) successfully installed in ${PATH2LETSDEV}"
115115
@@ -122,7 +122,7 @@ commands:
122122
BIN=${LETSOPT_BIN:-lets}
123123
124124
go build \
125-
-ldflags="-X main.Version=${VERSION:1}-dev" \
125+
-ldflags="-X main.Version=${VERSION:1}-dev -X main.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
126126
-o ${BIN} ./cmd/lets
127127
128128
success=$?

tests/config_version.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ setup() {
66
load "${BATS_UTILS_PATH}/bats-support/load.bash"
77
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
88
# NOTICE to test this functionality properly we building lets with specified version ${TEST_VERSION}
9-
go build -ldflags="-X main.Version=${TEST_VERSION}" -o ./tests/config_version/lets cmd/lets/main.go
9+
go build -ldflags="-X main.Version=${TEST_VERSION} -X main.BuildDate=2024-01-01T00:00:00Z" -o ./tests/config_version/lets cmd/lets/main.go
1010
cd ./tests/config_version
1111
}
1212

tests/version.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
load test_helpers
2+
3+
TEST_VERSION=1.2.3
4+
TEST_BUILD_DATE=2024-01-15T10:30:00Z
5+
16
setup() {
27
load "${BATS_UTILS_PATH}/bats-support/load.bash"
38
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
@@ -14,3 +19,11 @@ setup() {
1419
assert_success
1520
assert_line --index 0 "lets version 0.0.0-dev"
1621
}
22+
23+
@test "version: show build date when set via ldflags" {
24+
go build -ldflags="-X main.Version=${TEST_VERSION} -X main.BuildDate=${TEST_BUILD_DATE}" -o /tmp/lets-version-test cmd/lets/main.go
25+
run /tmp/lets-version-test --version
26+
assert_success
27+
assert_line --index 0 "lets version ${TEST_VERSION} (${TEST_BUILD_DATE})"
28+
rm -f /tmp/lets-version-test
29+
}

0 commit comments

Comments
 (0)