From 3e6e6fb7b1794d009c0e83550847e3a34c032671 Mon Sep 17 00:00:00 2001 From: "Mr. Is" Date: Tue, 2 Aug 2016 07:10:10 -0400 Subject: [PATCH] Migrate option. --- cmd/repo-migrate.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 26 +++++------- 2 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 cmd/repo-migrate.go diff --git a/cmd/repo-migrate.go b/cmd/repo-migrate.go new file mode 100644 index 0000000..cafb9c8 --- /dev/null +++ b/cmd/repo-migrate.go @@ -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 / ") + 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") +} diff --git a/cmd/root.go b/cmd/root.go index ec68b7f..333a4d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,23 +20,17 @@ var RootCmd = &cobra.Command{ Short: "Connect to the Gogs API.", 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*. - -You'll stick that into the default config file named below, -where you'll also want to *set your base Gogs url*. - - $HOME/.go-gogs-cli.yaml - -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: +$ gogs [repo|r] +$ gogs [create|c|new|n] + [migrate|m] + [list] + [search|find|s|f] + [delete|destroy|rid|d] +`, Run: func(cmd *cobra.Command, args []string) { token := viper.GetString("GOGS_TOKEN") if token != "" {