-
Notifications
You must be signed in to change notification settings - Fork 12
/
porter.go
90 lines (79 loc) · 2.57 KB
/
porter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)
}