提交 b2b3184b 编写于 作者: A Andreas Baumann

added a preliminary delete repository command (because I need it)

上级 944f3c74
......@@ -7,6 +7,7 @@ HELP_CMD = \
share/man/man1/hub-ci-status.1 \
share/man/man1/hub-compare.1 \
share/man/man1/hub-create.1 \
share/man/man1/hub-delete.1 \
share/man/man1/hub-fork.1 \
share/man/man1/hub-pr.1 \
share/man/man1/hub-pull-request.1 \
......
package commands
import (
"os"
"fmt"
"regexp"
"strings"
"github.com/github/hub/git"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
var cmdDelete = &Command{
Run: delete,
Usage: "delete [[<ORGANIZATION>/]<NAME>]",
Long: `Delete an existing repository on GitHub.
## Options:
[<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)
`,
}
var (
)
func init() {
CmdRunner.Use(cmdDelete)
}
func delete(command *Command, args *Args) {
var repoName string
if args.IsParamsEmpty() {
dirName, err := git.WorkdirName()
utils.Check(err)
repoName = github.SanitizeProjectName(dirName)
} else {
reg := regexp.MustCompile("^[^-]")
if !reg.MatchString(args.FirstParam()) {
err := fmt.Errorf("invalid argument: %s", args.FirstParam())
utils.Check(err)
}
repoName = args.FirstParam()
}
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 = split[0]
repoName = split[1]
}
project := github.NewProject(owner, repoName, host.Host)
gh := github.NewClient(project.Host)
if ! gh.IsRepositoryExist(project) {
fmt.Println("No such repository")
args.NoForward()
return
}
if os.Getenv("HUB_UNSAFE_DELETE") == "" {
fmt.Printf("Repository '%s' exists. Really delete it (y/N)?", repoName)
var s string
_, err = fmt.Scan(&s)
if err != nil {
fmt.Println(err);
args.NoForward()
return
}
s = strings.TrimSpace(s)
s = strings.ToLower(s)
if s != "y" {
fmt.Println("Abort: not deleting anything.")
args.NoForward()
return
}
fmt.Println("Please write the name of the repository again (this operation can not be undone!): ")
_, err = fmt.Scan(&s)
if err != nil {
fmt.Println(err);
args.NoForward()
return
}
s = strings.TrimSpace(s)
if s != repoName {
fmt.Println("Names don't match.. bailing out.. no deletion")
args.NoForward()
return
}
}
err = gh.DeleteRepository(project)
utils.Check(err)
fmt.Printf("Deleted repository %v\n", repoName)
args.NoForward()
}
......@@ -169,6 +169,7 @@ These GitHub commands are provided by hub:
ci-status Show the CI status of a commit
compare Open a compare page on GitHub
create Create this repository on GitHub and add GitHub as origin
delete Delete a repository on GitHub
fork Make a fork of a remote repository on GitHub and add as remote
issue List or create issues
pr Work with pull requests
......
......@@ -247,6 +247,27 @@ func (client *Client) CreateRepository(project *Project, description, homepage s
return
}
func (client *Client) DeleteRepository(project *Project) (err error) {
var repoURL string
if project.Owner != client.Host.User {
repoURL = fmt.Sprintf("/repos/%s/%s", project.Owner, project.Name)
} else {
repoURL = fmt.Sprintf("/repos/%s/%s", client.Host.User, project.Name)
}
api, err := client.simpleApi()
if err != nil {
return
}
res, err := api.Delete(repoURL);
if err = checkStatus(204, "deleting repository", res, err); err != nil {
return err
}
return err
}
type Release struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
......
......@@ -66,6 +66,9 @@ git but that are extended through hub, and custom ones that hub provides.
* hub-create(1):
Create a new repository on GitHub and add a git remote for it.
* hub-delete(1):
Delete a repository on Github.
* hub-fork(1):
Fork the current project on GitHub and add a git remote for it.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册