forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
47 lines (41 loc) · 1.21 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
package actionlint
import (
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v3"
)
// Config is configuration of actionlint. This struct instance is parsed from "actionlint.yaml"
// file usually put in ".github" directory.
type Config struct {
// SelfHostedRunner is configuration for self-hosted runner.
SelfHostedRunner struct {
// Labels is label names for self-hosted runner.
Labels []string `yaml:"labels"`
} `yaml:"self-hosted-runner"`
}
func parseConfig(b []byte, path string) (*Config, error) {
var c Config
if err := yaml.Unmarshal(b, &c); err != nil {
msg := strings.ReplaceAll(err.Error(), "\n", " ")
return nil, fmt.Errorf("could not parse config file %q: %s", path, msg)
}
return &c, nil
}
func readConfigFile(path string) (*Config, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read config file %q: %w", path, err)
}
return parseConfig(b, path)
}
func writeDefaultConfigFile(path string) error {
b := []byte(`self-hosted-runner:
# Labels of self-hosted runner in array of string
labels: []
`)
if err := ioutil.WriteFile(path, b, 0644); err != nil {
return fmt.Errorf("could not write default configuration file at %q: %w", path, err)
}
return nil
}