-
Notifications
You must be signed in to change notification settings - Fork 12
/
porter.go
101 lines (88 loc) · 3.06 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
91
92
93
94
95
96
97
98
99
100
101
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.1"
)
// Install the default version of porter, if porter isn't already installed
func EnsurePorter() {
home := GetPorterHome()
clientPath := filepath.Join(home, "porter"+xplat.FileExt())
if ok, _ := pkg.IsCommandAvailable(clientPath, "--version", ""); !ok {
EnsurePorterAt(DefaultPorterVersion)
}
}
// Install the specified version of porter
func EnsurePorterAt(version string) {
home := GetPorterHome()
runtimesDir := filepath.Join(home, "runtimes")
os.MkdirAll(runtimesDir, 0770)
var forceDownloadRuntime bool
clientPath := filepath.Join(home, "porter"+xplat.FileExt())
if clientFound, _ := pkg.IsCommandAvailable(clientPath, "--version", version); !clientFound {
// When we download a new client, always download a new runtime
forceDownloadRuntime = true
log.Printf("Porter %s not found at %s\n", version, 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))
}
// Only check if the runtime file _exists_ but don't try to check the version
// The binary isn't runnable in all cases because it will always be a linux binary, and the client could be windows or mac
// So we can't really check the version here...
runtimePath := filepath.Join(home, "runtimes", "porter-runtime")
if _, err := os.Stat(runtimePath); forceDownloadRuntime || os.IsNotExist(err) {
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)
}