11package rename
22
33import (
4- "bytes"
5- "encoding/json"
64 "errors"
75 "fmt"
86 "net/http"
97
108 "github.com/AlecAivazis/survey/v2"
119 "github.com/MakeNowJust/heredoc"
12- "github.com/cli/cli/v2/api"
1310 "github.com/cli/cli/v2/context"
1411 "github.com/cli/cli/v2/git"
1512 "github.com/cli/cli/v2/internal/config"
@@ -30,13 +27,6 @@ type RenameOptions struct {
3027 newRepoSelector string
3128}
3229
33- type renameRepo struct {
34- RepoHost string
35- RepoOwner string
36- RepoName string
37- Name string `json:"name,omitempty"`
38- }
39-
4030func NewCmdRename (f * cmdutil.Factory , runf func (* RenameOptions ) error ) * cobra.Command {
4131 opts := & RenameOptions {
4232 IO : f .IOStreams ,
@@ -46,15 +36,11 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
4636 }
4737
4838 cmd := & cobra.Command {
49- Use : "rename [<repository>] [< new-name>]" ,
39+ Use : "rename [<new-name>]" ,
5040 Short : "Rename a repository" ,
5141 Long : heredoc .Doc (`Rename a GitHub repository
52-
53- With no argument, the repository for the current directory is renamed using a prompt
54-
55- With one argument, the repository of the current directory is renamed using the argument
56-
57- With '-R', and two arguments the given repository is replaced with the new name` ),
42+
43+ By default, renames the current repository otherwise rename the specified repository.` ),
5844 Args : cobra .MaximumNArgs (1 ),
5945 RunE : func (cmd * cobra.Command , args []string ) error {
6046 opts .BaseRepo = f .BaseRepo
@@ -63,7 +49,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
6349 if len (args ) > 0 {
6450 opts .newRepoSelector = args [0 ]
6551 } else if ! opts .IO .CanPrompt () {
66- return & cmdutil.FlagError {Err : errors .New ("could not prompt: proceed with a repo name " )}
52+ return & cmdutil.FlagError {Err : errors .New ("could not prompt: new name required when not running interactively " )}
6753 }
6854
6955 if runf != nil {
@@ -83,12 +69,9 @@ func renameRun(opts *RenameOptions) error {
8369 if err != nil {
8470 return err
8571 }
86- apiClient := api .NewClientFromHTTP (httpClient )
8772
88- var input renameRepo
8973 var newRepo ghrepo.Interface
9074 var baseRemote * context.Remote
91- var remoteUpdateError error
9275 newRepoName := opts .newRepoSelector
9376
9477 currRepo , err := opts .BaseRepo ()
@@ -99,7 +82,7 @@ func renameRun(opts *RenameOptions) error {
9982 if newRepoName == "" {
10083 err = prompt .SurveyAskOne (
10184 & survey.Input {
102- Message : fmt .Sprintf ("Rename %s to: " , currRepo . RepoOwner () + "/" + currRepo . RepoName ( )),
85+ Message : fmt .Sprintf ("Rename %s to: " , ghrepo . FullName ( currRepo )),
10386 },
10487 & newRepoName ,
10588 )
@@ -108,21 +91,20 @@ func renameRun(opts *RenameOptions) error {
10891 }
10992 }
11093
111- input = renameRepo {
112- RepoHost : currRepo .RepoHost (),
113- RepoOwner : currRepo .RepoOwner (),
114- RepoName : currRepo .RepoName (),
115- Name : newRepoName ,
116- }
117-
11894 newRepo = ghrepo .NewWithHost (currRepo .RepoOwner (), newRepoName , currRepo .RepoHost ())
11995
120- err = runRename (apiClient , currRepo . RepoHost (), input )
96+ err = runRename (httpClient , currRepo , newRepoName )
12197 if err != nil {
122- return fmt .Errorf ("API called failed: %s, please check your parameters" , err )
98+ return fmt .Errorf ("API called failed: %s" , err )
99+ }
100+
101+ if opts .IO .IsStdoutTTY () {
102+ cs := opts .IO .ColorScheme ()
103+ fmt .Fprintf (opts .IO .Out , "%s Renamed repository %s\n " , cs .SuccessIcon (), ghrepo .FullName (newRepo ))
123104 }
124105
125106 if ! opts .HasRepoOverride {
107+ cs := opts .IO .ColorScheme ()
126108 cfg , err := opts .Config ()
127109 if err != nil {
128110 return err
@@ -132,30 +114,13 @@ func renameRun(opts *RenameOptions) error {
132114 remotes , _ := opts .Remotes ()
133115 baseRemote , _ = remotes .FindByRepo (currRepo .RepoOwner (), currRepo .RepoName ())
134116 remoteURL := ghrepo .FormatRemoteURL (newRepo , protocol )
135- remoteUpdateError = git .UpdateRemoteURL (baseRemote .Name , remoteURL )
136- if remoteUpdateError != nil {
137- cs := opts .IO .ColorScheme ()
117+ err = git .UpdateRemoteURL (baseRemote .Name , remoteURL )
118+ if err != nil {
138119 fmt .Fprintf (opts .IO .ErrOut , "%s warning: unable to update remote '%s' \n " , cs .WarningIcon (), err )
139120 }
140- }
141-
142- if opts .IO .IsStdoutTTY () {
143- cs := opts .IO .ColorScheme ()
144- fmt .Fprintf (opts .IO .Out , "%s Renamed repository %s\n " , cs .SuccessIcon (), input .RepoOwner + "/" + input .Name )
145- if ! opts .HasRepoOverride && remoteUpdateError == nil {
121+ if opts .IO .IsStdoutTTY () {
146122 fmt .Fprintf (opts .IO .Out , "%s Updated the %q remote \n " , cs .SuccessIcon (), baseRemote .Name )
147123 }
148124 }
149125 return nil
150126}
151-
152- func runRename (apiClient * api.Client , hostname string , input renameRepo ) error {
153- path := fmt .Sprintf ("repos/%s/%s" , input .RepoOwner , input .RepoName )
154- body := & bytes.Buffer {}
155- enc := json .NewEncoder (body )
156- if err := enc .Encode (input ); err != nil {
157- return err
158- }
159-
160- return apiClient .REST (hostname , "PATCH" , path , body , nil )
161- }
0 commit comments