Skip to content

Commit

Permalink
[plugin.install] support file: scheme URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Nov 14, 2017
1 parent bbc81d0 commit 6e0216a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
26 changes: 16 additions & 10 deletions plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plugin
import (
"fmt"
"net/http"
"strings"
)

// client provides utilities for http request
Expand All @@ -12,20 +13,25 @@ const userAgent = "mkr-plugin-installer/0.0.0"

// Get response from `url`
func (c *client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
resp, err := func() (*http.Response, error) {
if strings.HasPrefix(url, "file:///") {
t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
return (&http.Client{Transport: t}).Get(url)
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
return http.DefaultClient.Do(req)
}()
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("http response not OK. code: %d, url: %s", resp.StatusCode, url)
return nil, err
resp.Body.Close()
return nil, fmt.Errorf("http response not OK. code: %d, url: %s", resp.StatusCode, url)
}

return resp, nil
}
9 changes: 6 additions & 3 deletions plugin/install_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ const (
)

// the pattern of installTarget string
// (?:<plugin_name>|<owner>/<repo>)(?:@<releaseTag>)?
var targetReg = regexp.MustCompile(`^(?:([^@/]+)/([^@/]+)|([^@/]+))(?:@(.+))?$`)
var (
// (?:<plugin_name>|<owner>/<repo>)(?:@<releaseTag>)?
targetReg = regexp.MustCompile(`^(?:([^@/]+)/([^@/]+)|([^@/]+))(?:@(.+))?$`)
urlReg = regexp.MustCompile(`^(?:https?|file)://`)
)

// Parse install target string, and construct installTarget
// example is below
// - mackerelio/mackerel-plugin-sample
// - mackerel-plugin-sample
// - mackerelio/mackerel-plugin-sample@v0.0.1
func newInstallTargetFromString(target string) (*installTarget, error) {
if strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://") {
if urlReg.MatchString(target) {
return &installTarget{
directURL: target,
}, nil
Expand Down
28 changes: 25 additions & 3 deletions plugin/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -204,10 +205,9 @@ func newPluginInstallContext(target, prefix string, overwrite bool) *cli.Context
}

func TestDoPluginInstall(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()

t.Run("specify URL directly", func(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
tmpd := tempd(t)
defer os.RemoveAll(tmpd)

Expand All @@ -219,6 +219,28 @@ func TestDoPluginInstall(t *testing.T) {
_, err = os.Stat(fpath)
assert.Nil(t, err, "sample plugin is successfully installed and located")
})

t.Run("file: scheme URL", func(t *testing.T) {
cwd, _ := os.Getwd()
fpath := filepath.Join(cwd, "testdata", "mackerel-plugin-sample_linux_amd64.zip")
fpath = filepath.ToSlash(fpath) // care windows
scheme := "file://"
if !strings.HasPrefix(fpath, "/") {
// care windows drive letter
scheme += "/"
}

tmpd := tempd(t)
defer os.RemoveAll(tmpd)

ctx := newPluginInstallContext(scheme+fpath, tmpd, false)
err := doPluginInstall(ctx)
assert.Nil(t, err, "sample plugin is succesfully installed")

plugPath := filepath.Join(tmpd, "bin", "mackerel-plugin-sample")
_, err = os.Stat(plugPath)
assert.Nil(t, err, "sample plugin is successfully installed and located")
})
}

func TestLooksLikePlugin(t *testing.T) {
Expand Down

0 comments on commit 6e0216a

Please sign in to comment.