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

Danielade459cf08a4
diff --git a/kernel/cli/cmd/bookmark.go b/kernel/cli/cmd/bookmark.go
new file mode 100644
index 0000000000..7098b54792
--- /dev/null
+++ b/kernel/cli/cmd/bookmark.go
@@ -0,0 +1,125 @@
+// 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"
+ "os"
+ "text/tabwriter"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+
+ "github.com/spf13/cobra"
+)
+
+var bookmarkCmd = &cobra.Command{
+ Use: "bookmark",
+ Short: "Manage bookmarks",
+}
+
+var bookmarkListCmd = &cobra.Command{
+ Use: "list",
+ Short: "List bookmarks",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ bookmarks := model.BuildBookmark()
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(bookmarks, "", " ")
+ fmt.Println(string(data))
+ default:
+ printBookmarkTable(bookmarks)
+ }
+ return nil
+ },
+}
+
+var bookmarkLabelsCmd = &cobra.Command{
+ Use: "labels",
+ Short: "List bookmark labels",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ labels := model.BookmarkLabels()
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(labels, "", " ")
+ fmt.Println(string(data))
+ default:
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ fmt.Fprintln(w, "LABEL")
+ for _, l := range labels {
+ fmt.Fprintln(w, l)
+ }
+ w.Flush()
+ }
+ return nil
+ },
+}
+
+var bookmarkRemoveCmd = &cobra.Command{
+ Use: "remove --label <label>",
+ Short: "Remove a bookmark",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ label, _ := cmd.Flags().GetString("label")
+ if label == "" {
+ return fmt.Errorf("--label is required")
+ }
+ if err := model.RemoveBookmark(label); err != nil {
+ return err
+ }
+ return nil
+ },
+}
+
+var bookmarkRenameCmd = &cobra.Command{
+ Use: "rename --old <old> --new <new>",
+ Short: "Rename a bookmark",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ oldLabel, _ := cmd.Flags().GetString("old")
+ newLabel, _ := cmd.Flags().GetString("new")
+ if oldLabel == "" {
+ return fmt.Errorf("--old is required")
+ }
+ if newLabel == "" {
+ return fmt.Errorf("--new is required")
+ }
+ if err := model.RenameBookmark(oldLabel, newLabel); err != nil {
+ return err
+ }
+ return nil
+ },
+}
+
+func printBookmarkTable(bookmarks *model.Bookmarks) {
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ fmt.Fprintln(w, "NAME\tCOUNT")
+ for _, bm := range *bookmarks {
+ fmt.Fprintf(w, "%s\t%d\n", bm.Name, bm.Count)
+ }
+ w.Flush()
+}
+
+func init() {
+ bookmarkRemoveCmd.Flags().String("label", "", "bookmark label to remove")
+ bookmarkRenameCmd.Flags().String("old", "", "current bookmark label")
+ bookmarkRenameCmd.Flags().String("new", "", "new bookmark label")
+
+ rootCmd.AddCommand(bookmarkCmd)
+ bookmarkCmd.AddCommand(bookmarkListCmd)
+ bookmarkCmd.AddCommand(bookmarkLabelsCmd)
+ bookmarkCmd.AddCommand(bookmarkRemoveCmd)
+ bookmarkCmd.AddCommand(bookmarkRenameCmd)
+}
diff --git a/kernel/cli/cmd/sql.go b/kernel/cli/cmd/sql.go
new file mode 100644
index 0000000000..2152984538
--- /dev/null
+++ b/kernel/cli/cmd/sql.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 (
+ "encoding/json"
+ "fmt"
+ "os"
+ "sort"
+ "text/tabwriter"
+
+ "github.com/siyuan-note/siyuan/kernel/sql"
+
+ "github.com/spf13/cobra"
+)
+
+var sqlCmd = &cobra.Command{
+ Use: "sql <statement>",
+ Short: "Execute SQL query",
+ Args: cobra.MinimumNArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ stmt := args[0]
+ limit, _ := cmd.Flags().GetInt("limit")
+ if limit < 1 {
+ limit = 100
+ }
+
+ rows, err := sql.Query(stmt, limit)
+ if err != nil {
+ return err
+ }
+
+ switch outputFormat {
+ case "json":
+ data, _ := json.MarshalIndent(rows, "", " ")
+ fmt.Println(string(data))
+ default:
+ printSQLResult(rows)
+ }
+ return nil
+ },
+}
+
+func printSQLResult(rows []map[string]any) {
+ if len(rows) == 0 {
+ fmt.Println("No results.")
+ return
+ }
+
+ cols := make([]string, 0, len(rows[0]))
+ for k := range rows[0] {
+ cols = append(cols, k)
+ }
+ sort.Strings(cols)
+
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ for _, col := range cols {
+ fmt.Fprintf(w, "%s\t", col)
+ }
+ fmt.Fprintln(w)
+
+ for _, row := range rows {
+ for _, col := range cols {
+ fmt.Fprintf(w, "%v\t", row[col])
+ }
+ fmt.Fprintln(w)
+ }
+ w.Flush()
+
+ fmt.Printf("\n%d row(s)\n", len(rows))
+}
+
+func init() {
+ sqlCmd.Flags().IntP("limit", "l", 100, "max rows returned")
+
+ rootCmd.AddCommand(sqlCmd)
+}