Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Command Line Interface https://github.com/siyuan-note/siyuan/issues/17674

Daniel7d3225164cfd
diff --git a/kernel/cli/build.bat b/kernel/cli/build.bat
deleted file mode 100644
index 1ee6ebe30c..0000000000
--- a/kernel/cli/build.bat
+++ /dev/null
@@ -1,123 +0,0 @@
-@echo off
-echo.
-echo Building SiYuan CLI
-echo.
-cd /d "%~dp0..\"
-if errorlevel 1 (
- exit /b %errorlevel%
-)
-
-set GO111MODULE=on
-set CGO_ENABLED=1
-
-if "%1"=="" (
- set TARGET=local
-) else (
- set TARGET=%1
-)
-
-if "%TARGET%"=="local" (
- echo Building for local platform...
- go build -tags fts5 -ldflags "-s -w" -o siyuan.exe ./cli
- if errorlevel 1 (
- echo Build failed
- exit /b %errorlevel%
- )
- echo Done: siyuan.exe
- goto :end
-)
-
-if "%TARGET%"=="windows" (
- echo Building Windows amd64...
- set GOOS=windows
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-win-amd64.exe ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-win-amd64.exe
-)
-
-if "%TARGET%"=="linux" (
- echo Building Linux amd64...
- set GOOS=linux
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-amd64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-linux-amd64
-
- echo Building Linux arm64...
- set GOOS=linux
- set GOARCH=arm64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-arm64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-linux-arm64
-)
-
-if "%TARGET%"=="darwin" (
- echo Building macOS amd64...
- set GOOS=darwin
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-amd64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-darwin-amd64
-
- echo Building macOS arm64...
- set GOOS=darwin
- set GOARCH=arm64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-arm64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-darwin-arm64
-)
-
-if "%TARGET%"=="all" (
- call :build_windows
- call :build_linux
- call :build_darwin
- goto :end
-)
-
-goto :end
-
-:build_windows
- echo Building Windows amd64...
- set GOOS=windows
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-win-amd64.exe ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-win-amd64.exe
- exit /b
-
-:build_linux
- echo Building Linux amd64...
- set GOOS=linux
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-amd64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-linux-amd64
-
- echo Building Linux arm64...
- set GOOS=linux
- set GOARCH=arm64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-arm64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-linux-arm64
- exit /b
-
-:build_darwin
- echo Building macOS amd64...
- set GOOS=darwin
- set GOARCH=amd64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-amd64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-darwin-amd64
-
- echo Building macOS arm64...
- set GOOS=darwin
- set GOARCH=arm64
- go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-arm64 ./cli
- if errorlevel 1 ( exit /b %errorlevel% )
- echo Done: siyuan-darwin-arm64
- exit /b
-
-:end
-echo.
-echo Build complete.
diff --git a/kernel/cli/build.sh b/kernel/cli/build.sh
deleted file mode 100644
index 774c5dfad0..0000000000
--- a/kernel/cli/build.sh
+++ /dev/null
@@ -1,67 +0,0 @@
-#!/bin/bash
-set -e
-
-SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
-KERNEL_DIR="$(dirname "$SCRIPT_DIR")"
-cd "$KERNEL_DIR"
-
-export GO111MODULE=on
-export CGO_ENABLED=1
-
-build_local() {
- echo "Building for local platform..."
- go build -tags fts5 -ldflags "-s -w" -o siyuan ./cli
- echo "Done: siyuan"
-}
-
-build_windows() {
- echo "Building Windows amd64..."
- GOOS=windows GOARCH=amd64 go build -tags fts5 -ldflags "-s -w" -o siyuan-win-amd64.exe ./cli
- echo "Done: siyuan-win-amd64.exe"
-}
-
-build_linux() {
- echo "Building Linux amd64..."
- GOOS=linux GOARCH=amd64 go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-amd64 ./cli
- echo "Done: siyuan-linux-amd64"
-
- echo "Building Linux arm64..."
- GOOS=linux GOARCH=arm64 go build -tags fts5 -ldflags "-s -w" -o siyuan-linux-arm64 ./cli
- echo "Done: siyuan-linux-arm64"
-}
-
-build_darwin() {
- echo "Building macOS amd64..."
- GOOS=darwin GOARCH=amd64 go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-amd64 ./cli
- echo "Done: siyuan-darwin-amd64"
-
- echo "Building macOS arm64..."
- GOOS=darwin GOARCH=arm64 go build -tags fts5 -ldflags "-s -w" -o siyuan-darwin-arm64 ./cli
- echo "Done: siyuan-darwin-arm64"
-}
-
-TARGET="${1:-local}"
-
-case "$TARGET" in
- local) build_local ;;
- windows) build_windows ;;
- linux) build_linux ;;
- darwin) build_darwin ;;
- all)
- build_windows
- build_linux
- build_darwin
- ;;
- *)
- echo "Usage: $0 [local|windows|linux|darwin|all]"
- echo " local Build for current platform (default)"
- echo " windows Build Windows amd64"
- echo " linux Build Linux amd64 + arm64"
- echo " darwin Build macOS amd64 + arm64"
- echo " all Build all platforms"
- exit 1
- ;;
-esac
-
-echo ""
-echo "Build complete."
diff --git a/kernel/cli/cmd/serve.go b/kernel/cli/cmd/serve.go
new file mode 100644
index 0000000000..ef820d8826
--- /dev/null
+++ b/kernel/cli/cmd/serve.go
@@ -0,0 +1,91 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package cmd
+
+import (
+ "os"
+
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/job"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/plugin"
+ "github.com/siyuan-note/siyuan/kernel/server"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+ "github.com/siyuan-note/siyuan/kernel/util"
+
+ "github.com/spf13/cobra"
+)
+
+var serveCmd = &cobra.Command{
+ Use: "serve",
+ Short: "Start kernel HTTP server",
+ FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
+ PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
+ return nil // bypass root's init — Boot() handles it
+ },
+ Run: func(cmd *cobra.Command, args []string) {
+ // Boot() uses flag.Parse() which expects raw --flags, not a cobra subcommand
+ // Strip the "serve" subcommand from os.Args before calling Boot()
+ saved := os.Args
+ newArgs := make([]string, 1, len(os.Args))
+ newArgs[0] = os.Args[0]
+ for i := 1; i < len(os.Args); i++ {
+ if i == 1 && os.Args[1] == "serve" {
+ continue
+ }
+ newArgs = append(newArgs, os.Args[i])
+ }
+ os.Args = newArgs
+ defer func() { os.Args = saved }()
+
+ util.Boot()
+
+ model.InitJwtKey()
+ model.InitConf()
+ go server.Serve(false, model.Conf.CookieKey)
+ model.InitAppearance()
+ sql.InitDatabase(false)
+ sql.InitHistoryDatabase(false)
+ sql.InitAssetContentDatabase(false)
+ sql.SetCaseSensitive(model.Conf.Search.CaseSensitive)
+ sql.SetIndexAssetPath(model.Conf.Search.IndexAssetPath)
+
+ model.BootSyncData()
+ model.InitBoxes()
+ model.LoadFlashcards()
+ util.LoadAssetsTexts()
+
+ util.SetBooted()
+ util.PushClearAllMsg()
+
+ job.StartCron()
+
+ go model.AutoGenerateFileHistory()
+ go cache.LoadAssets()
+ go util.CheckFileSysStatus()
+ go plugin.InitManager()
+
+ model.WatchAssets()
+ model.WatchEmojis()
+ model.WatchThemes()
+ model.HandleSignal()
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(serveCmd)
+}
diff --git a/kernel/cli/main.go b/kernel/cli/main.go
deleted file mode 100644
index 2169bab603..0000000000
--- a/kernel/cli/main.go
+++ /dev/null
@@ -1,29 +0,0 @@
-// SiYuan - Refactor your thinking
-// Copyright (c) 2020-present, b3log.org
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see <https://www.gnu.org/licenses/>.
-
-package main
-
-import (
- "os"
-
- "github.com/siyuan-note/siyuan/kernel/cli/cmd"
-)
-
-func main() {
- if err := cmd.Execute(); err != nil {
- os.Exit(1)
- }
-}
diff --git a/kernel/main.go b/kernel/main.go
index 98c96d95db..8ae36a28f3 100644
--- a/kernel/main.go
+++ b/kernel/main.go
@@ -19,45 +19,18 @@
package main
import (
- "github.com/siyuan-note/siyuan/kernel/cache"
- "github.com/siyuan-note/siyuan/kernel/job"
- "github.com/siyuan-note/siyuan/kernel/model"
- "github.com/siyuan-note/siyuan/kernel/plugin"
- "github.com/siyuan-note/siyuan/kernel/server"
- "github.com/siyuan-note/siyuan/kernel/sql"
- "github.com/siyuan-note/siyuan/kernel/util"
+ "os"
+ "strings"
+
+ "github.com/siyuan-note/siyuan/kernel/cli/cmd"
)
func main() {
- util.Boot()
-
- model.InitJwtKey()
- model.InitConf()
- go server.Serve(false, model.Conf.CookieKey)
- model.InitAppearance()
- sql.InitDatabase(false)
- sql.InitHistoryDatabase(false)
- sql.InitAssetContentDatabase(false)
- sql.SetCaseSensitive(model.Conf.Search.CaseSensitive)
- sql.SetIndexAssetPath(model.Conf.Search.IndexAssetPath)
-
- model.BootSyncData()
- model.InitBoxes()
- model.LoadFlashcards()
- util.LoadAssetsTexts()
-
- util.SetBooted()
- util.PushClearAllMsg()
-
- job.StartCron()
-
- go model.AutoGenerateFileHistory()
- go cache.LoadAssets()
- go util.CheckFileSysStatus()
- go plugin.InitManager()
-
- model.WatchAssets()
- model.WatchEmojis()
- model.WatchThemes()
- model.HandleSignal()
+ // Auto-detect: --flag style args → kernel serve mode
+ if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-") {
+ os.Args = append([]string{os.Args[0], "serve"}, os.Args[1:]...)
+ }
+ if err := cmd.Execute(); err != nil {
+ os.Exit(1)
+ }
}