Skip to content

Commit a2b3d33

Browse files
committed
feat: add update check
1 parent f903ec8 commit a2b3d33

19 files changed

Lines changed: 439 additions & 82 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: golangci-lint
2222
uses: golangci/golangci-lint-action@v8
2323
with:
24-
version: v2.4.0
24+
version: v2.5.0
2525
args: --timeout 3m0s
2626

2727
build:

.golangci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ linters:
9393
- nilnesserr # reports that it checks for err != nil, but it returns a different nil value error (powered by nilness and nilerr)
9494
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
9595
- noctx # finds sending http request without context.Context
96-
- nolintlint # reports ill-formed or insufficient nolint directives
9796
- nonamedreturns # reports all named returns
9897
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
9998
- perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ builds:
3434
- arm64
3535
ldflags:
3636
- -s -w
37-
- -X github.com/xinnjie/onekeymap-cli/internal/cmd/version={{.Version}}
37+
- -X github.com/xinnjie/onekeymap-cli/internal/cmd.version={{.Version}}
3838

3939
archives:
4040
- id: onekeymap-cli

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
GO_LINT_CONFIG := $(CURDIR)/.golangci.yaml
44
GO_ENV := GO111MODULE=on GOFLAGS=-mod=mod
5+
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)
6+
GIT_DIRTY := $(shell if test -n "$$(git status --porcelain 2>/dev/null)"; then echo "true"; else echo "false"; fi)
7+
GO_LDFLAGS := -X github.com/xinnjie/onekeymap-cli/internal/cmd.commit=$(GIT_COMMIT) -X github.com/xinnjie/onekeymap-cli/internal/cmd.dirty=$(GIT_DIRTY)
58

69
build:
710
@mkdir -p .bin
8-
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o .bin/onekeymap-cli-arm64 ./cmd/onekeymap-cli
9-
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o .bin/onekeymap-cli-amd64 ./cmd/onekeymap-cli
11+
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags "$(GO_LDFLAGS)" -o .bin/onekeymap-cli-arm64 ./cmd/onekeymap-cli
12+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(GO_LDFLAGS)" -o .bin/onekeymap-cli-amd64 ./cmd/onekeymap-cli
1013
lipo -create -output .bin/onekeymap-cli .bin/onekeymap-cli-arm64 .bin/onekeymap-cli-amd64
1114
rm .bin/onekeymap-cli-arm64 .bin/onekeymap-cli-amd64
1215
./.bin/onekeymap-cli dev docSupportActions &> ./action-support-matrix.md

README.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,51 @@ OneKeymap CLI is a powerful command-line tool that lets you import, export, and
2222

2323
## 🚀 Quick Start
2424

25-
> ❗️Currently the OneKeymap CLI only supports macOS. Windows and Linux support is coming soon.
26-
2725
### Installation
2826

29-
**macOS (Homebrew):**
30-
```bash
31-
brew install xinnjie/homebrew-onekeymap/onekeymap-cli
32-
```
33-
34-
**Go Install:**
35-
```bash
36-
go install github.com/xinnjie/onekeymap-cli/cmd/onekeymap-cli@latest
37-
```
27+
#### macOS
28+
29+
- **Homebrew**
30+
```bash
31+
brew install xinnjie/homebrew-onekeymap/onekeymap-cli
32+
```
33+
34+
#### Linux
35+
36+
- **Debian/Ubuntu (.deb)**
37+
```bash
38+
wget https://github.com/xinnjie/onekeymap-cli/releases/download/v<version>/onekeymap-cli_<version>_x86_64.deb
39+
sudo dpkg -i onekeymap-cli_<version>_x86_64.deb
40+
```
41+
- **Fedora/RHEL/CentOS (.rpm)**
42+
```bash
43+
wget https://github.com/xinnjie/onekeymap-cli/releases/download/v<version>/onekeymap-cli_<version>_x86_64.rpm
44+
sudo rpm -i onekeymap-cli_<version>_x86_64.rpm
45+
```
46+
- **Alpine (.apk)**
47+
```bash
48+
wget https://github.com/xinnjie/onekeymap-cli/releases/download/v<version>/onekeymap-cli_<version>_x86_64.apk
49+
sudo apk add --allow-untrusted onekeymap-cli_<version>_x86_64.apk
50+
```
51+
52+
#### Windows
53+
54+
- **Zip Archive**
55+
Download `onekeymap-cli_Windows_<arch>.zip` from [GitHub Releases](https://github.com/xinnjie/onekeymap-cli/releases), extract it, and add the directory to your PATH, or run:
56+
```powershell
57+
Expand-Archive -Path .\onekeymap-cli_*.zip -DestinationPath "$Env:USERPROFILE\onekeymap-cli"
58+
setx PATH "$Env:USERPROFILE\onekeymap-cli;$Env:PATH"
59+
```
60+
61+
#### Cross-platform
62+
63+
- **Go Install**
64+
```bash
65+
go install github.com/xinnjie/onekeymap-cli/cmd/onekeymap-cli@latest
66+
```
3867

3968
**From Release:**
40-
Download the latest binary from [GitHub Releases](https://github.com/xinnjie/onekeymap-cli/releases).
69+
Download the latest binary from [GitHub Releases](https://github.com/xinnjie/onekeymap-cli/releases/latest).
4170

4271
### Basic Usage
4372

@@ -70,7 +99,8 @@ You can append `-h` or `--help` to any subcommand for detailed flag descriptions
7099

71100
---
72101

73-
## Configuration
102+
<details>
103+
<summary><strong>Configuration</strong></summary>
74104

75105
OneKeymap can be configured via a config file at `~/.config/onekeymap/config.yaml`:
76106

@@ -88,6 +118,8 @@ editors:
88118
keymap_path: ~/Library/Application Support/JetBrains/IntelliJIdea2024.1/keymaps/custom.xml
89119
```
90120
121+
</details>
122+
91123
---
92124
93125
## 🧩 Supported Editors

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/charmbracelet/lipgloss v1.1.0
1010
github.com/google/go-cmp v0.7.0
1111
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2
12+
github.com/hashicorp/go-version v1.7.0
1213
github.com/mrk21/go-diff-fmt v0.2.0
1314
github.com/pelletier/go-toml/v2 v2.2.4
1415
github.com/sergi/go-diff v1.4.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2 h1:sGm2vDRFUrQJO/Veii4h4z
8484
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2/go.mod h1:wd1YpapPLivG6nQgbf7ZkG1hhSOXDhhn4MLTknx2aAc=
8585
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 h1:8Tjv8EJ+pM1xP8mK6egEbD1OgnVTyacbefKhmbLhIhU=
8686
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2/go.mod h1:pkJQ2tZHJ0aFOVEEot6oZmaVEZcRme73eIFmhiVuRWs=
87+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
88+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
8789
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
8890
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
8991
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=

internal/cmd/root.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
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

4956
func 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

Comments
 (0)