This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
81 lines (72 loc) · 1.86 KB
/
config.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
package heroku
import (
"crypto/sha256"
"encoding/base32"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Config struct {
Build BuildConfig
Id string
}
type BuildConfig struct {
Buildpacks []string
Packages []string
Pre []string
Post []string
Config map[string]string
}
func ReadConfig(appDir string) (Config, error) {
herokuYamlFile := filepath.Join(appDir, "heroku.yml")
_, err := os.Stat(herokuYamlFile)
if err == nil {
configBytes, err := ioutil.ReadFile(herokuYamlFile)
if err == nil {
var herokuConfig Config
yaml.Unmarshal(configBytes, &herokuConfig)
hasher := sha256.New()
hasher.Write(configBytes)
sha := strings.ToLower(base32.HexEncoding.EncodeToString(hasher.Sum(nil)))
herokuConfig.Id = strings.Replace(sha, "=", "x", -1)
return herokuConfig, nil
}
}
return Config{}, err
}
func (c *Config) ResolveBuildpacks() []string {
buildpacks := make([]string, len(c.Build.Buildpacks))
for i, buildpack := range c.Build.Buildpacks {
if strings.HasPrefix(buildpack, "https://") || strings.HasPrefix(buildpack, "http://") {
buildpacks[i] = buildpack
} else {
buildpacks[i] = fmt.Sprintf("https://buildpack-registry.s3.amazonaws.com/buildpacks/%s.tgz", buildpack)
}
}
return buildpacks
}
func (c *Config) ConstructDockerfile(stack string) string {
if len(c.Build.Pre) <= 0 && len(c.Build.Packages) <= 0 {
return ""
}
dockerfile := fmt.Sprintf("FROM %s", stack)
for _, command := range c.Build.Pre {
dockerfile += fmt.Sprintf(`
RUN %s`, command)
}
if len(c.Build.Packages) > 0 {
dockerfile += fmt.Sprintf(`
RUN apt-get update`)
}
for _, aptPackage := range c.Build.Packages {
dockerfile += fmt.Sprintf(`
RUN apt-get install %s -y`, aptPackage)
}
if len(c.Build.Post) > 0 {
fmt.Println("Warning: `post` steps in heroku.yml are not supported!")
}
return dockerfile
}