group

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

Documentation

Index

Constants

This section is empty.

Variables

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

	// ListCmd represents the group list command
	ListCmd = &cobra.Command{
		Use:   "list",
		Short: "List groups",
		Long: `List groups in DocBase.

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

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

			groupList, err := c.Group.List(page, perPage)
			if err != nil {
				return err
			}

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

			if outputFormat == "text" {

				fmt.Printf("Count: %d\n", len(groupList.Groups))
				fmt.Println(strings.Repeat("-", 80))
				fmt.Printf("%-8s %s\n", "ID", "Name")
				fmt.Println(strings.Repeat("-", 80))

				for _, grp := range groupList.Groups {
					fmt.Printf("%-8d %s\n",
						grp.ID,
						format.Truncate(grp.Name, 37),
					)
				}

				if len(groupList.Groups) == perPage {
					fmt.Printf("\nUse --page %d to see the next page\n", page+1)
				}
				return nil
			}

			return f.Print(groupList)
		},
	}

	// ViewCmd represents the group view command
	ViewCmd = &cobra.Command{
		Use:   "view [id]",
		Short: "View a group",
		Long: `View a group in DocBase.

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

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

			grp, err := c.Group.Get(id)
			if err != nil {
				return err
			}

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

			if outputFormat == "text" {

				fmt.Printf("ID: %d\n", grp.ID)
				fmt.Printf("Name: %s\n", grp.Name)
				fmt.Printf("Created At: %s\n", grp.CreatedAt.Format("2006-01-02 15:04:05"))
				if grp.Description != "" {
					fmt.Printf("Description: %s\n", grp.Description)
				}
				fmt.Printf("Posts Count: %d\n", grp.PostsCount)
				if grp.LastActivityAt != nil {
					fmt.Printf("Last Activity At: %s\n", grp.LastActivityAt.Format("2006-01-02 15:04:05"))
				}
				fmt.Printf("Members: %d\n", len(grp.Users))
				return nil
			}

			return f.Print(grp)
		},
	}

	// MembersCmd represents the group members command
	MembersCmd = &cobra.Command{
		Use:   "members [id]",
		Short: "List members of a group",
		Long: `List members of a group in DocBase.

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

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

			members, err := c.Group.GetMembers(id)
			if err != nil {
				return err
			}

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

			if outputFormat == "text" {

				fmt.Printf("Group ID: %d\n", id)
				fmt.Printf("Total Members: %d\n", len(members))
				fmt.Println(strings.Repeat("-", 80))
				fmt.Printf("%-8s %-30s %-8s %s\n", "ID", "Name", "Admin", "Profile Image URL")
				fmt.Println(strings.Repeat("-", 80))

				for _, member := range members {
					fmt.Printf("%-8d %-30s %-8t %s\n",
						member.ID,
						format.Truncate(member.Name, 27),
						member.Admin,
						format.Truncate(member.ProfileImageURL, 40),
					)
				}
				return nil
			}

			return f.Print(members)
		},
	}
)

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