Migrate option.

This commit is contained in:
Mr. Is 2016-08-02 07:10:10 -04:00
parent 6093c4e53a
commit 3e6e6fb7b1
2 changed files with 106 additions and 16 deletions

96
cmd/repo-migrate.go Normal file
View File

@ -0,0 +1,96 @@
package cmd
import (
"fmt"
"strings"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
)
// Flags.
var migrateMirror bool
var migratePrivate bool
func unusableUsage() {
fmt.Println("Please argue me <user|organization>/<name-of-new-repo> <http://url.of.cloneable.repo.git>")
return
}
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Aliases: []string{"m"},
Use: "migrate, m",
Short: "Migrate a repository from a given remote source.",
Long: `Usage:
$ gogs repo migrate irstacks/copycat https://github.com/gogits/gogs.git
$ gogs repo migrate myCompany/copycat https://github.com/gogits/gogs.git
Options:
[-m | --mirror] If to be mirrored repo. (no arguments),
[-p | --private] If to be private repo. (no args)
`,
Run: func(cmd *cobra.Command, args []string) {
var opts gogs.MigrateRepoOption
if len(args) != 2 {
unusableUsage()
return
}
ownerSlashReponame := args[0]
migrateUrl := args[1]
// irstacks/my-copy-cat --> [irstacks, my-copy-cat]
ownernameReponame := strings.Split(ownerSlashReponame, "/")
if len(ownernameReponame) != 2 {
unusableUsage()
return
}
// get "userId" for owner name from extracurricular function...
// if the user return err, then we'll need to check if an organization was intended.
// this is an inconvenience by gogs api client. they should more explicity specify ownership
// in the migrateRepoOptions.
user, err := getUserByName(ownernameReponame[0])
if err != nil {
// I don't think it actually ever goes here...
// which means getUserByName works as well for orgs. huh.
fmt.Println(err)
fmt.Println("Searching for an org by than name...")
org, oerr := GetClient().GetOrg(ownernameReponame[0])
if oerr != nil {
fmt.Println("Could find neither user nor org by by the name: ", ownernameReponame[0])
fmt.Println(err)
return
} else {
opts.UID = int(org.ID)
}
} else {
opts.UID = int(user.ID)
}
opts.CloneAddr = migrateUrl
opts.RepoName = ownernameReponame[1]
opts.Mirror = migrateMirror
opts.Private = migratePrivate
repo, err := GetClient().MigrateRepo(opts)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Repo migrated! Woohoo!")
printRepo(repo)
},
}
func init() {
repoCmd.AddCommand(migrateCmd)
migrateCmd.Flags().BoolVarP(&migrateMirror, "mirror", "m", false, " make your migrated repo a mirror of the original")
migrateCmd.Flags().BoolVarP(&migratePrivate, "private", "p", false, " make your migrated repo private")
}

View File

@ -20,23 +20,17 @@ var RootCmd = &cobra.Command{
Short: "Connect to the Gogs API.", Short: "Connect to the Gogs API.",
Long: `Welcome to the Gogs CLI. Long: `Welcome to the Gogs CLI.
You'll probably, almost certainly, want a token for interacting with private data. $ gogs
--config path to config file (default $HOME/.go-gogs-cli.yaml; or you can set GOGS_TOKEN and GOGS_URL env vars)
--config="$HOME/.my-own-gogs-cli-file.yaml"
Visit your Profile Settings on Gogs and *create a token*. $ gogs [repo|r]
$ gogs [create|c|new|n]
You'll stick that into the default config file named below, [migrate|m]
where you'll also want to *set your base Gogs url*. [list]
[search|find|s|f]
$HOME/.go-gogs-cli.yaml [delete|destroy|rid|d]
`,
Recap:
- get a token from the Gogs UI Profile/settings page.
- put the token into the config file name above, along with
- the base url for your Gogs instance.
There's an example .go-gogs-cli.yaml`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
token := viper.GetString("GOGS_TOKEN") token := viper.GetString("GOGS_TOKEN")
if token != "" { if token != "" {