/
delete.go
executable file
·98 lines (79 loc) · 2.26 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package label
import (
"fmt"
"net/http"
"github.com/ungtb10d/cli/v2/api"
"github.com/ungtb10d/cli/v2/internal/ghrepo"
"github.com/ungtb10d/cli/v2/pkg/cmdutil"
"github.com/ungtb10d/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)
type iprompter interface {
ConfirmDeletion(string) error
}
type deleteOptions struct {
BaseRepo func() (ghrepo.Interface, error)
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Prompter iprompter
Name string
Confirmed bool
}
func newCmdDelete(f *cmdutil.Factory, runF func(*deleteOptions) error) *cobra.Command {
opts := deleteOptions{
HttpClient: f.HttpClient,
IO: f.IOStreams,
Prompter: f.Prompter,
}
cmd := &cobra.Command{
Use: "delete <name>",
Short: "Delete a label from a repository",
Args: cmdutil.ExactArgs(1, "cannot delete label: name argument required"),
RunE: func(c *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
opts.Name = args[0]
if !opts.IO.CanPrompt() && !opts.Confirmed {
return cmdutil.FlagErrorf("--confirm required when not running interactively")
}
if runF != nil {
return runF(&opts)
}
return deleteRun(&opts)
},
}
cmd.Flags().BoolVar(&opts.Confirmed, "confirm", false, "Confirm deletion without prompting")
return cmd
}
func deleteRun(opts *deleteOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
baseRepo, err := opts.BaseRepo()
if err != nil {
return err
}
if !opts.Confirmed {
if err := opts.Prompter.ConfirmDeletion(opts.Name); err != nil {
return err
}
}
opts.IO.StartProgressIndicator()
err = deleteLabel(httpClient, baseRepo, opts.Name)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
cs := opts.IO.ColorScheme()
successMsg := fmt.Sprintf("%s Label %q deleted from %s\n", cs.SuccessIcon(), opts.Name, ghrepo.FullName(baseRepo))
fmt.Fprint(opts.IO.Out, successMsg)
}
return nil
}
func deleteLabel(client *http.Client, repo ghrepo.Interface, name string) error {
apiClient := api.NewClientFromHTTP(client)
path := fmt.Sprintf("repos/%s/%s/labels/%s", repo.RepoOwner(), repo.RepoName(), name)
return apiClient.REST(repo.RepoHost(), "DELETE", path, nil, nil)
}