Skip to content

Commit 50ff24e

Browse files
committed
Add version command and embed build metadata
1 parent d86fe51 commit 50ff24e

5 files changed

Lines changed: 51 additions & 6 deletions

File tree

.github/workflows/build.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,18 @@ jobs:
5151
fi
5252
5353
out="ticktick-${{ matrix.goos }}-${{ matrix.goarch }}${ext}"
54+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
55+
build_version="${GITHUB_REF_NAME}"
56+
else
57+
build_version="main-${GITHUB_SHA::7}"
58+
fi
59+
build_commit="${GITHUB_SHA::7}"
60+
build_date="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
61+
5462
GOOS="${{ matrix.goos }}" GOARCH="${{ matrix.goarch }}" CGO_ENABLED=0 \
55-
go build -trimpath -ldflags="-s -w" -o "dist/${out}" ./cmd/ticktick
63+
go build -trimpath \
64+
-ldflags="-s -w -X 'main.version=${build_version}' -X 'main.commit=${build_commit}' -X 'main.buildDate=${build_date}'" \
65+
-o "dist/${out}" ./cmd/ticktick
5666
5767
(cd dist && sha256sum "${out}" > "${out}.sha256")
5868

.github/workflows/release.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ jobs:
4949
fi
5050
5151
out="ticktick-${{ matrix.goos }}-${{ matrix.goarch }}${ext}"
52+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
53+
build_version="${GITHUB_REF_NAME}"
54+
else
55+
build_version="main-${GITHUB_SHA::7}"
56+
fi
57+
build_commit="${GITHUB_SHA::7}"
58+
build_date="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
59+
5260
GOOS="${{ matrix.goos }}" GOARCH="${{ matrix.goarch }}" CGO_ENABLED=0 \
53-
go build -trimpath -ldflags="-s -w" -o "dist/${out}" ./cmd/ticktick
61+
go build -trimpath \
62+
-ldflags="-s -w -X 'main.version=${build_version}' -X 'main.commit=${build_commit}' -X 'main.buildDate=${build_date}'" \
63+
-o "dist/${out}" ./cmd/ticktick
5464
5565
(cd dist && sha256sum "${out}" > "${out}.sha256")
5666
tar -czf "dist/${out}.tar.gz" -C dist "${out}" "${out}.sha256"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ A Go-based TickTick CLI inspired by the command-oriented style of `gogcli`.
2020
go build -o ticktick ./cmd/ticktick
2121
```
2222

23+
Check version:
24+
25+
```bash
26+
./ticktick --version
27+
```
28+
2329
3. Install globally (pick one):
2430

2531
```bash

cmd/ticktick/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ import (
77
"github.com/apktdev/ticktick-cli/internal/cli"
88
)
99

10+
var (
11+
version = "dev"
12+
commit = "none"
13+
buildDate = "unknown"
14+
)
15+
16+
func buildVersionString() string {
17+
return fmt.Sprintf("%s (commit %s, built %s)", version, commit, buildDate)
18+
}
19+
1020
func main() {
11-
if err := cli.NewRootCmd().Execute(); err != nil {
21+
if err := cli.NewRootCmd(buildVersionString()).Execute(); err != nil {
1222
fmt.Fprintln(os.Stderr, err)
1323
os.Exit(1)
1424
}

internal/cli/root.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type app struct {
1818
shouldPersist bool
1919
}
2020

21-
func NewRootCmd() *cobra.Command {
21+
func NewRootCmd(version string) *cobra.Command {
2222
cfg, err := config.Load()
2323
if err != nil {
2424
panic(err)
@@ -34,8 +34,9 @@ func NewRootCmd() *cobra.Command {
3434
shouldPersist: !envOverride,
3535
}
3636
root := &cobra.Command{
37-
Use: "ticktick",
38-
Short: "TickTick command line client",
37+
Use: "ticktick",
38+
Short: "TickTick command line client",
39+
Version: version,
3940
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
4041
return nil
4142
},
@@ -46,8 +47,16 @@ func NewRootCmd() *cobra.Command {
4647
return config.Save(a.cfg)
4748
},
4849
}
50+
root.SetVersionTemplate("{{.Version}}\n")
4951
root.PersistentFlags().BoolVar(&a.jsonMode, "json", false, "Output JSON")
5052

53+
root.AddCommand(&cobra.Command{
54+
Use: "version",
55+
Short: "Print version",
56+
RunE: func(cmd *cobra.Command, args []string) error {
57+
return a.print(version)
58+
},
59+
})
5160
root.AddCommand(a.newAuthCmd())
5261
root.AddCommand(a.newProjectsCmd())
5362
root.AddCommand(a.newTasksCmd())

0 commit comments

Comments
 (0)