summaryrefslogtreecommitdiffstats
path: root/AUTOUPDATE.md
blob: 4c7251965a2a8e072c76f73ed130905be8d1f697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Auto-Update Feature

## Overview

The dborg CLI includes an automatic update checker that runs on every program start. It checks the git remote for newer tagged versions and prompts the user to update if available.

## How It Works

1. **Version Check**: On startup, the CLI queries the git remote for the latest tag
2. **Comparison**: Compares remote version with current version (embedded at build time)
3. **User Prompt**: If newer version exists, prompts user to update
4. **Installation**: Runs `go install git.db.org.ai/dborg` to fetch latest version
5. **Auto-Restart**: Uses `syscall.Exec()` to restart with the new binary seamlessly

## Implementation Details

### Version Embedding

The version is embedded at build time using ldflags:

```bash
go build -ldflags "-X git.db.org.ai/dborg/internal/utils.Version=v0.1.0"
```

The Makefile automatically extracts the version from git tags:

```makefile
VERSION := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.1.0")
LDFLAGS := -X git.db.org.ai/dborg/internal/utils.Version=$(VERSION)
```

### Version Check Process

Located in `internal/utils/version.go`:

1. **CheckForUpdates()**: Main entry point called from root command's PersistentPreRunE
2. **getLatestRemoteTag()**: Queries git remote using `git ls-remote --tags`
3. **isNewerVersion()**: Simple string comparison for version numbers
4. **promptAndUpdate()**: Handles user interaction and update process
5. **restartSelf()**: Restarts the binary using `syscall.Exec()`

### Skip Conditions

Auto-update is skipped when:
- Running in non-interactive mode (piped output)
- Version is "dev" (development builds)
- Git remote query fails (silently continues)
- No newer version available
- User declines update

## User Experience

```
$ dborg npd --firstname John

🔔 A new version of dborg is available: v0.2.0 (current: v0.1.0)
Would you like to update now? [Y/n]: y
Updating to v0.2.0...
✓ Update successful! Restarting...

[Binary automatically restarts and continues with original command]
```

## Manual Update

Users can always update manually:

```bash
go install git.db.org.ai/dborg@latest
```

Or check their current version:

```bash
dborg version
```

## Testing

Unit tests cover version comparison logic:

```bash
go test ./internal/utils -v
```

## Security Considerations

- Only queries official git remote (git.db.org.ai/dborg)
- Runs exact command: `go install git.db.org.ai/dborg`
- No automatic updates without user confirmation
- Fails safely if update errors occur
- Preserves original command arguments after restart