-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathdelete.go
97 lines (78 loc) · 2.1 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
package commands
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/github/hub/v2/github"
"github.com/github/hub/v2/ui"
"github.com/github/hub/v2/utils"
)
var cmdDelete = &Command{
Run: deleteRepo,
Usage: "delete [-y] [<ORGANIZATION>/]<NAME>",
Long: `Delete an existing repository on GitHub.
## Options:
-y, --yes
Skip the confirmation prompt and immediately delete the repository.
[<ORGANIZATION>/]<NAME>
The name for the repository on GitHub.
## Examples:
$ hub delete recipes
[ personal repo deleted on GitHub ]
$ hub delete sinatra/recipes
[ repo deleted in GitHub organization ]
## See also:
hub-init(1), hub(1)
`,
}
func init() {
CmdRunner.Use(cmdDelete)
}
func deleteRepo(command *Command, args *Args) {
var repoName string
if !args.IsParamsEmpty() {
repoName = args.FirstParam()
}
re := regexp.MustCompile(NameWithOwnerRe)
if !re.MatchString(repoName) {
utils.Check(command.UsageError(""))
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("deleting repository", err))
}
owner := host.User
if strings.Contains(repoName, "/") {
split := strings.SplitN(repoName, "/", 2)
owner, repoName = split[0], split[1]
}
project := github.NewProject(owner, repoName, host.Host)
gh := github.NewClient(project.Host)
if !args.Flag.Bool("--yes") {
ui.Printf("Really delete repository '%s' (yes/N)? ", project)
answer := ""
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
answer = strings.TrimSpace(scanner.Text())
}
utils.Check(scanner.Err())
if answer != "yes" {
utils.Check(fmt.Errorf("Please type 'yes' for confirmation."))
}
}
if args.Noop {
ui.Printf("Would delete repository '%s'.\n", project)
} else {
err = gh.DeleteRepository(project)
if err != nil && strings.Contains(err.Error(), "HTTP 403") {
ui.Errorf("Please edit the token used for hub at https://%s/settings/tokens\n", project.Host)
ui.Errorln("and verify that the `delete_repo` scope is enabled.")
}
utils.Check(err)
ui.Printf("Deleted repository '%s'.\n", project)
}
args.NoForward()
}