Skip to content

Commit f4d97dc

Browse files
author
vilmibm
committed
WIP refactoring
1 parent ae38daf commit f4d97dc

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

pkg/cmd/extension/command.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,38 +105,20 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
105105
return err
106106
}
107107
if err := checkValidExtension(cmd.Root(), m, repo.RepoName()); err != nil {
108-
// TODO i feel like this should check for a gh-foo script
109108
return err
110109
}
111-
112110
client, err := f.HttpClient()
113111
if err != nil {
114112
return fmt.Errorf("could not make http client: %w", err)
115113
}
116114
client = api.NewCachedClient(client, time.Second*30)
117115

118-
isBin, err := isBinExtension(client, repo)
119-
if err != nil {
120-
return fmt.Errorf("could not check for binary extension: %w", err)
121-
}
122-
if isBin {
123-
return m.InstallBin(client, repo)
124-
}
125-
126-
hs, err := hasScript(client, repo)
127-
if err != nil {
128-
return err
129-
}
130-
if !hs {
131-
return errors.New("extension is uninstallable: missing executable")
132-
}
133-
134116
cfg, err := f.Config()
135117
if err != nil {
136118
return err
137119
}
138-
protocol, _ := cfg.Get(repo.RepoHost(), "git_protocol")
139-
return m.InstallGit(ghrepo.FormatRemoteURL(repo, protocol), io.Out, io.ErrOut)
120+
121+
return m.Install(client, repo, io, cfg)
140122
},
141123
},
142124
func() *cobra.Command {

pkg/cmd/extension/manager.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cli/cli/v2/internal/ghrepo"
2020
"github.com/cli/cli/v2/pkg/extensions"
2121
"github.com/cli/cli/v2/pkg/findsh"
22+
"github.com/cli/cli/v2/pkg/iostreams"
2223
"github.com/cli/safeexec"
2324
"gopkg.in/yaml.v3"
2425
)
@@ -187,6 +188,28 @@ type BinManifest struct {
187188
Path string
188189
}
189190

191+
func (m *Manager) Install(client *http.Client, repo ghrepo.Interface, io *iostreams.IOStreams, cfg config.Config) error {
192+
isBin, err := isBinExtension(client, repo)
193+
if err != nil {
194+
return fmt.Errorf("could not check for binary extension: %w", err)
195+
}
196+
if isBin {
197+
return m.InstallBin(client, repo)
198+
}
199+
200+
hs, err := hasScript(client, repo)
201+
if err != nil {
202+
return err
203+
}
204+
if !hs {
205+
// TODO open an issue hint, here?
206+
return errors.New("extension is uninstallable: missing executable")
207+
}
208+
209+
protocol, _ := cfg.Get(repo.RepoHost(), "git_protocol")
210+
return m.InstallGit(ghrepo.FormatRemoteURL(repo, protocol), io.Out, io.ErrOut)
211+
}
212+
190213
func (m *Manager) InstallBin(client *http.Client, repo ghrepo.Interface) error {
191214
var r *release
192215
r, err := fetchLatestRelease(client, repo)

pkg/cmd/extension/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newTestManager(dir string) *Manager {
4444
return cmd
4545
},
4646
platform: func() string {
47-
return "amiga-arm64"
47+
return "windows-amd64"
4848
},
4949
}
5050
}
@@ -222,7 +222,7 @@ func TestManager_InstallBin(t *testing.T) {
222222
release{
223223
Assets: []releaseAsset{
224224
{
225-
Name: "gh-bin-ext-amiga-arm64",
225+
Name: "gh-bin-ext-windows-amd64",
226226
APIURL: "https://example.com/release/cool",
227227
},
228228
},

pkg/extensions/extension.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"io"
55
"net/http"
66

7+
"github.com/cli/cli/v2/internal/config"
78
"github.com/cli/cli/v2/internal/ghrepo"
9+
"github.com/cli/cli/v2/pkg/iostreams"
810
)
911

1012
//go:generate moq -rm -out extension_mock.go . Extension
@@ -19,8 +21,9 @@ type Extension interface {
1921
//go:generate moq -rm -out manager_mock.go . ExtensionManager
2022
type ExtensionManager interface {
2123
List(includeMetadata bool) []Extension
22-
InstallGit(url string, stdout, stderr io.Writer) error
23-
InstallBin(client *http.Client, repo ghrepo.Interface) error
24+
Install(*http.Client, ghrepo.Interface, *iostreams.IOStreams, config.Config) error
25+
InstallBin(*http.Client, ghrepo.Interface) error
26+
InstallGit(string, io.Writer, io.Writer) error
2427
InstallLocal(dir string) error
2528
Upgrade(name string, force bool, stdout, stderr io.Writer) error
2629
Remove(name string) error

0 commit comments

Comments
 (0)