Skip to content

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

:sparkles: New default appearance icon https://github.com/siyuan-note/siyuan/issues/7976

Daniel9f9769c6d58c
diff --git a/kernel/cli/cmd/repo.go b/kernel/cli/cmd/repo.go
index 1480f8cd28..1f13f5db9f 100644
--- a/kernel/cli/cmd/repo.go
+++ b/kernel/cli/cmd/repo.go
@@ -189,6 +189,94 @@ var repoSearchCmd = &cobra.Command{
},
}
+var repoPurgeCmd = &cobra.Command{
+ Use: "purge",
+ Short: "Purge old snapshots",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := model.PurgeRepo(); err != nil {
+ return err
+ }
+ fmt.Println("ok")
+ return nil
+ },
+}
+
+var repoFileCmd = &cobra.Command{
+ Use: "file",
+ Short: "File-level snapshot operations",
+}
+
+var repoFileGetCmd = &cobra.Command{
+ Use: "get --id <fileID>",
+ Short: "Get file content from snapshot",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+ if id == "" {
+ return fmt.Errorf("--id is required")
+ }
+ data, path, err := model.GetRepoFile(id)
+ if err != nil {
+ return err
+ }
+ output, _ := cmd.Flags().GetString("output")
+ if output != "" {
+ return os.WriteFile(output, data, 0644)
+ }
+ fmt.Printf("Path: %s\nSize: %d\n---\n%s", path, len(data), string(data))
+ return nil
+ },
+}
+
+var repoFileRollbackCmd = &cobra.Command{
+ Use: "rollback --id <fileID>",
+ Short: "Rollback a single file from snapshot",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+ if id == "" {
+ return fmt.Errorf("--id is required")
+ }
+ if err := model.RollbackRepoSnapshotFile(id); err != nil {
+ return err
+ }
+ fmt.Println("ok")
+ return nil
+ },
+}
+
+var repoFileOpenCmd = &cobra.Command{
+ Use: "open --id <fileID>",
+ Short: "Preview file content from snapshot",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+ if id == "" {
+ return fmt.Errorf("--id is required")
+ }
+ title, content, _, _, err := model.OpenRepoSnapshotFile(id)
+ if err != nil {
+ return err
+ }
+ fmt.Printf("Title: %s\n---\n%s\n", title, content)
+ return nil
+ },
+}
+
+var repoFileExportCmd = &cobra.Command{
+ Use: "export --id <fileID>",
+ Short: "Export file from snapshot to temp file",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ id, _ := cmd.Flags().GetString("id")
+ if id == "" {
+ return fmt.Errorf("--id is required")
+ }
+ exportPath, err := model.ExportRepoFile(id)
+ if err != nil {
+ return err
+ }
+ fmt.Println(exportPath)
+ return nil
+ },
+}
+
func printSnapshotTable(snapshots []*model.Snapshot) {
if len(snapshots) == 0 {
fmt.Println("No snapshots found.")
@@ -233,6 +321,15 @@ func init() {
repoSearchCmd.Flags().IntP("page", "p", 1, "page number")
+ repoFileGetCmd.Flags().String("id", "", "file ID (content hash)")
+ repoFileGetCmd.Flags().String("output", "", "output file path (default: stdout)")
+
+ repoFileRollbackCmd.Flags().String("id", "", "file ID to rollback")
+
+ repoFileOpenCmd.Flags().String("id", "", "file ID to preview")
+
+ repoFileExportCmd.Flags().String("id", "", "file ID to export")
+
rootCmd.AddCommand(repoCmd)
repoCmd.AddCommand(repoListCmd)
repoCmd.AddCommand(repoCreateCmd)
@@ -241,4 +338,10 @@ func init() {
repoCmd.AddCommand(repoCheckoutCmd)
repoCmd.AddCommand(repoDiffCmd)
repoCmd.AddCommand(repoSearchCmd)
+ repoCmd.AddCommand(repoPurgeCmd)
+ repoCmd.AddCommand(repoFileCmd)
+ repoFileCmd.AddCommand(repoFileGetCmd)
+ repoFileCmd.AddCommand(repoFileRollbackCmd)
+ repoFileCmd.AddCommand(repoFileOpenCmd)
+ repoFileCmd.AddCommand(repoFileExportCmd)
}
diff --git a/kernel/cli/cmd/sync.go b/kernel/cli/cmd/sync.go
new file mode 100644
index 0000000000..b3d43a516e
--- /dev/null
+++ b/kernel/cli/cmd/sync.go
@@ -0,0 +1,117 @@
+// 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 (
+ "encoding/json"
+ "fmt"
+ "time"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/model"
+
+ "github.com/spf13/cobra"
+)
+
+var syncCmd = &cobra.Command{
+ Use: "sync",
+ Short: "Sync data with cloud",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if model.Conf.Sync.Mode == 3 {
+ return fmt.Errorf("full-manual mode requires 'push' or 'pull' subcommand")
+ }
+ model.SyncData(true)
+ fmt.Println("ok")
+ return nil
+ },
+}
+
+var syncPushCmd = &cobra.Command{
+ Use: "push",
+ Short: "Upload to cloud",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ model.SyncDataUpload()
+ fmt.Println("ok")
+ return nil
+ },
+}
+
+var syncPullCmd = &cobra.Command{
+ Use: "pull",
+ Short: "Download from cloud",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ model.SyncDataDownload()
+ fmt.Println("ok")
+ return nil
+ },
+}
+
+var syncStatusCmd = &cobra.Command{
+ Use: "status",
+ Short: "Show sync status",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ s := model.Conf.Sync
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(map[string]any{
+ "enabled": s.Enabled,
+ "mode": s.Mode,
+ "provider": s.Provider,
+ "stat": s.Stat,
+ "synced": s.Synced,
+ "cloudName": s.CloudName,
+ "perception": s.Perception,
+ "interval": s.Interval,
+ }, "", " ")
+ fmt.Println(string(data))
+ default:
+ fmt.Printf("Enabled: %v\n", s.Enabled)
+ fmt.Printf("Mode: %s\n", syncModeName(s.Mode))
+ if s.Synced > 0 {
+ fmt.Printf("Synced: %s\n", time.UnixMilli(s.Synced).Format("2006-01-02 15:04:05"))
+ }
+ if s.Stat != "" {
+ fmt.Printf("Stat: %s\n", s.Stat)
+ }
+ fmt.Printf("Provider: %s\n", conf.ProviderToStr(s.Provider))
+ fmt.Printf("Cloud: %s\n", s.CloudName)
+ fmt.Printf("Perception: %v\n", s.Perception)
+ fmt.Printf("Interval: %ds\n", s.Interval)
+ }
+ return nil
+ },
+}
+
+func syncModeName(mode int) string {
+ switch mode {
+ case 0, 1:
+ return "auto"
+ case 2:
+ return "manual"
+ case 3:
+ return "full-manual"
+ default:
+ return fmt.Sprintf("unknown(%d)", mode)
+ }
+}
+
+func init() {
+ rootCmd.AddCommand(syncCmd)
+ syncCmd.AddCommand(syncPushCmd)
+ syncCmd.AddCommand(syncPullCmd)
+ syncCmd.AddCommand(syncStatusCmd)
+}