comment

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: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CommentCmd represents the comment command
	CommentCmd = &cobra.Command{
		Use:   "comment",
		Short: "Manage comments",
		Long:  `Manage comments in DocBase.`,
	}

	// ListCmd represents the comment list command
	ListCmd = &cobra.Command{
		Use:   "list [memo_id]",
		Short: "List comments for a memo",
		Long: `List comments for a memo in DocBase.

Example:
  docbase comment list 12345
  docbase comment list 12345 --page 2 --per-page 20`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := client.Create(cmd)
			if err != nil {
				return err
			}

			memoID, err := strconv.Atoi(args[0])
			if err != nil {
				return fmt.Errorf("invalid memo ID: %s", args[0])
			}

			page, _ := cmd.Flags().GetInt("page")
			perPage, _ := cmd.Flags().GetInt("per-page")

			commentList, err := c.Comment.List(memoID, page, perPage)
			if err != nil {
				return err
			}

			outputFormat, _ := cmd.Flags().GetString("format")
			f := formatter.NewFormatter(outputFormat, os.Stdout, true)

			if outputFormat == "text" {

				fmt.Printf("Memo ID: %d\n", memoID)
				fmt.Printf("Total Comments: %d\n", commentList.Meta.Total)
				fmt.Println(strings.Repeat("-", 80))

				for _, cmt := range commentList.Comments {
					fmt.Printf("ID: %d\n", cmt.ID)
					fmt.Printf("Author: %s\n", cmt.User.Name)
					fmt.Printf("Created At: %s\n", cmt.CreatedAt.Format("2006-01-02 15:04:05"))
					fmt.Println(strings.Repeat("-", 40))
					fmt.Println(cmt.Body)
					fmt.Println(strings.Repeat("-", 80))
				}

				if commentList.Meta.NextPage != nil {
					nextPage, _ := strconv.Atoi(*commentList.Meta.NextPage)
					fmt.Printf("\nUse --page %d to see the next page\n", nextPage)
				}
				return nil
			}

			return f.Print(commentList)
		},
	}

	// CreateCmd represents the comment create command
	CreateCmd = &cobra.Command{
		Use:   "create [memo_id]",
		Short: "Create a comment",
		Long: `Create a comment for a memo in DocBase.

Example:
  docbase comment create 12345 --body "This is a comment"
  docbase comment create 12345 --body-file comment.md`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := client.Create(cmd)
			if err != nil {
				return err
			}

			memoID, err := strconv.Atoi(args[0])
			if err != nil {
				return fmt.Errorf("invalid memo ID: %s", args[0])
			}

			body, _ := cmd.Flags().GetString("body")
			bodyFile, _ := cmd.Flags().GetString("body-file")
			notify, _ := cmd.Flags().GetBool("notify")

			if body == "" && bodyFile == "" {
				return fmt.Errorf("either body or body-file is required")
			}

			if body == "" && bodyFile != "" {
				var err error
				body, err = fileio.Read(bodyFile)
				if err != nil {
					return err
				}
			}

			req := &docbase.CreateCommentRequest{
				Body:   body,
				Notify: notify,
			}

			cmt, err := c.Comment.Create(memoID, req)
			if err != nil {
				return err
			}

			fmt.Println(color.GreenString("Comment created successfully"))
			fmt.Printf("ID: %d\n", cmt.ID)
			return nil
		},
	}

	// DeleteCmd represents the comment delete command
	DeleteCmd = &cobra.Command{
		Use:   "delete [comment_id]",
		Short: "Delete a comment",
		Long: `Delete a comment from DocBase.

Example:
  docbase comment delete 67890`,
		Args: cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			c, err := client.Create(cmd)
			if err != nil {
				return err
			}

			commentID, err := strconv.Atoi(args[0])
			if err != nil {
				return fmt.Errorf("invalid comment ID: %s", args[0])
			}

			force, _ := cmd.Flags().GetBool("force")
			if !force {
				fmt.Printf("Are you sure you want to delete comment %d? (y/N): ", commentID)
				var confirm string
				_, _ = fmt.Scanln(&confirm)
				if strings.ToLower(confirm) != "y" {
					fmt.Println("Deletion canceled")
					return nil
				}
			}

			if err := c.Comment.Delete(commentID); err != nil {
				return err
			}

			fmt.Println(color.GreenString("Comment deleted successfully"))
			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