confirm when inferring repository
This commit is contained in:
parent
296cf381a2
commit
da258fb4f4
2 changed files with 84 additions and 3 deletions
|
|
@ -22,6 +22,7 @@ type RenameOptions struct {
|
|||
Config func() (config.Config, error)
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Remotes func() (context.Remotes, error)
|
||||
DoConfirm bool
|
||||
HasRepoOverride bool
|
||||
newRepoSelector string
|
||||
}
|
||||
|
|
@ -34,6 +35,8 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
|
|||
Config: f.Config,
|
||||
}
|
||||
|
||||
var confirm bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "rename [<new-name>]",
|
||||
Short: "Rename a repository",
|
||||
|
|
@ -51,6 +54,13 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
|
|||
return cmdutil.FlagErrorf("new name argument required when not running interactively\n")
|
||||
}
|
||||
|
||||
if len(args) == 1 && !confirm && !opts.HasRepoOverride {
|
||||
if !opts.IO.CanPrompt() {
|
||||
return cmdutil.FlagErrorf("--confirm required when passing a single argument")
|
||||
}
|
||||
opts.DoConfirm = true
|
||||
}
|
||||
|
||||
if runf != nil {
|
||||
return runf(opts)
|
||||
}
|
||||
|
|
@ -59,6 +69,7 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
|
|||
}
|
||||
|
||||
cmdutil.EnableRepoOverride(cmd, f)
|
||||
cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "skip confirmation prompt")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -88,6 +99,21 @@ func renameRun(opts *RenameOptions) error {
|
|||
}
|
||||
}
|
||||
|
||||
if opts.DoConfirm {
|
||||
var confirmed bool
|
||||
p := &survey.Confirm{
|
||||
Message: fmt.Sprintf("Rename %s to %s?", ghrepo.FullName(currRepo), newRepoName),
|
||||
Default: false,
|
||||
}
|
||||
err = prompt.SurveyAskOne(p, &confirmed)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prompt: %w", err)
|
||||
}
|
||||
if !confirmed {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
err = runRename(httpClient, currRepo, newRepoName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("API called failed: %s", err)
|
||||
|
|
|
|||
|
|
@ -32,15 +32,37 @@ func TestNewCmdRename(t *testing.T) {
|
|||
input: "",
|
||||
errMsg: "new name argument required when not running interactively\n",
|
||||
wantErr: true,
|
||||
tty: false,
|
||||
},
|
||||
{
|
||||
name: "one argument",
|
||||
input: "REPO",
|
||||
name: "one argument no tty confirmed",
|
||||
input: "REPO --confirm",
|
||||
output: RenameOptions{
|
||||
newRepoSelector: "REPO",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "one argument no tty",
|
||||
input: "REPO",
|
||||
errMsg: "--confirm required when passing a single argument",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "one argument tty confirmed",
|
||||
input: "REPO --confirm",
|
||||
tty: true,
|
||||
output: RenameOptions{
|
||||
newRepoSelector: "REPO",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "one argument tty",
|
||||
input: "REPO",
|
||||
tty: true,
|
||||
output: RenameOptions{
|
||||
newRepoSelector: "REPO",
|
||||
DoConfirm: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "full flag argument",
|
||||
input: "--repo OWNER/REPO NEW_REPO",
|
||||
|
|
@ -153,6 +175,39 @@ func TestRenameRun(t *testing.T) {
|
|||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "confirmation with yes",
|
||||
tty: true,
|
||||
opts: RenameOptions{
|
||||
newRepoSelector: "NEW_REPO",
|
||||
DoConfirm: true,
|
||||
},
|
||||
wantOut: "✓ Renamed repository OWNER/NEW_REPO\n✓ Updated the \"origin\" remote\n",
|
||||
askStubs: func(q *prompt.AskStubber) {
|
||||
q.StubOne(true)
|
||||
},
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
reg.Register(
|
||||
httpmock.REST("PATCH", "repos/OWNER/REPO"),
|
||||
httpmock.StatusStringResponse(204, "{}"))
|
||||
},
|
||||
execStubs: func(cs *run.CommandStubber) {
|
||||
cs.Register(`git remote set-url origin https://github.com/OWNER/NEW_REPO.git`, 0, "")
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "confirmation with no",
|
||||
tty: true,
|
||||
opts: RenameOptions{
|
||||
newRepoSelector: "NEW_REPO",
|
||||
DoConfirm: true,
|
||||
},
|
||||
askStubs: func(q *prompt.AskStubber) {
|
||||
q.StubOne(false)
|
||||
},
|
||||
wantOut: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue