-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from carolynvs/ensuremixins
Improve helper methods for porter
- Loading branch information
Showing
17 changed files
with
325 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: Test | ||
|
||
on: [push] | ||
on: [push,pull_request] | ||
|
||
jobs: | ||
test: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ import ( | |
"os" | ||
"path/filepath" | ||
|
||
"get.porter.sh/magefiles/porter" | ||
"get.porter.sh/magefiles/releases" | ||
"get.porter.sh/magefiles/tools" | ||
"github.com/carolynvs/magex/mgx" | ||
"github.com/carolynvs/magex/shx" | ||
"github.com/carolynvs/magex/xplat" | ||
|
@@ -64,14 +64,15 @@ func (m Magefile) Publish() { | |
// Publish binaries to a github release | ||
// Requires PORTER_RELEASE_REPOSITORY to be set to github.com/USERNAME/REPO | ||
func (m Magefile) PublishBinaries() { | ||
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) | ||
releases.PrepareMixinForPublish(m.MixinName) | ||
releases.PublishMixin(m.MixinName) | ||
} | ||
|
||
// Publish a mixin feed | ||
// Requires PORTER_PACKAGES_REMOTE to be set to [email protected]:USERNAME/REPO.git | ||
func (m Magefile) PublishMixinFeed() { | ||
mg.Deps(tools.EnsurePorter) | ||
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) | ||
releases.PublishMixinFeed(m.MixinName) | ||
} | ||
|
||
|
@@ -90,14 +91,7 @@ func (m Magefile) TestPublish(username string) { | |
|
||
// Install the mixin | ||
func (m Magefile) Install() { | ||
porterHome := os.Getenv("PORTER_HOME") | ||
if porterHome == "" { | ||
home, _ := os.UserHomeDir() | ||
porterHome = filepath.Join(home, ".porter") | ||
} | ||
if _, err := os.Stat(porterHome); err != nil { | ||
panic("Could not find a Porter installation. Make sure that Porter is installed and set PORTER_HOME if you are using a non-standard installation path") | ||
} | ||
porterHome := porter.GetPorterHome() | ||
fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome) | ||
|
||
os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0770) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package porter | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/carolynvs/magex/xplat" | ||
) | ||
|
||
var ( | ||
// DefaultMixinVersion is the default version of mixins installed when it's not present | ||
DefaultMixinVersion = "canary" | ||
) | ||
|
||
// GetPorterHome determines the current PORTER_HOME directory | ||
func GetPorterHome() string { | ||
porterHome := os.Getenv("PORTER_HOME") | ||
if porterHome == "" { | ||
home, _ := os.UserHomeDir() | ||
porterHome = filepath.Join(home, ".porter") | ||
} | ||
if _, err := os.Stat(porterHome); err != nil { | ||
panic("Could not find a Porter installation. Make sure that Porter is installed and set PORTER_HOME if you are using a non-standard installation path") | ||
} | ||
return porterHome | ||
} | ||
|
||
// UseBinForPorterHome sets the bin/ directory to be PORTER_HOME | ||
func UseBinForPorterHome() { | ||
// use bin as PORTER_HOME | ||
wd, _ := os.Getwd() | ||
home := filepath.Join(wd, "bin") | ||
os.Mkdir(home, 0770) | ||
UsePorterHome(home) | ||
} | ||
|
||
// UsePorterHome sets the specified directory to be PORTER_HOME | ||
func UsePorterHome(home string) { | ||
os.Setenv("PORTER_HOME", home) | ||
|
||
// Add PORTER_HOME to the PATH | ||
xplat.EnsureInPath(home) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package porter | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/carolynvs/magex/xplat" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetPorterHome(t *testing.T) { | ||
t.Run("PORTER_HOME set", func(t *testing.T) { | ||
tmpPorterHome, err := ioutil.TempDir("", "magefiles") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmpPorterHome) | ||
|
||
os.Setenv("PORTER_HOME", tmpPorterHome) | ||
defer os.Unsetenv("PORTER_HOME") | ||
|
||
gotHome := GetPorterHome() | ||
assert.Equal(t, tmpPorterHome, gotHome) | ||
}) | ||
|
||
t.Run("Default to HOME/.porter", func(t *testing.T) { | ||
tmpUserHome, err := ioutil.TempDir("", "magefiles") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmpUserHome) | ||
tmpPorterHome := filepath.Join(tmpUserHome, ".porter") | ||
err = os.Mkdir(tmpPorterHome, 0700) | ||
require.NoError(t, err) | ||
|
||
os.Setenv("HOME", tmpUserHome) | ||
defer os.Unsetenv("HOME") | ||
|
||
gotHome := GetPorterHome() | ||
assert.Equal(t, tmpPorterHome, gotHome) | ||
}) | ||
|
||
t.Run("no home found", func(t *testing.T) { | ||
os.Unsetenv("HOME") | ||
|
||
defer func() { | ||
panicObj := recover() | ||
require.Contains(t, panicObj, "Could not find a Porter installation", "Expected a panic since there's no home for porter") | ||
}() | ||
|
||
GetPorterHome() | ||
}) | ||
} | ||
|
||
func TestUseBinForPorterHome(t *testing.T) { | ||
defer os.RemoveAll("bin") | ||
|
||
UseBinForPorterHome() | ||
|
||
pwd, _ := os.Getwd() | ||
binDir := filepath.Join(pwd, "bin") | ||
assert.Equal(t, binDir, os.Getenv("PORTER_HOME"), "PORTER_HOME was not set correctly") | ||
|
||
assert.True(t, xplat.InPath(binDir), "expected the bin directory to be in the PATH") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package porter | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/carolynvs/magex/mgx" | ||
"github.com/carolynvs/magex/pkg" | ||
"github.com/carolynvs/magex/pkg/downloads" | ||
"github.com/carolynvs/magex/shx" | ||
"github.com/carolynvs/magex/xplat" | ||
) | ||
|
||
var ( | ||
// DefaultPorterVersion is the default version of Porter that is installed when it's not present | ||
DefaultPorterVersion = "v1.0.0-alpha.19" | ||
) | ||
|
||
// Install the default version of porter | ||
func EnsurePorter() { | ||
EnsurePorterAt(DefaultPorterVersion) | ||
} | ||
|
||
// Install the specified version of porter | ||
func EnsurePorterAt(version string) { | ||
home := GetPorterHome() | ||
runtimesDir := filepath.Join(home, "runtimes") | ||
os.MkdirAll(runtimesDir, 0770) | ||
|
||
clientPath := filepath.Join(home, "porter"+xplat.FileExt()) | ||
if clientFound, _ := pkg.IsCommandAvailable(clientPath, version, "--version"); !clientFound { | ||
log.Println("Porter client not found at", clientPath) | ||
log.Println("Installing porter into", home) | ||
opts := downloads.DownloadOptions{ | ||
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", | ||
Name: "porter", | ||
Version: version, | ||
Ext: xplat.FileExt(), | ||
} | ||
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { | ||
// we don't yet publish arm64 for porter | ||
opts.UrlTemplate = "https://cdn.porter.sh/{{.VERSION}}/porter-darwin-amd64" | ||
} | ||
mgx.Must(downloads.Download(home, opts)) | ||
} | ||
|
||
runtimePath := filepath.Join(home, "runtimes", "porter-runtime") | ||
if runtimeFound, _ := pkg.IsCommandAvailable(runtimePath, version, "--version"); !runtimeFound { | ||
log.Println("Porter runtime not found at", runtimePath) | ||
opts := downloads.DownloadOptions{ | ||
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-linux-amd64", | ||
Name: "porter-runtime", | ||
Version: version, | ||
} | ||
mgx.Must(downloads.Download(runtimesDir, opts)) | ||
} | ||
} | ||
|
||
type InstallMixinOptions struct { | ||
Name string | ||
URL string | ||
Feed string | ||
Version string | ||
} | ||
|
||
// EnsureMixin installs the specified mixin. | ||
func EnsureMixin(mixin InstallMixinOptions) error { | ||
home := GetPorterHome() | ||
mixinDir := filepath.Join(home, "mixins", mixin.Name) | ||
if _, err := os.Stat(mixinDir); err == nil { | ||
log.Println("Mixin already installed:", mixin.Name) | ||
return nil | ||
} | ||
|
||
log.Println("Installing mixin:", mixin.Name) | ||
if mixin.Version == "" { | ||
mixin.Version = DefaultMixinVersion | ||
} | ||
var source string | ||
if mixin.Feed != "" { | ||
source = "--feed-url=" + mixin.Feed | ||
} else { | ||
source = "--url=" + mixin.URL | ||
} | ||
|
||
porterPath := filepath.Join(home, "porter") | ||
return shx.Run(porterPath, "mixin", "install", mixin.Name, "--version", mixin.Version, source) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package porter | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/carolynvs/magex/pkg" | ||
"github.com/carolynvs/magex/xplat" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEnsurePorter(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
wantVersion string | ||
}{ | ||
{name: "default version", wantVersion: DefaultPorterVersion}, | ||
{name: "custom version", wantVersion: "v1.0.0-alpha.10"}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tmp, err := ioutil.TempDir("", "magefiles") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmp) | ||
|
||
UsePorterHome(tmp) | ||
EnsurePorterAt(tc.wantVersion) | ||
require.FileExists(t, filepath.Join(tmp, "porter"+xplat.FileExt()), "expected the porter client to be in bin") | ||
assert.FileExists(t, filepath.Join(tmp, "runtimes", "porter-runtime"), "expected the porter runtime to be in bin") | ||
|
||
ok, err := pkg.IsCommandAvailable("porter", tc.wantVersion, "--version") | ||
require.NoError(t, err) | ||
assert.True(t, ok, "could not resolve the desired porter version") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.