|
| 1 | +package rename |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/AlecAivazis/survey/v2" |
| 8 | + "github.com/MakeNowJust/heredoc" |
| 9 | + "github.com/cli/cli/v2/context" |
| 10 | + "github.com/cli/cli/v2/git" |
| 11 | + "github.com/cli/cli/v2/internal/config" |
| 12 | + "github.com/cli/cli/v2/internal/ghrepo" |
| 13 | + "github.com/cli/cli/v2/pkg/cmdutil" |
| 14 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 15 | + "github.com/cli/cli/v2/pkg/prompt" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +type RenameOptions struct { |
| 20 | + HttpClient func() (*http.Client, error) |
| 21 | + IO *iostreams.IOStreams |
| 22 | + Config func() (config.Config, error) |
| 23 | + BaseRepo func() (ghrepo.Interface, error) |
| 24 | + Remotes func() (context.Remotes, error) |
| 25 | + DoConfirm bool |
| 26 | + HasRepoOverride bool |
| 27 | + newRepoSelector string |
| 28 | +} |
| 29 | + |
| 30 | +func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Command { |
| 31 | + opts := &RenameOptions{ |
| 32 | + IO: f.IOStreams, |
| 33 | + HttpClient: f.HttpClient, |
| 34 | + Remotes: f.Remotes, |
| 35 | + Config: f.Config, |
| 36 | + } |
| 37 | + |
| 38 | + var confirm bool |
| 39 | + |
| 40 | + cmd := &cobra.Command{ |
| 41 | + Use: "rename [<new-name>]", |
| 42 | + Short: "Rename a repository", |
| 43 | + Long: heredoc.Doc(`Rename a GitHub repository |
| 44 | +
|
| 45 | + By default, this renames the current repository; otherwise renames the specified repository.`), |
| 46 | + Args: cobra.MaximumNArgs(1), |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + opts.BaseRepo = f.BaseRepo |
| 49 | + opts.HasRepoOverride = cmd.Flags().Changed("repo") |
| 50 | + |
| 51 | + if len(args) > 0 { |
| 52 | + opts.newRepoSelector = args[0] |
| 53 | + } else if !opts.IO.CanPrompt() { |
| 54 | + return cmdutil.FlagErrorf("new name argument required when not running interactively") |
| 55 | + } |
| 56 | + |
| 57 | + if len(args) == 1 && !confirm && !opts.HasRepoOverride { |
| 58 | + if !opts.IO.CanPrompt() { |
| 59 | + return cmdutil.FlagErrorf("--confirm required when passing a single argument") |
| 60 | + } |
| 61 | + opts.DoConfirm = true |
| 62 | + } |
| 63 | + |
| 64 | + if runf != nil { |
| 65 | + return runf(opts) |
| 66 | + } |
| 67 | + return renameRun(opts) |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + cmdutil.EnableRepoOverride(cmd, f) |
| 72 | + cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "skip confirmation prompt") |
| 73 | + |
| 74 | + return cmd |
| 75 | +} |
| 76 | + |
| 77 | +func renameRun(opts *RenameOptions) error { |
| 78 | + httpClient, err := opts.HttpClient() |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + newRepoName := opts.newRepoSelector |
| 84 | + |
| 85 | + currRepo, err := opts.BaseRepo() |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + if newRepoName == "" { |
| 91 | + err = prompt.SurveyAskOne( |
| 92 | + &survey.Input{ |
| 93 | + Message: fmt.Sprintf("Rename %s to: ", ghrepo.FullName(currRepo)), |
| 94 | + }, |
| 95 | + &newRepoName, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if opts.DoConfirm { |
| 103 | + var confirmed bool |
| 104 | + p := &survey.Confirm{ |
| 105 | + Message: fmt.Sprintf("Rename %s to %s?", ghrepo.FullName(currRepo), newRepoName), |
| 106 | + Default: false, |
| 107 | + } |
| 108 | + err = prompt.SurveyAskOne(p, &confirmed) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to prompt: %w", err) |
| 111 | + } |
| 112 | + if !confirmed { |
| 113 | + return nil |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + newRepo, err := apiRename(httpClient, currRepo, newRepoName) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + cs := opts.IO.ColorScheme() |
| 123 | + if opts.IO.IsStdoutTTY() { |
| 124 | + fmt.Fprintf(opts.IO.Out, "%s Renamed repository %s\n", cs.SuccessIcon(), ghrepo.FullName(newRepo)) |
| 125 | + } |
| 126 | + |
| 127 | + if opts.HasRepoOverride { |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + remote, err := updateRemote(currRepo, newRepo, opts) |
| 132 | + if err != nil { |
| 133 | + fmt.Fprintf(opts.IO.ErrOut, "%s Warning: unable to update remote %q: %v\n", cs.WarningIcon(), remote.Name, err) |
| 134 | + } else if opts.IO.IsStdoutTTY() { |
| 135 | + fmt.Fprintf(opts.IO.Out, "%s Updated the %q remote\n", cs.SuccessIcon(), remote.Name) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func updateRemote(repo ghrepo.Interface, renamed ghrepo.Interface, opts *RenameOptions) (*context.Remote, error) { |
| 142 | + cfg, err := opts.Config() |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + protocol, err := cfg.Get(repo.RepoHost(), "git_protocol") |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + remotes, err := opts.Remotes() |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + remote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName()) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + remoteURL := ghrepo.FormatRemoteURL(renamed, protocol) |
| 163 | + err = git.UpdateRemoteURL(remote.Name, remoteURL) |
| 164 | + return remote, err |
| 165 | +} |
0 commit comments