Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add bin custom plugin type #129

Merged
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion plugins/custom_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package plugins
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"

"github.com/pkg/errors"
Expand All @@ -20,6 +22,8 @@ type TypePluginFormat string
const (
// TypePluginFormatTar is a tar archived plugin
TypePluginFormatTar TypePluginFormat = "tar"
// TypePluginFormatBin is a binary plugin
TypePluginFormatBin TypePluginFormat = "bin"
)

// CustomPlugin is a custom plugin
Expand Down Expand Up @@ -53,7 +57,7 @@ func (cp *CustomPlugin) SetTargetPath(path string) {

// fetch fetches the custom plugin at URL
func (cp *CustomPlugin) fetch(pluginName string) (string, error) {
tmpFile, err := ioutil.TempFile("", pluginName)
tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-*.tmp", pluginName))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requires go 1.11

if err != nil {
return "", errors.Wrap(err, "could not create temporary directory")
}
Expand All @@ -71,11 +75,35 @@ func (cp *CustomPlugin) process(fs afero.Fs, pluginName string, path string) err
switch cp.Format {
case TypePluginFormatTar:
return cp.processTar(fs, path)
case TypePluginFormatBin:
return cp.processBin(fs, pluginName, path)
default:
return errors.Errorf("Unknown plugin format %s", cp.Format)
}
}

func (cp *CustomPlugin) processBin(fs afero.Fs, name string, downloadPath string) error {
err := fs.MkdirAll(cp.targetDir, 0755)
if err != nil {
return errors.Wrapf(err, "Could not create directory %s", cp.targetDir)
}
targetPath := path.Join(cp.targetDir, name)
src, err := os.Open(downloadPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm not really happy with the os/fs break here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but, can be dealt with in another pr

if err != nil {
return errors.Wrapf(err, "Could not open downloaded file at %s", downloadPath)
}
dst, err := fs.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "Could not open target file at %s", targetPath)
}
_, err = io.Copy(dst, src)
if err != nil {
return errors.Wrapf(err, "Could not move %s to %s", downloadPath, targetPath)
}
err = fs.Chmod(targetPath, os.FileMode(0755))
return errors.Wrapf(err, "Error making %s executable", targetPath)
}

// https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
func (cp *CustomPlugin) processTar(fs afero.Fs, path string) error {
err := fs.MkdirAll(cp.targetDir, 0755)
Expand Down
39 changes: 39 additions & 0 deletions plugins/custom_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,45 @@ func TestCustomPluginTar(t *testing.T) {
}
}

func TestCustomPluginBin(t *testing.T) {
a := assert.New(t)
pluginName := "test-provider"
fs := afero.NewMemMapFs()
fileContents := "some contents"

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, fileContents)
a.Nil(err)
}))
defer ts.Close()

customPlugin := &plugins.CustomPlugin{
URL: ts.URL,
Format: plugins.TypePluginFormatBin,
}

customPlugin.SetTargetPath(plugins.CustomPluginDir)
a.Nil(customPlugin.Install(fs, pluginName))

afero.Walk(fs, "", func(path string, info os.FileInfo, err error) error {
a.Nil(err)
return nil
})

customPluginPath := path.Join(plugins.CustomPluginDir, pluginName)
f, err := fs.Open(customPluginPath)
a.Nil(err)

contents, err := ioutil.ReadAll(f)
a.Nil(err)
a.Equal(string(contents), fileContents)

fi, err := fs.Stat(customPluginPath)
a.Nil(err)
a.False(fi.IsDir())
a.Equal(fi.Mode().Perm(), os.FileMode(0755))
}

func generateTar(t *testing.T, files []string) string {
a := assert.New(t)

Expand Down