Add created gogs remote git url as git remote. 📯

This commit is contained in:
Mr. Is 2016-07-28 09:10:55 -04:00
parent b53cbe0174
commit d11905f9f3
2 changed files with 76 additions and 3 deletions

View File

@ -2,6 +2,8 @@ package cmd
import (
"fmt"
"os/exec"
"strings"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
@ -13,17 +15,23 @@ var repoDescription string
var repoIsPrivate bool
var orgName string
var repoRemoteName string
var createCmd = &cobra.Command{
Aliases: []string{"new"},
Aliases: []string{"new", "n", "c"},
Use: "create",
Short: "Create a repository",
Long: `create [my-new-repo | [-n | --name]] [-d | --desc] [-org | --org] [-p | --private]]
Long: `create [my-new-repo | [-n | --name]] [-d | --desc] [-org | --org] [-p | --private] [-r | --add-remote]]
$ 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
$ gogs repo new my-new-repo --private`,
$ 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`,
Run: func(cmd *cobra.Command, args []string) {
// accept gogs repo create thingeys || gogs repo create -name=thingeys
@ -53,6 +61,69 @@ var createCmd = &cobra.Command{
fmt.Println("Repo created! Woohoo!")
printRepo(repo)
// 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))
}
}
},
}
@ -63,4 +134,6 @@ func init() {
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")
createCmd.Flags().StringVarP(&repoRemoteName, "add-remote", "r", "", "remote name")
}

BIN
gogs

Binary file not shown.