WIP refactoring

This commit is contained in:
vilmibm 2021-09-20 16:25:26 -05:00
parent ae38daf08a
commit f4d97dcedd
4 changed files with 32 additions and 24 deletions

View file

@ -105,38 +105,20 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
return err
}
if err := checkValidExtension(cmd.Root(), m, repo.RepoName()); err != nil {
// TODO i feel like this should check for a gh-foo script
return err
}
client, err := f.HttpClient()
if err != nil {
return fmt.Errorf("could not make http client: %w", err)
}
client = api.NewCachedClient(client, time.Second*30)
isBin, err := isBinExtension(client, repo)
if err != nil {
return fmt.Errorf("could not check for binary extension: %w", err)
}
if isBin {
return m.InstallBin(client, repo)
}
hs, err := hasScript(client, repo)
if err != nil {
return err
}
if !hs {
return errors.New("extension is uninstallable: missing executable")
}
cfg, err := f.Config()
if err != nil {
return err
}
protocol, _ := cfg.Get(repo.RepoHost(), "git_protocol")
return m.InstallGit(ghrepo.FormatRemoteURL(repo, protocol), io.Out, io.ErrOut)
return m.Install(client, repo, io, cfg)
},
},
func() *cobra.Command {

View file

@ -19,6 +19,7 @@ import (
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/extensions"
"github.com/cli/cli/v2/pkg/findsh"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/safeexec"
"gopkg.in/yaml.v3"
)
@ -187,6 +188,28 @@ type BinManifest struct {
Path string
}
func (m *Manager) Install(client *http.Client, repo ghrepo.Interface, io *iostreams.IOStreams, cfg config.Config) error {
isBin, err := isBinExtension(client, repo)
if err != nil {
return fmt.Errorf("could not check for binary extension: %w", err)
}
if isBin {
return m.InstallBin(client, repo)
}
hs, err := hasScript(client, repo)
if err != nil {
return err
}
if !hs {
// TODO open an issue hint, here?
return errors.New("extension is uninstallable: missing executable")
}
protocol, _ := cfg.Get(repo.RepoHost(), "git_protocol")
return m.InstallGit(ghrepo.FormatRemoteURL(repo, protocol), io.Out, io.ErrOut)
}
func (m *Manager) InstallBin(client *http.Client, repo ghrepo.Interface) error {
var r *release
r, err := fetchLatestRelease(client, repo)

View file

@ -44,7 +44,7 @@ func newTestManager(dir string) *Manager {
return cmd
},
platform: func() string {
return "amiga-arm64"
return "windows-amd64"
},
}
}
@ -222,7 +222,7 @@ func TestManager_InstallBin(t *testing.T) {
release{
Assets: []releaseAsset{
{
Name: "gh-bin-ext-amiga-arm64",
Name: "gh-bin-ext-windows-amd64",
APIURL: "https://example.com/release/cool",
},
},

View file

@ -4,7 +4,9 @@ import (
"io"
"net/http"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/iostreams"
)
//go:generate moq -rm -out extension_mock.go . Extension
@ -19,8 +21,9 @@ type Extension interface {
//go:generate moq -rm -out manager_mock.go . ExtensionManager
type ExtensionManager interface {
List(includeMetadata bool) []Extension
InstallGit(url string, stdout, stderr io.Writer) error
InstallBin(client *http.Client, repo ghrepo.Interface) error
Install(*http.Client, ghrepo.Interface, *iostreams.IOStreams, config.Config) error
InstallBin(*http.Client, ghrepo.Interface) error
InstallGit(string, io.Writer, io.Writer) error
InstallLocal(dir string) error
Upgrade(name string, force bool, stdout, stderr io.Writer) error
Remove(name string) error