This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
/
repo_delete.go
56 lines (48 loc) · 1.59 KB
/
repo_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
package commands
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/urfave/cli"
"github.com/victorgama/gh/utils"
"github.com/victorgama/go-octokit/octokit"
)
var rmRepoLogger = utils.Logger.WithExtra("rm")
// RmRepo exposes a command responsible for deleting a repository
var RmRepo = cli.Command{
Name: "rm",
Usage: "Destroys a repository",
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return fmt.Errorf("the 'rm' command requires one repository name")
}
client := utils.NewClient()
rmRepoLogger.Timing("One moment, please...")
r := utils.RepoURLFromString(c.Args()[0])
r.AutoComplete()
repo, resp := client.Repositories().One(&octokit.RepositoryURL, octokit.M{"owner": r.Username, "repo": r.RepoName})
utils.HandleClientError(resp, rmRepoLogger)
fmt.Println("Hey! You're about to perform a really dangerous action.")
fmt.Printf("To confirm you really want to delete %s, please enter its name again:\n", repo.FullName)
fmt.Print("What is its name again? ")
reader := bufio.NewReader(os.Stdin)
s, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
s = strings.TrimSpace(s)
s = strings.ToLower(s)
if s != strings.ToLower(repo.Name) {
fmt.Println("Nope. That's not its name. Aborting.")
return fmt.Errorf("aborted")
}
rmRepoLogger.Info("Removing %s...", repo.FullName)
success, resp := client.Repositories().Delete(&octokit.RepositoryURL, octokit.M{"owner": r.Username, "repo": r.RepoName})
if !success {
utils.HandleClientError(resp, rmRepoLogger)
}
rmRepoLogger.Success("Removed %s", repo.FullName)
return nil
},
}