1package cmd
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/charmbracelet/lipgloss"
8 "github.com/charmbracelet/soft-serve/pkg/backend"
9 "github.com/charmbracelet/soft-serve/pkg/proto"
10 "github.com/spf13/cobra"
11)
12
13// RepoCommand returns a command for managing repositories.
14func RepoCommand(renderer *lipgloss.Renderer) *cobra.Command {
15 cmd := &cobra.Command{
16 Use: "repo",
17 Aliases: []string{"repos", "repository", "repositories"},
18 Short: "Manage repositories",
19 }
20
21 cmd.AddCommand(
22 blobCommand(renderer),
23 branchCommand(),
24 collabCommand(),
25 commitCommand(renderer),
26 createCommand(),
27 deleteCommand(),
28 descriptionCommand(),
29 hiddenCommand(),
30 importCommand(),
31 listCommand(),
32 mirrorCommand(),
33 privateCommand(),
34 projectName(),
35 renameCommand(),
36 tagCommand(),
37 treeCommand(),
38 webhookCommand(),
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}