This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathherald.go
178 lines (143 loc) · 4.25 KB
/
herald.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package herald
import (
"fmt"
"github.com/hashicorp/go-getter"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
// BPBranch is which branch of the buildpack to use.
const BPBranch = "versions"
// BPTarballTemplate is the template for constructing the zipball URL.
const BPTarballTemplate = "https://github.com/heroku/heroku-buildpack-%s/archive/%s.zip"
// OwnedBuildpack is a buildpack that is owned by an owner (for notifications).
type OwnedBuildpack struct {
Name string
Owner string
}
// Buildpacks is a list of a buildpacks with their respective owners.
var Buildpacks = []OwnedBuildpack{
{Name: "python", Owner: "kennethreitz"},
{Name: "php", Owner: "kreitz@salesforce.com"},
{Name: "nodejs", Owner: "kreitz@salesforce.com"},
{Name: "ruby", Owner: "kreitz@salesforce.com"},
{Name: "jvm-common", Owner: "kreitz@salesforce.com"},
}
// Version is a type used to represent a given version of a Target.
type Version struct {
Name string
Target Target
}
// NewVersion returns a new Version instance.
func NewVersion() Version {
return Version{}
}
// Buildpack is a type which seems inherintly useful for this utility.
type Buildpack struct {
Tarball string
Path string
Name string
Owner string
}
// ZipballURI Returns the GitHub ZipBall URI for the given buildpack.
func (b Buildpack) ZipballURI() string {
return fmt.Sprintf(BPTarballTemplate, b.Name, BPBranch)
}
// Download Downloads the given buildpack to a temporary directory.
// Returns a new Buildpack object, as well as the target.
func (b *Buildpack) Download() string {
target, _ := ioutil.TempDir("", "buildpacks")
getter.Get(target, b.ZipballURI())
// The branch to base this off of.
// Set the Path.
b.Path = target + fmt.Sprintf("/heroku-buildpack-%s-%s", b.Name, BPBranch)
return b.Path
}
// String representatino of Buildpack.
func (b Buildpack) String() string {
return b.Name
}
// Determines wether a given path is a file or not.
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
return fileInfo.IsDir(), err
}
// FindVersionScripts Finds executables from a given buildpack, with globbing.
// Rerturns a slice of the Executable type.
func (b Buildpack) FindVersionScripts() []Executable {
results := []Executable{}
globResults, _ := filepath.Glob(fmt.Sprintf("%s/versions/*", b.Path))
for _, result := range globResults {
// Only yield a result if the glob result is a file.
isDirectory, _ := isDirectory(result)
if !isDirectory {
results = append(results, NewExecutable(result))
}
}
return results
}
// NewBuildpack Creates a new Buildpack type.
func NewBuildpack(name string, owner string) Buildpack {
return Buildpack{
Name: name,
Owner: owner,
}
}
// Target represents a target (e.g. buildable asset) owned by a Buildpack.
type Target struct {
Buildpack Buildpack
Name string
Versions []Version
}
// NewTarget returns a new Target instance.
func NewTarget(bp Buildpack, name string) Target {
return Target{
Buildpack: bp,
Name: name,
Versions: nil,
}
}
// Executable provided by a buildpack, for collecting version information.
type Executable struct {
Path string
}
// String representation of Executable.
func (e Executable) String() string {
sl := strings.Split(e.Path, "/")
return sl[len(sl)-1]
}
// EnsureExecutable Ensures that the given executable is… executable.
func (e Executable) EnsureExecutable() {
// TODO: Chmod to the proper permissions.
if err := os.Chmod(e.Path, 0777); err != nil {
// TODO: return error, etc.
log.Fatal(err)
}
}
// Execute Executes the given executable, and returns results.
func (e Executable) Execute() ([]string, error) {
out, err := exec.Command(e.Path).Output()
if err != nil {
// TODO: Update this to return, etc.
log.Fatal(err)
}
return strings.Split(strings.Trim(string(out), "\n"), "\n"), err
}
// NewExecutable Creates a new Executable type.
func NewExecutable(path string) Executable {
return Executable{
Path: path,
}
}
// GetBuildpacks Generates a list of Buildpack objects, to be used by this utility.
func GetBuildpacks() []Buildpack {
// Download and unpack each Zipball from GitHub.
buildpacks := []Buildpack{}
for _, bp := range Buildpacks {
buildpacks = append(buildpacks, NewBuildpack(bp.Name, bp.Owner))
}
return buildpacks
}