forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (51 loc) · 1.9 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
package actionlint
import (
"fmt"
"os"
"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"`
// ConfigVariables is names of configuration variables used in the checked workflows. When this value is nil,
// property names of `vars` context will not be checked. Otherwise actionlint will report a name which is not
// listed here as undefined config variables.
// https://docs.github.com/en/actions/learn-github-actions/variables
ConfigVariables []string `yaml:"config-variables"`
}
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
}
// ReadConfigFile reads actionlint config file (actionlint.yaml) from the given file path.
func ReadConfigFile(path string) (*Config, error) {
b, err := os.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 strings.
labels: []
# Configuration variables in array of strings defined in your repository or
# organization. ` + "`null`" + ` means disabling configuration variables check.
# Empty array means no configuration variable is allowed.
config-variables: null
`)
if err := os.WriteFile(path, b, 0644); err != nil {
return fmt.Errorf("could not write default configuration file at %q: %w", path, err)
}
return nil
}