import_cmd

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 11 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 {
			client, err := utils.CreateClient(cmd)
			if err != nil {
				return err
			}

			filePath := args[0]
			groupNames, _ := cmd.Flags().GetStringSlice("group")
			tagNames, _ := cmd.Flags().GetStringSlice("tag")
			draft, _ := cmd.Flags().GetBool("draft")
			scope, _ := cmd.Flags().GetString("scope")
			notify, _ := cmd.Flags().GetBool("notify")

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

			// Parse file
			var title, body string
			ext := strings.ToLower(filepath.Ext(filePath))

			switch ext {
			case ".md":
				title, body, err = parseMdFile(content)
				if err != nil {
					return err
				}
			case ".json":
				title, body, err = parseJsonFile(content)
				if err != nil {
					return err
				}
			default:
				return fmt.Errorf("unsupported file format: %s", ext)
			}

			// Get group IDs
			var groupIDs []int
			if len(groupNames) > 0 {
				groups, err := client.Group.List(1, 100)
				if err != nil {
					return err
				}

				groupMap := make(map[string]int)
				for _, group := range groups.Groups {
					groupMap[group.Name] = group.ID
				}

				for _, name := range groupNames {
					id, ok := groupMap[name]
					if !ok {
						return fmt.Errorf("group not found: %s", name)
					}
					groupIDs = append(groupIDs, id)
				}
			}

			req := &docbase.CreateMemoRequest{
				Title:  title,
				Body:   body,
				Draft:  draft,
				Tags:   tagNames,
				Scope:  scope,
				Groups: groupIDs,
				Notify: notify,
			}

			memo, err := client.Memo.Create(req)
			if 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 {
			client, err := utils.CreateClient(cmd)
			if err != nil {
				return err
			}

			dirPath := args[0]
			groupNames, _ := cmd.Flags().GetStringSlice("group")
			tagNames, _ := cmd.Flags().GetStringSlice("tag")
			draft, _ := cmd.Flags().GetBool("draft")
			scope, _ := cmd.Flags().GetString("scope")
			notify, _ := cmd.Flags().GetBool("notify")
			limit, _ := cmd.Flags().GetInt("limit")

			// Get group IDs
			var groupIDs []int
			if len(groupNames) > 0 {
				groups, err := client.Group.List(1, 100)
				if err != nil {
					return err
				}

				groupMap := make(map[string]int)
				for _, group := range groups.Groups {
					groupMap[group.Name] = group.ID
				}

				for _, name := range groupNames {
					id, ok := groupMap[name]
					if !ok {
						return fmt.Errorf("group not found: %s", name)
					}
					groupIDs = append(groupIDs, id)
				}
			}

			files, err := ioutil.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))

			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 := ioutil.ReadFile(filePath)
				if err != nil {
					fmt.Printf("Error reading file %s: %v, skipping\n", filePath, err)
					continue
				}

				// Parse file
				var title, body string
				ext := strings.ToLower(filepath.Ext(filePath))

				switch ext {
				case ".md":
					title, body, err = parseMdFile(content)
					if err != nil {
						fmt.Printf("Error parsing file %s: %v, skipping\n", filePath, err)
						continue
					}
				case ".json":
					title, body, err = parseJsonFile(content)
					if err != nil {
						fmt.Printf("Error parsing file %s: %v, skipping\n", filePath, err)
						continue
					}
				default:
					fmt.Printf("Unsupported file format: %s, skipping\n", ext)
					continue
				}

				req := &docbase.CreateMemoRequest{
					Title:  title,
					Body:   body,
					Draft:  draft,
					Tags:   tagNames,
					Scope:  scope,
					Groups: groupIDs,
					Notify: notify,
				}

				memo, err := client.Memo.Create(req)
				if err != nil {
					fmt.Printf("Error creating 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