From 5ed77c996835102973d79d098e30093f5e787520 Mon Sep 17 00:00:00 2001 From: Parth Patel Date: Fri, 15 Oct 2021 19:10:05 -0400 Subject: [PATCH] made some progress, not done yet --- pkg/cmd/repo/rename/rename.go | 85 +++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/repo/rename/rename.go b/pkg/cmd/repo/rename/rename.go index 8c1410d83..4492bef06 100644 --- a/pkg/cmd/repo/rename/rename.go +++ b/pkg/cmd/repo/rename/rename.go @@ -3,16 +3,19 @@ package rename import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" "strings" + "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghinstance" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" + "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -20,8 +23,10 @@ type RenameOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams Config func() (config.Config, error) + BaseRepo func() (ghrepo.Interface, error) oldRepoSelector string newRepoSelector string + flagRepo bool } type renameRepo struct { @@ -34,24 +39,42 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co opts := &RenameOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, + BaseRepo: f.BaseRepo, } cmd := &cobra.Command{ - DisableFlagsInUseLine: true, - - Use: "rename ", + Use: "rename [-R] [] []", Short: "Rename a repository", - Long: "Rename a GitHub repository", - Args: cmdutil.ExactArgs(2, "cannot rename: repository argument required"), + Long: `Rename a GitHub repository + With no argument, the repository for the current directory is renamed using a prompt + With one argument, the repository of the current directory is renamed using the argument + With '-R', and two arguments the given repository is replaced with the new name `, + Args: cobra.MaximumNArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - opts.oldRepoSelector = args[0] - opts.newRepoSelector = args[1] + if len(args) > 0 { + if len(args) == 2 && opts.flagRepo { + opts.oldRepoSelector = args[0] + opts.newRepoSelector = args[1] + } else if len(args) == 1 && !opts.flagRepo { + opts.newRepoSelector = args[0] + } else { + return fmt.Errorf("check your parameters") + } + } else { + if !opts.IO.CanPrompt() { + return &cmdutil.FlagError{ + Err: errors.New("could not prompt: proceed with prompt or argument(s) required")} + } + } if runf != nil { return runf(opts) } return renameRun(opts) }, } + + cmd.Flags().BoolVarP(&opts.flagRepo, "repo", "R", false, "pass in two arguments to rename a repository") + return cmd } @@ -63,15 +86,49 @@ func renameRun(opts *RenameOptions) error { } apiClient := api.NewClientFromHTTP(httpClient) - oldRepoURL := opts.oldRepoSelector - if !strings.Contains(oldRepoURL, "/") { - currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default()) - if err != nil { - return err + var toRename ghrepo.Interface + oldRepoURL := "" + newRepoName := "" + + if !opts.flagRepo { + if opts.newRepoSelector != "" && opts.oldRepoSelector == "" { + newRepoName = opts.newRepoSelector + } else { + err = prompt.SurveyAskOne( + &survey.Input{ + Message: "Rename current repo to: ", + }, + &newRepoName, + ) + if err != nil { + return err + } + + toRename, err = opts.BaseRepo() + if err != nil { + return err + } + } + } else { + if opts.newRepoSelector != "" && opts.oldRepoSelector != "" { + oldRepoURL = opts.oldRepoSelector + newRepoName = opts.newRepoSelector + if !strings.Contains(oldRepoURL, "/") { + currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default()) + if err != nil { + return err + } + oldRepoURL = currentUser + "/" + oldRepoURL + } + } else { + return fmt.Errorf("check your params") } - oldRepoURL = currentUser + "/" + oldRepoURL } - newRepoName := opts.newRepoSelector + + fmt.Println(toRename) + fmt.Printf("Old: %s\n", oldRepoURL) + fmt.Printf("New: %s\n", newRepoName) + fmt.Println(opts.flagRepo) repo, err := ghrepo.FromFullName(oldRepoURL) if err != nil {