ssharpcc on X reads every changelog before updating, for fear of a ban
when behind, and with many releases a day that is a lot of reading.
Settings → About → Update channel (zh: 更新渠道, 稳定版 / 每个版本) picks
Every version, the default and today's behaviour, or Stable: one version
a week, the newest that had been out three days as the week began
(Monday 00:00 UTC), and an urgent release at once. usemagpie.ai chooses
it from the release list (the previous commit); a release is urgent when
a line of its notes starts with "**Urgent release:**", which
magpie-releases' release run writes from an `Urgent: <why>` trailer
(magpie-releases 73f6cff).
- settings: updateChannel, "" or "stable"; anything else is refused.
- update: on Stable the feed is asked with channel=stable; What's new
shows every release since the installed one (up to 1500) instead of
the newest 40, so a week's notes read as one list.
- gui: the row is segs, never a <select>; a save that doesn't send the
channel keeps it; switching asks the feed again at once, and a
download from every version newer than Stable's pick is dropped,
never restarted into. What's new's notes are kept per channel.
- cli: magpie update channel [stable|every] ("all" too), and it says
which with nothing after it.
- docs: reference.md's Update channel (the rule and the marker); the
privacy pages (en, zh, ja) name ?channel=stable on /api/latest.
The full Go and GUI suites were not run: the disk was at 3 GB free and
other sessions had GUI suites and a 45-minute gateway run going the
whole time.
Run, at this tree before the rebase:
- gofmt -l (clean); go vet on darwin, GOOS=linux -tags nogui and
GOOS=windows for ./internal/update/... ./internal/cli/...
./internal/settings/... ./internal/gui/.
- go test -count=1 ./internal/update/... ./internal/cli/...
./internal/settings/...: ok. ./internal/gui -run
'TestUpdateChannelSwitch|TestWhatsNewKeyedByChannel|TestCoreScriptBudget':
ok (the app.js budget passes).
- GUI, chromium and webkit: update-channel (22: en, zh, zh-TW, ja, de at
900 and 420 wide: default lit, Stable saved, kept by another save,
kept over a reload, Every saved back, no click scrolls, no sideways
scroll at 420), i18n-keys, gui-ja, gui-de, gui-zh-tw and every
update-*.test.cjs: 253 of 253 pass.
- The live feed: /api/latest?channel=stable answers 0.1.1327 (out
2026-10-22 21:55 UTC, the last before Friday 10-23 00:00) with 15
assets after the first real cron run filled the ledger (400 releases
back to 10-10); /api/notes 0.1.1200..0.1.1327 gives 124 notes, none
empty (1213 and 1218 were never published).
After rebasing on afc99490f: TestCoreScriptBudget, the channel Go tests
(update, cli, gui) and GUI i18n-keys + update-channel (both engines,
26 of 26) pass again.
Broken by hand, each red: the feed asked without channel=stable
(TestLatestAsksTheChannel); Stable capped at 40 (TestStableNotesAreAll);
any channel saved (TestUpdateChannelSaved); "all" dropped
(TestUpdateChannelCmd); a save without the field clearing it, no new ask
on a switch, the newer download kept (TestUpdateChannelSwitch); one
notes key for both channels, What's new capped at 40 on Stable
(TestWhatsNewKeyedByChannel); app.js's other saves dropping the channel,
Stable's click saving "" (update-channel 11 of 11 red); the zh label or
a de subtitle taken out (update-channel and i18n-keys red).
Not tried: the full go test ./... and the whole GUI suite (above); a
real app switching channel against the live feed; Windows and Linux
runs (vet only); a real urgent release.
Requested by ssharpcc on X.
263 lines
9.1 KiB
Go
263 lines
9.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// updateExecutable, updateCanElevate and updateInContainer are
|
|
// update.Executable, update.CanElevate and gateway.InContainer; tests stand
|
|
// in for them.
|
|
var (
|
|
updateExecutable = update.Executable
|
|
updateCanElevate = update.CanElevate
|
|
updateInContainer = gateway.InContainer
|
|
)
|
|
|
|
// updateEveries are the intervals magpie update auto takes, by their
|
|
// minutes in settings.UpdateEveries.
|
|
var updateEveries = map[string]int{"30m": 30, "1h": 60, "6h": 360, "24h": 1440}
|
|
|
|
// updateAutoCmd is `magpie update auto [on|off] [download|notify]
|
|
// [30m|1h|6h|24h]`: whether the app asks for a newer version by itself, how
|
|
// often (#472), and whether one found is downloaded at once or only said to
|
|
// be out, downloaded on a click (notify, #2296). With none, it says how it
|
|
// is set.
|
|
func updateAutoCmd(args []string) error {
|
|
s := settings.Load()
|
|
for _, a := range args {
|
|
switch a {
|
|
case "on", "off":
|
|
s.NoAutoUpdate = a == "off"
|
|
case "download", "notify":
|
|
s.NoAutoDownload = a == "notify"
|
|
default:
|
|
m, ok := updateEveries[a]
|
|
if !ok {
|
|
return fmt.Errorf("magpie update auto takes on, off, download, notify or how often (30m, 1h, 6h or 24h), not %q", a)
|
|
}
|
|
s.UpdateEvery = m
|
|
}
|
|
}
|
|
if len(args) > 0 {
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
every := "6h"
|
|
for k, m := range updateEveries {
|
|
if m == s.UpdateEvery {
|
|
every = k
|
|
}
|
|
}
|
|
if s.NoAutoUpdate {
|
|
fmt.Println("automatic updates are off", muted.Render("· magpie update or Settings' Check looks for one (every "+every+" when on)"))
|
|
if s.NoAutoDownload {
|
|
fmt.Println(muted.Render(" a newer version found is only said to be out; Download update or magpie update downloads it"))
|
|
}
|
|
return nil
|
|
}
|
|
if s.NoAutoDownload {
|
|
fmt.Println("automatic updates are on", muted.Render("· magpie looks for a newer version every "+every+" and says it is out; Download update or magpie update downloads it (magpie update auto download: at once)"))
|
|
return nil
|
|
}
|
|
fmt.Println("automatic updates are on", muted.Render("· magpie looks for a newer version every "+every+" and downloads it (magpie update auto notify: only says it is out)"))
|
|
return nil
|
|
}
|
|
|
|
// updateMirrorCmd is `magpie update mirror [<prefix>|off]`: the GitHub
|
|
// download mirror every update goes through, the app's own among them
|
|
// (settings.UpdateMirror). With nothing, it says which.
|
|
func updateMirrorCmd(args []string) error {
|
|
s := settings.Load()
|
|
if len(args) > 1 {
|
|
return fmt.Errorf("magpie update mirror takes one prefix, or off")
|
|
}
|
|
if len(args) == 1 {
|
|
switch m := strings.TrimSpace(args[0]); m {
|
|
case "off", "none", "":
|
|
s.UpdateMirror = ""
|
|
default:
|
|
if err := update.CheckMirror(m); err != nil {
|
|
return err
|
|
}
|
|
s.UpdateMirror = m
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if s.UpdateMirror == "" {
|
|
fmt.Println("updates are downloaded from GitHub", muted.Render("· magpie update mirror <prefix> has them come through a mirror; the checksum is still usemagpie.ai's"))
|
|
return nil
|
|
}
|
|
fmt.Println("updates are downloaded through", bold.Render(s.UpdateMirror), muted.Render("· each is checked against usemagpie.ai's checksum · magpie update mirror off goes back to GitHub"))
|
|
return nil
|
|
}
|
|
|
|
// updateChannelCmd is `magpie update channel [stable|every]`: which
|
|
// versions an update is offered (settings.UpdateChannel, ssharpcc on X):
|
|
// Stable, one a week and an urgent one at once, or every version as it
|
|
// comes out. With none, it says which.
|
|
func updateChannelCmd(args []string) error {
|
|
s := settings.Load()
|
|
if len(args) > 1 {
|
|
return fmt.Errorf("magpie update channel takes stable or every")
|
|
}
|
|
if len(args) == 1 {
|
|
switch strings.ToLower(strings.TrimSpace(args[0])) {
|
|
case "stable":
|
|
s.UpdateChannel = settings.UpdateStable
|
|
case "every", "all":
|
|
s.UpdateChannel = ""
|
|
default:
|
|
return fmt.Errorf("magpie update channel takes stable or every, not %q", args[0])
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if s.UpdateChannel == settings.UpdateStable {
|
|
fmt.Println("update channel: stable", muted.Render("· a version a week, the newest out three days as the week began, and an urgent one at once · magpie update channel every: each one as it comes out"))
|
|
return nil
|
|
}
|
|
fmt.Println("update channel: every version", muted.Render("· each one as it comes out · magpie update channel stable: one a week, an urgent one at once"))
|
|
return nil
|
|
}
|
|
|
|
// updateFlags takes --proxy and --mirror (each as --flag value or
|
|
// --flag=value) out of args.
|
|
func updateFlags(args []string) (rest []string, proxy, mirror string, err error) {
|
|
for i := 0; i < len(args); i++ {
|
|
a := args[i]
|
|
name, val, eq := strings.Cut(a, "=")
|
|
if name != "--proxy" && name != "--mirror" {
|
|
rest = append(rest, a)
|
|
continue
|
|
}
|
|
if !eq {
|
|
if i+1 >= len(args) {
|
|
return nil, "", "", fmt.Errorf("%s needs a value", name)
|
|
}
|
|
i++
|
|
val = args[i]
|
|
}
|
|
if name == "--proxy" {
|
|
if err := update.CheckProxy(val); err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
proxy = val
|
|
continue
|
|
}
|
|
if val != "off" {
|
|
if err := update.CheckMirror(val); err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
}
|
|
mirror = val
|
|
}
|
|
return rest, proxy, mirror, nil
|
|
}
|
|
|
|
// updateCmd is `magpie update [check] [--proxy <url>] [--mirror <prefix>]`:
|
|
// the app replaces its bundle, the terminal build its binary.
|
|
func updateCmd(args []string) error {
|
|
args, proxy, mirror, err := updateFlags(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(args) > 1 && args[1] == "auto" {
|
|
return updateAutoCmd(args[2:])
|
|
}
|
|
if len(args) > 1 && args[1] == "mirror" {
|
|
return updateMirrorCmd(args[2:])
|
|
}
|
|
if len(args) > 1 && args[1] == "channel" {
|
|
return updateChannelCmd(args[2:])
|
|
}
|
|
if len(args) > 2 || len(args) == 2 && args[1] != "check" {
|
|
return fmt.Errorf("usage: magpie update [check] [--proxy <url>] [--mirror <prefix>]")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
|
defer cancel()
|
|
ctx = update.WithMirror(update.WithProxy(ctx, proxy), mirror)
|
|
rel, err := update.Latest(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !update.Newer(rel.Version, version) {
|
|
if update.Released(version) {
|
|
if update.Stable() {
|
|
fmt.Println(green.Render("✓"), "magpie", version, muted.Render("is up to date on the stable channel (stable is "+rel.Version+")"))
|
|
} else {
|
|
fmt.Println(green.Render("✓"), "magpie", version, muted.Render("is the latest"))
|
|
}
|
|
} else {
|
|
fmt.Println("magpie", version, muted.Render("was built from source; the latest release is "+rel.Version))
|
|
}
|
|
return nil
|
|
}
|
|
fmt.Println("magpie", bold.Render(rel.Version), "is out", muted.Render("(you have "+version+") · "+rel.URL))
|
|
if len(args) > 1 && args[1] == "check" {
|
|
return nil
|
|
}
|
|
if !update.Released(version) {
|
|
return fmt.Errorf("this magpie was built from source; update it the way you built it, or get the release from %s", update.Site)
|
|
}
|
|
if app := update.Bundle(); app != "" {
|
|
if update.Stuck(app) != "" {
|
|
return fmt.Errorf("%s can't be replaced where it is; move magpie to Applications, or download the new version from %s", tilde(app), update.Site)
|
|
}
|
|
fmt.Println(muted.Render(" downloading " + update.AppAsset() + via(ctx) + " …"))
|
|
staged, err := update.Stage(ctx, rel, app)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = update.Install(staged, app)
|
|
if update.NeedsAdmin(err) {
|
|
err = update.InstallAsAdmin(staged, app)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "updated", tilde(app), "to", rel.Version, muted.Render("· quit and reopen magpie to use it"))
|
|
if stale := update.StaleCLI(rel.Version); stale != "" {
|
|
// The copy won't follow the app; the fix is the link install.sh
|
|
// makes. `magpie update` run from the copy can't do it (it
|
|
// replaces the copy with another copy), so name the real step.
|
|
bin := filepath.Join(app, "Contents", "MacOS", "magpie")
|
|
fmt.Println(muted.Render(" the `magpie` command at " + tilde(stale) + " is a copy behind this app; re-run the installer, or link it: ln -sf " + tilde(bin) + " " + tilde(stale)))
|
|
}
|
|
return nil
|
|
}
|
|
// the Docker image's /magpie, run as nonroot: the image is what is
|
|
// updated, and a replace would only fail with "permission denied"
|
|
if exe, err := updateExecutable(); err == nil && !update.Writable(filepath.Dir(exe)) && !updateCanElevate() && updateInContainer() {
|
|
return fmt.Errorf("this magpie runs in a container and can't replace itself; pull the new image (docker pull ghcr.io/yetone/magpie:latest) and recreate the container")
|
|
}
|
|
if exe, err := update.Executable(); err == nil && update.Homebrew(exe) {
|
|
return fmt.Errorf("this magpie was installed with Homebrew; update it with: brew upgrade magpie")
|
|
}
|
|
fmt.Println(muted.Render(" downloading " + update.BinaryAsset() + via(ctx) + " …"))
|
|
if err := update.ReplaceBinary(ctx, rel); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "updated to", rel.Version)
|
|
return nil
|
|
}
|
|
|
|
// via says the mirror a download goes through, if any.
|
|
func via(ctx context.Context) string {
|
|
if m := update.Mirror(ctx); m != "" {
|
|
return " through " + m
|
|
}
|
|
return ""
|
|
}
|