Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(hub): skip PocketBase bootstrap for update command (#2800)

henrygd2c1980f47d0b
diff --git a/internal/cmd/hub/hub.go b/internal/cmd/hub/hub.go
index 678632b26..e85a9a89d 100644
--- a/internal/cmd/hub/hub.go
+++ b/internal/cmd/hub/hub.go
@@ -29,6 +29,17 @@ func main() {
}
baseApp := getBaseApp()
+
+ // The update command doesn't use the database, so run it without bootstrapping
+ // PocketBase. Bootstrapping would create the data dir relative to the working
+ // directory and open the database as the updater's user (usually root).
+ if isUpdateCmd(baseApp.RootCmd, os.Args[1:]) {
+ if err := baseApp.RootCmd.Execute(); err != nil {
+ log.Fatal(err)
+ }
+ return
+ }
+
hub := hub.NewHub(baseApp)
if err := hub.StartHub(); err != nil {
log.Fatal(err)
@@ -67,6 +78,12 @@ func getBaseApp() *pocketbase.PocketBase {
return baseApp
}
+// isUpdateCmd reports whether args resolve to the update command.
+func isUpdateCmd(rootCmd *cobra.Command, args []string) bool {
+ cmd, _, err := rootCmd.Find(args)
+ return err == nil && cmd.Name() == "update"
+}
+
func newHealthCmd() *cobra.Command {
var baseURL string
diff --git a/internal/cmd/hub/hub_test.go b/internal/cmd/hub/hub_test.go
new file mode 100644
index 000000000..e94804ebd
--- /dev/null
+++ b/internal/cmd/hub/hub_test.go
@@ -0,0 +1,34 @@
+//go:build testing
+
+package main
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestIsUpdateCmd(t *testing.T) {
+ rootCmd := getBaseApp().RootCmd
+
+ tests := []struct {
+ name string
+ args []string
+ want bool
+ }{
+ {"update", []string{"update"}, true},
+ {"update with flag", []string{"update", "--china-mirrors"}, true},
+ {"update with global flag first", []string{"--dir", "/tmp/x", "update"}, true},
+ {"update help", []string{"update", "--help"}, true},
+ {"serve", []string{"serve"}, false},
+ {"health", []string{"health", "--url", "http://localhost"}, false},
+ {"no args", []string{}, false},
+ {"unknown", []string{"nope"}, false},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.want, isUpdateCmd(rootCmd, tt.args))
+ })
+ }
+}