repo.go

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