-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
89 lines (74 loc) · 2.18 KB
/
metadata.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
package slugcmplr
import (
"context"
"fmt"
"sort"
heroku "github.com/heroku/heroku-go/v5"
)
// MetadataCmd wraps up all the information required to fetch metadata require
// for compilation of application.
type MetadataCmd struct {
Heroku *heroku.Service
Application string
SourceDir string
BuildDir string
}
// MetadataResult contains the result of resolving metadata for an application.
type MetadataResult struct {
ApplicationName string
Stack string
Buildpacks []*BuildpackReference
ConfigVars map[string]string
SourceVersion string
}
// Execute fetches the applications name, stack, buildpacks, and config
// variables. It will also resolve the current HEAD commit of the source to be
// compiled.
func (m *MetadataCmd) Execute(ctx context.Context, _ Outputter) (*MetadataResult, error) {
commit, err := Commit(m.SourceDir)
if err != nil {
return nil, fmt.Errorf("failed to resolve commit: %w", err)
}
app, err := m.Heroku.AppInfo(ctx, m.Application)
if err != nil {
return nil, fmt.Errorf("failed to fetch app info: %w", err)
}
conf, err := m.Heroku.ConfigVarInfoForApp(ctx, m.Application)
if err != nil {
return nil, fmt.Errorf("failed to fetch app configuration: %w", err)
}
bpi, err := m.Heroku.BuildpackInstallationList(ctx, m.Application, nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch app buildpacks: %w", err)
}
return &MetadataResult{
ApplicationName: app.Name,
Stack: app.Stack.Name,
Buildpacks: buildBuildpacks(bpi),
ConfigVars: buildConfigVars(conf),
SourceVersion: commit,
}, nil
}
func buildConfigVars(conf heroku.ConfigVarInfoForAppResult) map[string]string {
configVars := make(map[string]string, len(conf))
for k, v := range conf {
if v == nil {
continue
}
configVars[k] = *v
}
return configVars
}
func buildBuildpacks(bpi heroku.BuildpackInstallationListResult) []*BuildpackReference {
sort.Slice(bpi, func(a, b int) bool {
return bpi[a].Ordinal < bpi[b].Ordinal
})
buildpacks := make([]*BuildpackReference, len(bpi))
for i, bp := range bpi {
buildpacks[i] = &BuildpackReference{
Name: bp.Buildpack.Name,
URL: bp.Buildpack.URL,
}
}
return buildpacks
}