Skip to content

Commit 3513105

Browse files
committed
commit @samcoe's suggestions
1 parent 0596834 commit 3513105

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

pkg/cmd/repo/archive/archive.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.
3131
IO: f.IOStreams,
3232
HttpClient: f.HttpClient,
3333
Config: f.Config,
34+
BaseRepo: f.BaseRepo,
3435
}
3536

3637
cmd := &cobra.Command{
@@ -44,10 +45,9 @@ With no argument, archives the current repository.`),
4445
if len(args) > 0 {
4546
opts.RepoArg = args[0]
4647
}
47-
opts.BaseRepo = f.BaseRepo
4848

4949
if !opts.Confirmed && !opts.IO.CanPrompt() {
50-
return cmdutil.FlagErrorf("could not prompt: confirmation with prompt or --confirm flag required")
50+
return cmdutil.FlagErrorf("--confirm required when not running interactively")
5151
}
5252
if runF != nil {
5353
return runF(opts)
@@ -56,7 +56,7 @@ With no argument, archives the current repository.`),
5656
},
5757
}
5858

59-
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "skip confirmation prompt")
59+
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "Skip the confirmation prompt")
6060
return cmd
6161
}
6262

@@ -68,36 +68,37 @@ func archiveRun(opts *ArchiveOptions) error {
6868
}
6969
apiClient := api.NewClientFromHTTP(httpClient)
7070

71-
repoSelector := opts.RepoArg
72-
if repoSelector == "" {
73-
currRepo, err := opts.BaseRepo()
74-
if err != nil {
75-
return err
76-
}
77-
repoSelector = ghrepo.FullName(currRepo)
78-
}
71+
var toArchive ghrepo.Interface
7972

80-
if !strings.Contains(repoSelector, "/") {
81-
cfg, err := opts.Config()
73+
if opts.RepoArg == "" {
74+
toArchive, err = opts.BaseRepo()
8275
if err != nil {
8376
return err
8477
}
78+
} else {
79+
repoSelector := opts.RepoArg
80+
if !strings.Contains(repoSelector, "/") {
81+
cfg, err := opts.Config()
82+
if err != nil {
83+
return err
84+
}
8585

86-
hostname, err := cfg.DefaultHost()
87-
if err != nil {
88-
return err
86+
hostname, err := cfg.DefaultHost()
87+
if err != nil {
88+
return err
89+
}
90+
91+
currentUser, err := api.CurrentLoginName(apiClient, hostname)
92+
if err != nil {
93+
return err
94+
}
95+
repoSelector = currentUser + "/" + repoSelector
8996
}
9097

91-
currentUser, err := api.CurrentLoginName(apiClient, hostname)
98+
toArchive, err = ghrepo.FromFullName(repoSelector)
9299
if err != nil {
93100
return err
94101
}
95-
repoSelector = currentUser + "/" + repoSelector
96-
}
97-
98-
toArchive, err := ghrepo.FromFullName(repoSelector)
99-
if err != nil {
100-
return err
101102
}
102103

103104
fields := []string{"name", "owner", "isArchived", "id"}
@@ -122,7 +123,7 @@ func archiveRun(opts *ArchiveOptions) error {
122123
return fmt.Errorf("failed to prompt: %w", err)
123124
}
124125
if !opts.Confirmed {
125-
return nil
126+
return cmdutil.CancelError
126127
}
127128
}
128129

pkg/cmd/repo/archive/archive_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ func TestNewCmdArchive(t *testing.T) {
2020
name string
2121
input string
2222
wantErr bool
23+
output ArchiveOptions
2324
errMsg string
2425
}{
2526
{
26-
name: "no arguments tty",
27+
name: "no arguments no tty",
2728
input: "",
28-
errMsg: "could not prompt: confirmation with prompt or --confirm flag required",
29+
errMsg: "--confirm required when not running interactively",
2930
wantErr: true,
3031
},
32+
{
33+
name: "repo argument tty",
34+
input: "OWNER/REPO --confirm",
35+
output: ArchiveOptions{RepoArg: "OWNER/REPO", Confirmed: true},
36+
},
3137
}
3238
for _, tt := range tests {
3339
t.Run(tt.name, func(t *testing.T) {
@@ -37,7 +43,9 @@ func TestNewCmdArchive(t *testing.T) {
3743
}
3844
argv, err := shlex.Split(tt.input)
3945
assert.NoError(t, err)
46+
var gotOpts *ArchiveOptions
4047
cmd := NewCmdArchive(f, func(opts *ArchiveOptions) error {
48+
gotOpts = opts
4149
return nil
4250
})
4351
cmd.SetArgs(argv)
@@ -51,15 +59,14 @@ func TestNewCmdArchive(t *testing.T) {
5159
return
5260
}
5361
assert.NoError(t, err)
62+
assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg)
63+
assert.Equal(t, tt.output.Confirmed, gotOpts.Confirmed)
5464
})
5565
}
5666
}
5767

5868
func Test_ArchiveRun(t *testing.T) {
59-
queryResponse := `{ "data": { "repository":
60-
{ "id": "THE-ID",
61-
"isArchived": %s}
62-
} }`
69+
queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }`
6370
tests := []struct {
6471
name string
6572
opts ArchiveOptions
@@ -107,9 +114,6 @@ func Test_ArchiveRun(t *testing.T) {
107114
name: "archived repo tty",
108115
wantStderr: "! Repository OWNER/REPO is already archived\n",
109116
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
110-
askStubs: func(q *prompt.AskStubber) {
111-
q.StubOne(true)
112-
},
113117
httpStubs: func(reg *httpmock.Registry) {
114118
reg.Register(
115119
httpmock.GraphQL(`query RepositoryInfo\b`),
@@ -120,27 +124,26 @@ func Test_ArchiveRun(t *testing.T) {
120124

121125
for _, tt := range tests {
122126
repo, _ := ghrepo.FromFullName("OWNER/REPO")
123-
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
124-
return repo, nil
125-
}
126-
127-
q, teardown := prompt.InitAskStubber()
128-
defer teardown()
129-
if tt.askStubs != nil {
130-
tt.askStubs(q)
131-
}
132-
133127
reg := &httpmock.Registry{}
134128
if tt.httpStubs != nil {
135129
tt.httpStubs(reg)
136130
}
131+
132+
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
133+
return repo, nil
134+
}
137135
tt.opts.HttpClient = func() (*http.Client, error) {
138136
return &http.Client{Transport: reg}, nil
139137
}
140-
141138
io, _, stdout, stderr := iostreams.Test()
142139
tt.opts.IO = io
143140

141+
q, teardown := prompt.InitAskStubber()
142+
defer teardown()
143+
if tt.askStubs != nil {
144+
tt.askStubs(q)
145+
}
146+
144147
t.Run(tt.name, func(t *testing.T) {
145148
defer reg.Verify(t)
146149
io.SetStdoutTTY(tt.isTTY)

0 commit comments

Comments
 (0)