-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
93 lines (84 loc) · 2.02 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
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/json"
"log/slog"
"os"
"strings"
)
// Config defines a set of build targets and options.
type Config struct {
OutDir string `json:"outDir"`
SrcDir string `json:"srcDir"`
MD5 bool `json:"md5"`
SHA1 bool `json:"sha1"`
SHA256 bool `json:"sha256"`
SHA512 bool `json:"sha512"`
ZipFile bool `json:"zipFile"`
Artifacts []Artifact `json:"artifacts"`
}
// NewConfig returns a new Config with default values
func NewConfig() *Config {
return &Config{
OutDir: "build",
SrcDir: ".",
MD5: false,
SHA1: true,
SHA256: false,
SHA512: false,
ZipFile: true,
Artifacts: []Artifact{},
}
}
// AddBuild adds a build target group for Go binaries.
//
// ex: myconfig.AddBuild("example.exe", "windows", "amd64")
func (c *Config) AddBuild(name, os, arch string, cgo bool, flags ...string) {
c.Artifacts = append(c.Artifacts,
Artifact{
Name: name,
OS: os,
ARCH: arch,
CGOEnabled: cgo,
Flags: flags,
})
}
// Save saves a json representation of Config to filename
//
// ex: myconfig.Save("go-github-releaser.json")
func (c *Config) Save(filename string) error {
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
// should we assume to clobber the file?
err = os.WriteFile(filename, data, 0644)
if err != nil {
return err
}
return nil
}
// Load reads filename into a Config struct
//
// ex: myconfig.Load("go-github-releaser.json")
func (c *Config) Load(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(data, c)
if err != nil {
return err
}
return nil
}
// RunChecks performs some basic checks on the config to catch gotchas.
//
// ex: myconfig.RunChecks()
func (c *Config) RunChecks() {
// check if arch is not found in the artifact name
for _, artifact := range c.Artifacts {
if !strings.Contains(artifact.Name, artifact.ARCH) {
slog.Warn("arch not found in artifact name", "artifact", artifact.Name, "arch", artifact.ARCH)
}
}
}