-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuildpack_config.go
More file actions
76 lines (63 loc) · 1.89 KB
/
buildpack_config.go
File metadata and controls
76 lines (63 loc) · 1.89 KB
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
package internal
import (
"fmt"
"os"
"github.com/pelletier/go-toml/v2"
)
type BuildpackConfig struct {
API interface{} `toml:"api"`
Buildpack interface{} `toml:"buildpack"`
Metadata interface{} `toml:"metadata"`
Order []BuildpackConfigOrder `toml:"order"`
Stacks []BuildpackConfigStack `toml:"stacks,omitempty"`
Targets []BuildpackConfigTarget `toml:"targets,omitempty"`
}
type BuildpackConfigOrder struct {
Group []BuildpackConfigOrderGroup `toml:"group"`
}
type BuildpackConfigOrderGroup struct {
ID string `toml:"id"`
Version string `toml:"version,omitempty"`
Optional bool `toml:"optional,omitempty"`
}
type BuildpackConfigStack struct {
ID string `toml:"id"`
Mixins []string `toml:"mixins,omitempty"`
}
type BuildpackConfigTarget struct {
OS string `toml:"os,omitempty"`
Arch string `toml:"arch,omitempty"`
}
func ParseBuildpackConfig(path string) (BuildpackConfig, error) {
file, err := os.Open(path)
if err != nil {
return BuildpackConfig{}, fmt.Errorf("failed to open buildpack config file: %w", err)
}
defer func() {
if err2 := file.Close(); err2 != nil && err == nil {
err = err2
}
}()
var config BuildpackConfig
err = toml.NewDecoder(file).Decode(&config)
if err != nil {
return BuildpackConfig{}, fmt.Errorf("failed to parse buildpack config: %w", err)
}
return config, err // err should be nil here, but return err to catch deferred error
}
func OverwriteBuildpackConfig(path string, config BuildpackConfig) error {
file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open buildpack config file: %w", err)
}
defer func() {
if err2 := file.Close(); err2 != nil && err == nil {
err = err2
}
}()
err = toml.NewEncoder(file).Encode(config)
if err != nil {
return fmt.Errorf("failed to write buildpack config: %w", err)
}
return nil
}