2016-07-27 22:06:32 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-28 13:10:55 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2016-07-27 22:06:32 +00:00
|
|
|
|
|
|
|
"github.com/gogits/go-gogs-client"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags.
|
|
|
|
var repoName string
|
|
|
|
var repoDescription string
|
|
|
|
var repoIsPrivate bool
|
|
|
|
var orgName string
|
|
|
|
|
2016-07-28 13:10:55 +00:00
|
|
|
var repoRemoteName string
|
|
|
|
|
2016-07-27 22:06:32 +00:00
|
|
|
var createCmd = &cobra.Command{
|
2016-07-28 13:10:55 +00:00
|
|
|
Aliases: []string{"new", "n", "c"},
|
2016-07-28 00:11:57 +00:00
|
|
|
Use: "create",
|
|
|
|
Short: "Create a repository",
|
2016-07-28 13:10:55 +00:00
|
|
|
Long: `create [my-new-repo | [-n | --name]] [-d | --desc] [-org | --org] [-p | --private] [-r | --add-remote]]
|
2016-07-28 00:11:57 +00:00
|
|
|
|
|
|
|
$ gogs repo create my-new-repo
|
|
|
|
$ gogs repo new my-new-repo
|
|
|
|
$ gogs repo create -n=my-new-repo
|
|
|
|
$ gogs repo create my-new-repo --desc="a thing with things" --org=JustUsGuys -p=true
|
2016-07-28 13:10:55 +00:00
|
|
|
$ gogs repo new my-new-repo --private
|
|
|
|
|
|
|
|
Add your new repo as a remote to your working dir:
|
|
|
|
|
|
|
|
$ gogs repo create my-new-repo --add-remote=origin`,
|
2016-07-27 22:06:32 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
// accept gogs repo create thingeys || gogs repo create -name=thingeys
|
|
|
|
if repoName == "" {
|
|
|
|
repoName = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
createRepoOpts := gogs.CreateRepoOption{
|
|
|
|
Name: repoName,
|
|
|
|
Description: repoDescription,
|
|
|
|
Private: repoIsPrivate,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var repo *gogs.Repository
|
|
|
|
|
|
|
|
if orgName == "" {
|
|
|
|
repo, err = GetClient().CreateRepo(createRepoOpts)
|
|
|
|
} else {
|
|
|
|
repo, err = GetClient().CreateOrgRepo(orgName, createRepoOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Repo created! Woohoo!")
|
|
|
|
printRepo(repo)
|
|
|
|
|
2016-07-28 13:10:55 +00:00
|
|
|
// add git url as remote to working dir
|
|
|
|
if repoRemoteName != "" {
|
|
|
|
// get git path
|
|
|
|
getGitComm := exec.Command("/usr/bin/which", "git")
|
|
|
|
whichGit, err := getGitComm.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println("...You have git installed, right?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
whichGitClean := strings.Replace(string(whichGit), "\n", "", 1)
|
|
|
|
|
|
|
|
gitAddRemoteComm := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneUrl)
|
|
|
|
_, err = gitAddRemoteComm.Output()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// go a step further and let's try init-ing the repo
|
|
|
|
//
|
|
|
|
var gitInitDone = make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
gitInitComm := exec.Command(whichGitClean, "init")
|
|
|
|
gitInit, initErr := gitInitComm.Output()
|
|
|
|
if initErr != nil {
|
|
|
|
fmt.Println(initErr)
|
|
|
|
} else {
|
|
|
|
fmt.Println(string(gitInit))
|
|
|
|
}
|
|
|
|
gitInitDone <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for gitInitDone
|
|
|
|
select {
|
|
|
|
case <-gitInitDone:
|
|
|
|
// Apparently exec can only call any given command once.
|
|
|
|
// https://github.com/golang/go/issues/10305
|
|
|
|
gitAddRemoteComm2 := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneUrl)
|
|
|
|
_, err = gitAddRemoteComm2.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error adding remote -- ", err.Error())
|
|
|
|
} else {
|
|
|
|
gitShowRemotesCommand := exec.Command(whichGitClean, "remote", "-v")
|
|
|
|
gitShowRemotes, err := gitShowRemotesCommand.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(string(gitShowRemotes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// else there was git already and remote was added
|
|
|
|
// fmt.Println(string(addRemote)) // gotcha: adding a remote success returns ""
|
|
|
|
// fmt.Println("Remote added as " + repoRemoteName)
|
|
|
|
gitShowRemotesCommand2 := exec.Command(whichGitClean, "remote", "-v")
|
|
|
|
gitShowRemotes2, err := gitShowRemotesCommand2.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(gitShowRemotes2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 22:06:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
repoCmd.AddCommand(createCmd)
|
|
|
|
|
|
|
|
createCmd.Flags().StringVarP(&repoName, "name", "n", "", "repo name")
|
|
|
|
createCmd.Flags().StringVarP(&repoDescription, "desc", "d", "", "repo description")
|
|
|
|
createCmd.Flags().BoolVarP(&repoIsPrivate, "private", "p", false, "repo is private")
|
|
|
|
createCmd.Flags().StringVarP(&orgName, "org", "o", "", "organization")
|
2016-07-28 13:10:55 +00:00
|
|
|
|
|
|
|
createCmd.Flags().StringVarP(&repoRemoteName, "add-remote", "r", "", "remote name")
|
2016-07-27 22:06:32 +00:00
|
|
|
}
|