Skip to content

Commit

Permalink
Merge pull request cli#4568 from cli/bin-ext-dispatch
Browse files Browse the repository at this point in the history
dispatch binary extensions directly
  • Loading branch information
Nate Smith authored Oct 21, 2021
2 parents 6759511 + f65c153 commit 1f3478d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
4 changes: 4 additions & 0 deletions pkg/cmd/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ func (e *Extension) UpdateAvailable() bool {
}
return true
}

func (e *Extension) IsBinary() bool {
return e.kind == BinaryKind
}
34 changes: 18 additions & 16 deletions pkg/cmd/extension/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Wri
forwardArgs := args[1:]

exts, _ := m.list(false)
var ext Extension
for _, e := range exts {
if e.Name() == extName {
exe = e.Path()
ext = e
exe = ext.Path()
break
}
}
Expand All @@ -81,7 +83,9 @@ func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Wri

var externalCmd *exec.Cmd

if runtime.GOOS == "windows" {
if ext.IsBinary() || runtime.GOOS != "windows" {
externalCmd = m.newCommand(exe, forwardArgs...)
} else if runtime.GOOS == "windows" {
// Dispatch all extension calls through the `sh` interpreter to support executable files with a
// shebang line on Windows.
shExe, err := m.findSh()
Expand All @@ -93,8 +97,6 @@ func (m *Manager) Dispatch(args []string, stdin io.Reader, stdout, stderr io.Wri
}
forwardArgs = append([]string{"-c", `command "$@"`, "--", exe}, forwardArgs...)
externalCmd = m.newCommand(shExe, forwardArgs...)
} else {
externalCmd = m.newCommand(exe, forwardArgs...)
}
externalCmd.Stdin = stdin
externalCmd.Stdout = stdout
Expand Down Expand Up @@ -268,30 +270,30 @@ func (m *Manager) getLatestVersion(ext Extension) (string, error) {
if ext.isLocal {
return "", fmt.Errorf("unable to get latest version for local extensions")
}
if ext.kind == GitKind {
gitExe, err := m.lookPath("git")
if ext.IsBinary() {
repo, err := ghrepo.FromFullName(ext.url)
if err != nil {
return "", err
}
extDir := filepath.Dir(ext.path)
gitDir := "--git-dir=" + filepath.Join(extDir, ".git")
cmd := m.newCommand(gitExe, gitDir, "ls-remote", "origin", "HEAD")
lsRemote, err := cmd.Output()
r, err := fetchLatestRelease(m.client, repo)
if err != nil {
return "", err
}
remoteSha := bytes.SplitN(lsRemote, []byte("\t"), 2)[0]
return string(remoteSha), nil
return r.Tag, nil
} else {
repo, err := ghrepo.FromFullName(ext.url)
gitExe, err := m.lookPath("git")
if err != nil {
return "", err
}
r, err := fetchLatestRelease(m.client, repo)
extDir := filepath.Dir(ext.path)
gitDir := "--git-dir=" + filepath.Join(extDir, ".git")
cmd := m.newCommand(gitExe, gitDir, "ls-remote", "origin", "HEAD")
lsRemote, err := cmd.Output()
if err != nil {
return "", err
}
return r.Tag, nil
remoteSha := bytes.SplitN(lsRemote, []byte("\t"), 2)[0]
return string(remoteSha), nil
}
}

Expand Down Expand Up @@ -477,7 +479,7 @@ func (m *Manager) upgradeExtension(ext Extension, force bool) error {
return upToDateError
}
var err error
if ext.kind == BinaryKind {
if ext.IsBinary() {
err = m.upgradeBinExtension(ext)
} else {
err = m.upgradeGitExtension(ext, force)
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/extension/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ func TestManager_Dispatch(t *testing.T) {
assert.Equal(t, "", stderr.String())
}

func TestManager_Dispatch_binary(t *testing.T) {
tempDir := t.TempDir()
extPath := filepath.Join(tempDir, "extensions", "gh-hello")
exePath := filepath.Join(extPath, "gh-hello")
bm := binManifest{
Owner: "owner",
Name: "gh-hello",
Host: "github.com",
Tag: "v1.0.0",
}
assert.NoError(t, stubBinaryExtension(extPath, bm))

m := newTestManager(tempDir, nil, nil)

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
found, err := m.Dispatch([]string{"hello", "one", "two"}, nil, stdout, stderr)
assert.NoError(t, err)
assert.True(t, found)

assert.Equal(t, fmt.Sprintf("[%s one two]\n", exePath), stdout.String())
assert.Equal(t, "", stderr.String())
}

func TestManager_Remove(t *testing.T) {
tempDir := t.TempDir()
assert.NoError(t, stubExtension(filepath.Join(tempDir, "extensions", "gh-hello", "gh-hello")))
Expand Down
1 change: 1 addition & 0 deletions pkg/extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Extension interface {
URL() string
IsLocal() bool
UpdateAvailable() bool
IsBinary() bool
}

//go:generate moq -rm -out manager_mock.go . ExtensionManager
Expand Down
36 changes: 36 additions & 0 deletions pkg/extensions/extension_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f3478d

Please sign in to comment.