import_cmd

package
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ImportCmd represents the import command
	ImportCmd = &cobra.Command{
		Use:   "import",
		Short: "Import memos",
		Long:  `Import memos to DocBase from local files.`,
	}

	// FileCmd represents the import file command
	FileCmd = &cobra.Command{
		Use:   "file [file_path]",
		Short: "Import a memo from a file",
		Long: `Import a memo to DocBase from a local file.

Example:
  docbase import file ./memo.md
  docbase import file ./memo.json --group "全員" --tag "週報"`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := client.Create(cmd)
			if err != nil {
				return err
			}

			filePath := args[0]
			opts, err := getImportOptions(cmd)
			if err != nil {
				return err
			}

			content, err := os.ReadFile(filePath)
			if err != nil {
				return fmt.Errorf("failed to read file: %w", err)
			}

			data, err := parseImportFile(filePath, content)
			if err != nil {
				return err
			}

			var groupMap map[string]int
			if opts.groupNamesChanged && len(opts.groupNames) > 0 {
				groupMap, err = groups.BuildNameToIDMap(c)
				if err != nil {
					return err
				}
				opts.fixedGroupIDs, err = groups.ResolveIDsFromMap(groupMap, dedupeStrings(normalizeStringSlice(opts.groupNames)))
				if err != nil {
					return err
				}
			}

			req, _, err := buildCreateMemoRequest(c, opts, data, groupMap)
			if err != nil {
				return err
			}

			memo, err := c.Memo.Create(req)
			if err != nil {
				return err
			}

			if data.Archived != nil && *data.Archived {
				if err := c.Memo.Archive(memo.ID); err != nil {
					return err
				}
			}

			fmt.Println(color.GreenString("Memo imported successfully"))
			fmt.Printf("ID: %d\n", memo.ID)
			fmt.Printf("URL: %s\n", memo.URL)

			return nil
		},
	}

	// DirCmd represents the import dir command
	DirCmd = &cobra.Command{
		Use:   "dir [dir_path]",
		Short: "Import memos from a directory",
		Long: `Import memos to DocBase from a directory.

Example:
  docbase import dir ./exports
  docbase import dir ./exports --group "全員" --tag "週報"`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := client.Create(cmd)
			if err != nil {
				return err
			}

			dirPath := args[0]
			opts, err := getImportOptions(cmd)
			if err != nil {
				return err
			}
			limit, _ := cmd.Flags().GetInt("limit")

			files, err := os.ReadDir(dirPath)
			if err != nil {
				return fmt.Errorf("failed to read directory: %w", err)
			}

			// Filter files by extension
			var validFiles []string
			for _, file := range files {
				if file.IsDir() {
					continue
				}

				ext := strings.ToLower(filepath.Ext(file.Name()))
				if ext == ".md" || ext == ".json" {
					validFiles = append(validFiles, filepath.Join(dirPath, file.Name()))
				}
			}

			fmt.Printf("Found %d valid files\n", len(validFiles))

			var groupMap map[string]int
			if opts.groupNamesChanged && len(opts.groupNames) > 0 {
				groupMap, err = groups.BuildNameToIDMap(c)
				if err != nil {
					return err
				}
				opts.fixedGroupIDs, err = groups.ResolveIDsFromMap(groupMap, dedupeStrings(normalizeStringSlice(opts.groupNames)))
				if err != nil {
					return err
				}
			}

			count := 0
			for _, filePath := range validFiles {
				if limit > 0 && count >= limit {
					fmt.Println("Reached limit, stopping import")
					break
				}

				fmt.Printf("Importing %s...\n", filePath)

				content, err := os.ReadFile(filePath)
				if err != nil {
					fmt.Printf("Error reading file %s: %v, skipping\n", filePath, err)
					continue
				}

				data, err := parseImportFile(filePath, content)
				if err != nil {
					fmt.Printf("Error parsing file %s: %v, skipping\n", filePath, err)
					continue
				}

				var req *docbase.CreateMemoRequest
				req, groupMap, err = buildCreateMemoRequest(c, opts, data, groupMap)
				if err != nil {
					fmt.Printf("Error building request for file %s: %v, skipping\n", filePath, err)
					continue
				}

				memo, err := c.Memo.Create(req)
				if err != nil {
					fmt.Printf("Error creating memo from file %s: %v, skipping\n", filePath, err)
					continue
				}

				if data.Archived != nil && *data.Archived {
					if err := c.Memo.Archive(memo.ID); err != nil {
						fmt.Printf("Error archiving memo from file %s: %v, skipping\n", filePath, err)
						continue
					}
				}

				fmt.Printf("Imported memo ID: %d, URL: %s\n", memo.ID, memo.URL)
				count++
			}

			fmt.Println(color.GreenString("Import completed successfully"))
			fmt.Printf("Imported %d memos\n", count)

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL