Skip to content

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

5.5 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package cmd

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"text/tabwriter"

	"github.com/siyuan-note/siyuan/kernel/model"

	"github.com/spf13/cobra"
)

var templateCmd = &cobra.Command{
	Use:   "template",
	Short: "Manage templates",
}

var templateSearchCmd = &cobra.Command{
	Use:   "search [keyword]",
	Short: "Search templates (empty keyword lists all)",
	Args:  cobra.MaximumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		keyword := ""
		if len(args) > 0 {
			keyword = args[0]
		}
		results := model.SearchTemplate(keyword)
		switch outputFormat {
		case "json":
			data, _ := json.MarshalIndent(results, "", "  ")
			fmt.Println(string(data))
		default:
			if len(results) == 0 {
				fmt.Println("No templates found.")
				return nil
			}
			return writeTemplateSearchResults(os.Stdout, results)
		}
		return nil
	},
}

func writeTemplateSearchResults(output io.Writer, results []*model.TemplateSearchResult) error {
	w := tabwriter.NewWriter(output, 0, 0, 2, ' ', 0)
	if _, err := fmt.Fprintln(w, "NAME\tRELATIVE_PATH\tPATH"); err != nil {
		return err
	}
	for _, result := range results {
		if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", result.Content, result.RelativePath, result.Path); err != nil {
			return err
		}
	}
	if err := w.Flush(); err != nil {
		return err
	}
	_, err := fmt.Fprintf(output, "\n%d template(s)\n", len(results))
	return err
}

var templateGetCmd = &cobra.Command{
	Use:   "get --path <path>",
	Short: "Read template content",
	RunE: func(cmd *cobra.Command, args []string) error {
		p, _ := cmd.Flags().GetString("path")
		data, err := model.ReadTemplateFile(p)
		if err != nil {
			return err
		}
		fmt.Print(string(data))
		return nil
	},
}

var templateRemoveCmd = &cobra.Command{
	Use:   "remove --path <path>",
	Short: "Remove a template",
	RunE: func(cmd *cobra.Command, args []string) error {
		p, _ := cmd.Flags().GetString("path")
		abs, err := model.ResolveTemplatePath(p)
		if err != nil {
			return err
		}
		if dryRun {
			fmt.Printf("[dry-run] Would remove template: %s\n", p)
			return nil
		}
		if err := model.RemoveTemplate(abs); err != nil {
			return err
		}
		fmt.Println("ok")
		return nil
	},
}

var templateRenderCmd = &cobra.Command{
	Use:   "render --path <path> --id <id>",
	Short: "Render a template against a block (preview)",
	RunE: func(cmd *cobra.Command, args []string) error {
		p, _ := cmd.Flags().GetString("path")
		id, _ := cmd.Flags().GetString("id")
		if id == "" {
			return fmt.Errorf("--id is required")
		}
		abs, err := model.ResolveTemplatePath(p)
		if err != nil {
			return err
		}
		_, dom, err := model.RenderTemplate(abs, id, true)
		if err != nil {
			return err
		}
		fmt.Print(dom)
		return nil
	},
}

var templateSaveAsCmd = &cobra.Command{
	Use:   "save-as --id <id> --name <name>",
	Short: "Save a document as a template",
	RunE: func(cmd *cobra.Command, args []string) error {
		id, _ := cmd.Flags().GetString("id")
		name, _ := cmd.Flags().GetString("name")
		if id == "" {
			return fmt.Errorf("--id is required")
		}
		if name == "" {
			return fmt.Errorf("--name is required")
		}
		overwrite, _ := cmd.Flags().GetBool("overwrite")
		if dryRun {
			fmt.Printf("[dry-run] Would save document %s as template \"%s\"\n", id, name)
			return nil
		}
		code, err := model.DocSaveAsTemplate(id, name, overwrite)
		if err != nil {
			return err
		}
		if code == 1 {
			return fmt.Errorf("template already exists, use --overwrite to replace: %s", name)
		}
		fmt.Printf("%s.md\n", name)
		return nil
	},
}

var templateCreateCmd = &cobra.Command{
	Use:   "create --name <name> [--data <markdown> | --file <path>]",
	Short: "Create a template from markdown content",
	RunE: func(cmd *cobra.Command, args []string) error {
		name, _ := cmd.Flags().GetString("name")
		if name == "" {
			return fmt.Errorf("--name is required")
		}
		content, err := resolveData(cmd)
		if err != nil {
			return err
		}
		overwrite, _ := cmd.Flags().GetBool("overwrite")
		if dryRun {
			fmt.Printf("[dry-run] Would create template \"%s\"\n", name)
			return nil
		}
		code, err := model.CreateTemplate(name, content, overwrite)
		if err != nil {
			return err
		}
		if code == 1 {
			return fmt.Errorf("template already exists, use --overwrite to replace: %s", name)
		}
		fmt.Printf("%s.md\n", name)
		return nil
	},
}

func init() {
	templateGetCmd.Flags().String("path", "", "template path (absolute or relative to data/templates)")
	templateRemoveCmd.Flags().String("path", "", "template path (absolute or relative to data/templates)")
	templateRenderCmd.Flags().String("path", "", "template path (absolute or relative to data/templates)")
	templateRenderCmd.Flags().String("id", "", "block ID to render against")
	templateSaveAsCmd.Flags().String("id", "", "source document block ID")
	templateSaveAsCmd.Flags().String("name", "", "template name without extension")
	templateSaveAsCmd.Flags().Bool("overwrite", false, "overwrite if exists")
	templateCreateCmd.Flags().String("name", "", "template name without extension")
	templateCreateCmd.Flags().String("data", "", "markdown content")
	templateCreateCmd.Flags().String("file", "", "read content from file path (- for stdin)")
	templateCreateCmd.Flags().Bool("overwrite", false, "overwrite if exists")

	rootCmd.AddCommand(templateCmd)
	templateCmd.AddCommand(templateSearchCmd)
	templateCmd.AddCommand(templateGetCmd)
	templateCmd.AddCommand(templateRemoveCmd)
	templateCmd.AddCommand(templateRenderCmd)
	templateCmd.AddCommand(templateSaveAsCmd)
	templateCmd.AddCommand(templateCreateCmd)
}