forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
76 lines (69 loc) · 2.39 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
package actionlint
import (
"fmt"
"os"
"path/filepath"
"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)
}
// loadRepoConfig reads config file from the repository's .github/actionlint.yml or
// .github/actionlint.yaml.
func loadRepoConfig(root string) (*Config, error) {
for _, f := range []string{"actionlint.yaml", "actionlint.yml"} {
path := filepath.Join(root, ".github", f)
b, err := os.ReadFile(path)
if err != nil {
continue // file does not exist
}
cfg, err := parseConfig(b, path)
if err != nil {
return nil, err
}
return cfg, nil
}
return nil, nil
}
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
}