1package cmd
2
3import (
4 "strings"
5
6 "github.com/spf13/cobra"
7)
8
9func tagCommand() *cobra.Command {
10 cmd := &cobra.Command{
11 Use: "tag",
12 Short: "Manage repository tags",
13 }
14
15 cmd.AddCommand(
16 tagListCommand(),
17 tagDeleteCommand(),
18 )
19
20 return cmd
21}
22
23func tagListCommand() *cobra.Command {
24 cmd := &cobra.Command{
25 Use: "list REPOSITORY",
26 Aliases: []string{"ls"},
27 Short: "List repository tags",
28 Args: cobra.ExactArgs(1),
29 PersistentPreRunE: checkIfReadable,
30 RunE: func(cmd *cobra.Command, args []string) error {
31 cfg, _ := fromContext(cmd)
32 rn := strings.TrimSuffix(args[0], ".git")
33 rr, err := cfg.Backend.Repository(rn)
34 if err != nil {
35 return err
36 }
37
38 r, err := rr.Open()
39 if err != nil {
40 return err
41 }
42
43 tags, _ := r.Tags()
44 for _, t := range tags {
45 cmd.Println(t)
46 }
47
48 return nil
49 },
50 }
51
52 return cmd
53}
54
55func tagDeleteCommand() *cobra.Command {
56 cmd := &cobra.Command{
57 Use: "delete REPOSITORY TAG",
58 Aliases: []string{"remove", "rm", "del"},
59 Short: "Delete a tag",
60 Args: cobra.ExactArgs(2),
61 PersistentPreRunE: checkIfCollab,
62 RunE: func(cmd *cobra.Command, args []string) error {
63 cfg, _ := fromContext(cmd)
64 rn := strings.TrimSuffix(args[0], ".git")
65 rr, err := cfg.Backend.Repository(rn)
66 if err != nil {
67 return err
68 }
69
70 r, err := rr.Open()
71 if err != nil {
72 return err
73 }
74
75 return r.DeleteTag(args[1])
76 },
77 }
78
79 return cmd
80}