/
delete_repo.go
59 lines (50 loc) · 1.25 KB
/
delete_repo.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
package cmd
import (
"context"
"fmt"
"github.com/alok87/github-cli/pkg/utils"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
)
// DeleteRepoOptions holds options for deleting a repo.
type DeleteRepoOptions struct {
Name string
IsPrivate string
}
var deleteRepoOptions = &DeleteRepoOptions{}
var deleteRepoCmd = &cobra.Command{
Use: "repo [name]",
Short: "Delete repo",
Long: `Deletes a Github repo.`,
Run: func(cmd *cobra.Command, args []string) {
err := runDeleteRepo(cmd, args, deleteRepoOptions)
if err != nil {
exitWithError(err)
}
},
}
func runDeleteRepo(cmd *cobra.Command, args []string, o *DeleteRepoOptions) error {
if len(args) != 1 {
return cmd.Help()
}
repoName := args[0]
ctx := context.Background()
client, err := gc.GetClient(ctx)
if err != nil {
return err
}
user := gc.User
repoURL := user + "/" + repoName
c := utils.AskForConfirmation("Are you sure you want to delete " + repoURL + " ?")
if c {
_, err := client.Repositories.Delete(ctx, user, repoName)
if err != nil {
if err.(*github.ErrorResponse).Response.StatusCode == 404 {
return fmt.Errorf("Repo %s does not exist", repoName)
}
return err
}
fmt.Printf("Repo %s deleted in github.\n", repoName)
}
return nil
}