soft-serve

git clone git://git.lin.moe/fork/soft-serve.git

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