repo.go

  1package cmd
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/charmbracelet/soft-serve/pkg/backend"
  8	"github.com/charmbracelet/soft-serve/pkg/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		webhookCommand(),
 38	)
 39
 40	cmd.AddCommand(
 41		&cobra.Command{
 42			Use:               "info REPOSITORY",
 43			Short:             "Get information about a repository",
 44			Args:              cobra.ExactArgs(1),
 45			PersistentPreRunE: checkIfReadable,
 46			RunE: func(cmd *cobra.Command, args []string) error {
 47				ctx := cmd.Context()
 48				be := backend.FromContext(ctx)
 49				rn := args[0]
 50				rr, err := be.Repository(ctx, rn)
 51				if err != nil {
 52					return err //nolint:wrapcheck
 53				}
 54
 55				r, err := rr.Open()
 56				if err != nil {
 57					return err //nolint:wrapcheck
 58				}
 59
 60				head, err := r.HEAD()
 61				if err != nil {
 62					return err //nolint:wrapcheck
 63				}
 64
 65				var owner proto.User
 66				if rr.UserID() > 0 {
 67					owner, err = be.UserByID(ctx, rr.UserID())
 68					if err != nil {
 69						return err //nolint:wrapcheck
 70					}
 71				}
 72
 73				branches, _ := r.Branches()
 74				tags, _ := r.Tags()
 75
 76				// project name and description are optional, handle trailing
 77				// whitespace to avoid breaking tests.
 78				cmd.Println(strings.TrimSpace(fmt.Sprint("Project Name: ", rr.ProjectName())))
 79				cmd.Println("Repository:", rr.Name())
 80				cmd.Println(strings.TrimSpace(fmt.Sprint("Description: ", rr.Description())))
 81				cmd.Println("Private:", rr.IsPrivate())
 82				cmd.Println("Hidden:", rr.IsHidden())
 83				cmd.Println("Mirror:", rr.IsMirror())
 84				if owner != nil {
 85					cmd.Println(strings.TrimSpace(fmt.Sprint("Owner: ", owner.Username())))
 86				}
 87				cmd.Println("Default Branch:", head.Name().Short())
 88				if len(branches) > 0 {
 89					cmd.Println("Branches:")
 90					for _, b := range branches {
 91						cmd.Println("  -", b)
 92					}
 93				}
 94				if len(tags) > 0 {
 95					cmd.Println("Tags:")
 96					for _, t := range tags {
 97						cmd.Println("  -", t)
 98					}
 99				}
100
101				return nil
102			},
103		},
104	)
105
106	return cmd
107}