11package cmd
22
33import (
4+ "fmt"
45 "log/slog"
56 "os"
67
@@ -14,15 +15,20 @@ import (
1415 "github.com/xinnjie/onekeymap-cli/internal/plugins/intellij"
1516 "github.com/xinnjie/onekeymap-cli/internal/plugins/vscode"
1617 "github.com/xinnjie/onekeymap-cli/internal/plugins/zed"
18+ "github.com/xinnjie/onekeymap-cli/internal/updatecheck"
1719
1820 "github.com/xinnjie/onekeymap-cli/pkg/exportapi"
1921 "github.com/xinnjie/onekeymap-cli/pkg/importapi"
2022 "github.com/xinnjie/onekeymap-cli/pkg/metrics"
2123)
2224
23- const (
24- // NOTE: use go build -ldflags "-X github.com/xinnjie/onekeymap-cli/internal/cmd/ version=$(git describe)"
25+ var (
26+ // NOTE: use go build -ldflags "-X github.com/xinnjie/onekeymap-cli/internal/cmd. version=$(git describe)"
2527 version = "dev"
28+ //nolint:gochecknoglobals // use go build -ldflags "-X github.com/xinnjie/onekeymap-cli/internal/cmd.commit=$(git describe)"
29+ commit = "unknown"
30+ //nolint:gochecknoglobals // use go build -ldflags "-X github.com/xinnjie/onekeymap-cli/internal/cmd.dirty=$(git describe)"
31+ dirty = "unknown"
2632)
2733
2834//nolint:gochecknoglobals // TODO(xinnjie): Stop using these global variables. But for now I can not think of a better way.
@@ -44,6 +50,7 @@ type rootFlags struct {
4450 interactive bool
4551 enableTelemetry bool
4652 sandbox bool
53+ skipUpdateCheck bool
4754}
4855
4956func NewCmdRoot () * cobra.Command {
@@ -52,7 +59,7 @@ func NewCmdRoot() *cobra.Command {
5259 cmd := & cobra.Command {
5360 Use : "onekeymap-cli" ,
5461 Short : "A tool to import, export, and synchronize keyboard shortcuts between editors." ,
55- Version : version ,
62+ Version : buildVersionString () ,
5663 PersistentPreRun : rootPersistentPreRun (& f ),
5764 PersistentPostRun : func (cmd * cobra.Command , _ []string ) {
5865 if err := cmdRecorder .Shutdown (cmd .Context ()); err != nil {
@@ -70,6 +77,8 @@ func NewCmdRoot() *cobra.Command {
7077 BoolVar (& f .enableTelemetry , "telemetry" , false , "Enable OpenTelemetry to help improve onekeymap" )
7178 cmd .PersistentFlags ().
7279 BoolVar (& f .sandbox , "sandbox" , false , "Enable sandbox mode for macOS, restricting file access" )
80+ cmd .PersistentFlags ().
81+ BoolVar (& f .skipUpdateCheck , "skip-update-check" , false , "Skip checking for updates" )
7382
7483 if err := viper .BindPFlag ("verbose" , cmd .PersistentFlags ().Lookup ("verbose" )); err != nil {
7584 cmd .PrintErrf ("Error binding verbose flag: %v\n " , err )
@@ -102,6 +111,8 @@ func rootPersistentPreRun(f *rootFlags) func(cmd *cobra.Command, _ []string) {
102111 verbose := viper .GetBool ("verbose" )
103112 quiet := viper .GetBool ("quiet" )
104113 logJSON := viper .GetBool ("log-json" )
114+ sandbox := viper .GetBool ("sandbox" )
115+ ctx := cmd .Context ()
105116
106117 cmdMappingConfig , err = mappings .NewMappingConfig ()
107118 if err != nil {
@@ -180,6 +191,12 @@ func rootPersistentPreRun(f *rootFlags) func(cmd *cobra.Command, _ []string) {
180191
181192 cmdImportService = internal .NewImportService (cmdPluginRegistry , cmdMappingConfig , cmdLogger , cmdRecorder )
182193 cmdExportService = internal .NewExportService (cmdPluginRegistry , cmdMappingConfig , cmdLogger )
194+
195+ // Check for updates asynchronously
196+ if ! sandbox && ! f .skipUpdateCheck {
197+ checker := updatecheck .New (version , cmdLogger )
198+ checker .CheckForUpdate (ctx )
199+ }
183200 }
184201}
185202
@@ -200,3 +217,21 @@ func Execute() {
200217 os .Exit (1 )
201218 }
202219}
220+
221+ func buildVersionString () string {
222+ commitID := commit
223+ if commitID == "" {
224+ commitID = "unknown"
225+ }
226+
227+ status := dirty
228+ if status == "" {
229+ status = "unknown"
230+ }
231+
232+ if version == "dev" {
233+ return fmt .Sprintf ("%s (commit: %s, dirty: %s)" , version , commitID , status )
234+ }
235+
236+ return version
237+ }
0 commit comments