Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ All commands output JSON envelopes (`{"ok": true, "data": ...}`), making them co
| Command | Description |
|---------|-------------|
| `gm init` | Initialize event graph database |
| `gm update check` | Check GitHub Releases for a newer CLI version |
| `gm update apply` | Download and install a newer CLI version |

### Proposal-First Writes

Expand Down Expand Up @@ -219,6 +221,20 @@ Download a pre-built binary from [Releases](https://github.com/GuoMonth/graphmin
go install github.com/senguoyun-guosheng/graphmind/cmd/gm@latest
```

## Updates

GraphMind can check for newer releases published on GitHub:

```bash
gm update check
gm update apply
gm update apply --version v0.3.1
```

Normal `gm` commands may trigger a **non-blocking** background update check at
most once every 24 hours. If a newer release is already known, `gm` prints a
short notice to **stderr** while keeping stdout JSON unchanged.

---

## License
Expand Down
41 changes: 41 additions & 0 deletions docs/cli-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ One JSON object per line. Every object includes `id` and `entity` for downstream
| Command | Purpose |
|---|---|
| `gm init` | Initialize graph database |
| `gm update check` | Check GitHub Releases for a newer CLI version |
| `gm update apply` | Download and install a release archive |
| `gm schema` | Machine-readable command/type schema (JSON) |

---
Expand Down Expand Up @@ -172,6 +174,45 @@ Creates the database file and runs all migrations. Safe to run on existing datab

---

### gm update check

Check GitHub Releases for a newer `gm` binary.

```
gm update check
```

This command performs a blocking network request and refreshes the local update
cache immediately.

Regular `gm` commands may also trigger a **non-blocking** background update
check at most once every 24 hours. Any update hint is written to **stderr** so
stdout remains valid JSON.

---

### gm update apply

Download and install the latest matching release asset for the current
platform.

```
gm update apply [--version <tag>]
```

| Flag | Description |
|---|---|
| `--version <tag>` | Install a specific release tag instead of the latest one |

Examples:

```bash
gm update apply
gm update apply --version v0.3.1
```

---

### gm schema

Output machine-readable schema as JSON. AI agents call this once at session start for self-discovery.
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ go 1.26.2

require (
github.com/google/uuid v1.6.0
github.com/minio/selfupdate v0.6.0
github.com/spf13/cobra v1.10.2
modernc.org/sqlite v1.48.2
)

require (
aead.dev/minisign v0.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/spf13/pflag v1.0.9 // indirect
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
golang.org/x/sys v0.42.0 // indirect
modernc.org/libc v1.70.0 // indirect
modernc.org/mathutil v1.7.1 // indirect
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
aead.dev/minisign v0.2.0 h1:kAWrq/hBRu4AARY6AlciO83xhNnW9UaC8YipS2uhLPk=
aead.dev/minisign v0.2.0/go.mod h1:zdq6LdSd9TbuSxchxwhpA9zEb9YXcVGoE8JakuiGaIQ=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
Expand All @@ -11,6 +13,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minio/selfupdate v0.6.0 h1:i76PgT0K5xO9+hjzKcacQtO7+MjJ4JKA8Ak8XQ9DDwU=
github.com/minio/selfupdate v0.6.0/go.mod h1:bO02GTIPCMQFTEvE5h4DjYB58bCoZ35XLeBf0buTDdM=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
Expand All @@ -21,13 +25,30 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b h1:QAqMVf3pSa6eeTsuklijukjXBlj7Es2QQplab+/RbQ4=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
37 changes: 35 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/senguoyun-guosheng/graphmind/internal/model"
"github.com/senguoyun-guosheng/graphmind/internal/proposal"
"github.com/senguoyun-guosheng/graphmind/internal/tag"
"github.com/senguoyun-guosheng/graphmind/internal/update"
"github.com/spf13/cobra"
)

Expand All @@ -31,6 +32,7 @@ type services struct {
graph *graph.Service
tag *tag.Service
proposal *proposal.Service
updater *update.Manager
}

var svc services
Expand Down Expand Up @@ -107,6 +109,7 @@ COMMANDS

Setup:
init Initialize graph database gm init
update Check/apply CLI updates gm update check

GLOBAL FLAGS

Expand Down Expand Up @@ -138,8 +141,10 @@ Use "gm <command> --help" for detailed usage, examples, and output samples.`,

func init() {
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// init handles its own DB setup; skip for root-only (help/version)
if cmd.Name() == "init" || cmd.Parent() == nil {
if shouldAutoCheckForUpdates(cmd) && svc.updater != nil {
svc.updater.MaybeNotifyAndCheck()
}
if shouldSkipServiceWire(cmd) {
return nil
}
return wireAndMigrate(cmd.Context())
Expand All @@ -162,11 +167,13 @@ func init() {
rootCmd.AddCommand(grepCmd)
rootCmd.AddCommand(logCmd)
rootCmd.AddCommand(batchCmd)
rootCmd.AddCommand(updateCmd)
}

// Execute runs the root command and handles exit codes.
func Execute(version string) {
rootCmd.Version = version
svc.updater = update.NewManager(version)
if err := rootCmd.Execute(); err != nil {
code := outputError(err)
os.Exit(code)
Expand Down Expand Up @@ -283,3 +290,29 @@ func newJSONEncoder(w io.Writer) *json.Encoder {
enc.SetEscapeHTML(false)
return enc
}

func shouldSkipServiceWire(cmd *cobra.Command) bool {
if cmd == nil || cmd.Parent() == nil {
return true
}
return commandInTree(cmd, "init") || commandInTree(cmd, "update")
}

func shouldAutoCheckForUpdates(cmd *cobra.Command) bool {
if quiet || cmd == nil {
return false
}
if cmd.Parent() == nil {
return false
}
return !commandInTree(cmd, "init") && !commandInTree(cmd, "update")
}

func commandInTree(cmd *cobra.Command, name string) bool {
for current := cmd; current != nil; current = current.Parent() {
if current.Name() == name {
return true
}
}
return false
}
90 changes: 90 additions & 0 deletions internal/cli/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cli

import (
"fmt"

"github.com/senguoyun-guosheng/graphmind/internal/update"
"github.com/spf13/cobra"
)

var (
updateApplyVersion string
updateCheckBackground bool
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "Check for or apply gm updates",
Long: `Manage GraphMind CLI updates published through GitHub Releases.

Use "gm update check" to perform a blocking version check now.
Use "gm update apply" to download and install a newer release.

Regular gm commands may trigger a non-blocking background update check at most
once every 24 hours. If a newer release is already known, gm prints a short
notice to stderr without changing stdout JSON output.`,
}

var updateCheckCmd = &cobra.Command{
Use: "check",
Short: "Check for a newer release",
Long: `Check GitHub Releases for a newer GraphMind CLI build.

This command performs a blocking network request and refreshes the local update
cache immediately. Normal gm commands use a separate non-blocking background
check path instead.`,
RunE: func(cmd *cobra.Command, _ []string) error {
result, err := svc.updater.Check(cmd.Context(), update.CheckOptions{
Background: updateCheckBackground,
})
if err != nil {
return err
}
if updateCheckBackground {
return nil
}

summary := fmt.Sprintf("Checked for updates. Current: %s. Latest: %s.", result.CurrentVersion, result.LatestVersion)
next := []string{"gm update apply — download and install the latest release"}
if !result.UpdateAvailable {
summary = fmt.Sprintf("Already up to date at %s.", result.CurrentVersion)
next = []string{"gm --version — confirm the active version"}
}
outputSuccess(result, summary, next)
return nil
},
}

var updateApplyCmd = &cobra.Command{
Use: "apply",
Short: "Download and install a release",
Long: `Download the matching GitHub Release archive for the current platform and
replace the current gm executable.

By default this installs the latest release. Use --version to pin a specific
tag such as v0.3.1.`,
RunE: func(cmd *cobra.Command, _ []string) error {
result, err := svc.updater.Apply(cmd.Context(), updateApplyVersion)
if err != nil {
return err
}

summary := fmt.Sprintf("Installed %s.", result.InstalledVersion)
next := []string{"gm --version — confirm the installed version"}
if !result.Updated {
summary = fmt.Sprintf("Already up to date at %s.", result.InstalledVersion)
}
outputSuccess(result, summary, next)
return nil
},
}

func init() {
updateCmd.AddCommand(updateCheckCmd)
updateCmd.AddCommand(updateApplyCmd)

updateCheckCmd.Flags().BoolVar(&updateCheckBackground, "background", false, "Run as a silent background refresh")
_ = updateCheckCmd.Flags().MarkHidden("background")

updateApplyCmd.Flags().StringVar(&updateApplyVersion, "version", "", "Install a specific release tag (for example v0.3.1)")
}
Loading
Loading