1package cmd
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/charmbracelet/soft-serve/server/backend"
  8	"github.com/charmbracelet/soft-serve/server/proto"
  9	"github.com/spf13/cobra"
 10)
 11
 12// RepoCommand returns a command for managing repositories.
 13func RepoCommand() *cobra.Command {
 14	cmd := &cobra.Command{
 15		Use:     "repo",
 16		Aliases: []string{"repos", "repository", "repositories"},
 17		Short:   "Manage repositories",
 18	}
 19
 20	cmd.AddCommand(
 21		blobCommand(),
 22		branchCommand(),
 23		collabCommand(),
 24		commitCommand(),
 25		createCommand(),
 26		deleteCommand(),
 27		descriptionCommand(),
 28		hiddenCommand(),
 29		importCommand(),
 30		listCommand(),
 31		mirrorCommand(),
 32		privateCommand(),
 33		projectName(),
 34		renameCommand(),
 35		tagCommand(),
 36		treeCommand(),
 37	)
 38
 39	cmd.AddCommand(
 40		&cobra.Command{
 41			Use:               "info REPOSITORY",
 42			Short:             "Get information about a repository",
 43			Args:              cobra.ExactArgs(1),
 44			PersistentPreRunE: checkIfReadable,
 45			RunE: func(cmd *cobra.Command, args []string) error {
 46				ctx := cmd.Context()
 47				be := backend.FromContext(ctx)
 48				rn := args[0]
 49				rr, err := be.Repository(ctx, rn)
 50				if err != nil {
 51					return err
 52				}
 53
 54				r, err := rr.Open()
 55				if err != nil {
 56					return err
 57				}
 58
 59				head, err := r.HEAD()
 60				if err != nil {
 61					return err
 62				}
 63
 64				var owner proto.User
 65				if rr.UserID() > 0 {
 66					owner, err = be.UserByID(ctx, rr.UserID())
 67					if err != nil {
 68						return err
 69					}
 70				}
 71
 72				branches, _ := r.Branches()
 73				tags, _ := r.Tags()
 74
 75				// project name and description are optional, handle trailing
 76				// whitespace to avoid breaking tests.
 77				cmd.Println(strings.TrimSpace(fmt.Sprint("Project Name: ", rr.ProjectName())))
 78				cmd.Println("Repository:", rr.Name())
 79				cmd.Println(strings.TrimSpace(fmt.Sprint("Description: ", rr.Description())))
 80				cmd.Println("Private:", rr.IsPrivate())
 81				cmd.Println("Hidden:", rr.IsHidden())
 82				cmd.Println("Mirror:", rr.IsMirror())
 83				if owner != nil {
 84					cmd.Println(strings.TrimSpace(fmt.Sprint("Owner: ", owner.Username())))
 85				}
 86				cmd.Println("Default Branch:", head.Name().Short())
 87				if len(branches) > 0 {
 88					cmd.Println("Branches:")
 89					for _, b := range branches {
 90						cmd.Println("  -", b)
 91					}
 92				}
 93				if len(tags) > 0 {
 94					cmd.Println("Tags:")
 95					for _, t := range tags {
 96						cmd.Println("  -", t)
 97					}
 98				}
 99
100				return nil
101			},
102		},
103	)
104
105	return cmd
106}