Skip to content

Commit d7bc543

Browse files
author
David Cavazos
committed
handle packages without setup file
1 parent 02892a7 commit d7bc543

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

.github/cloud-samples-tools/pkg/config/config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func (c *Config) Save(file *os.File) error {
7272
// LoadConfig loads the config from the given path.
7373
func LoadConfig(path string) (*Config, error) {
7474
// Read the config file.
75-
config, err := ReadJsonc[Config](path, nil)
75+
var config Config
76+
err := ReadJsonc(path, &config)
7677
if err != nil {
7778
return nil, err
7879
}
@@ -85,7 +86,7 @@ func LoadConfig(path string) (*Config, error) {
8586
config.Match = []string{"*"}
8687
}
8788

88-
return config, nil
89+
return &config, nil
8990
}
9091

9192
// Match returns true if the path matches any of the patterns.
@@ -168,14 +169,15 @@ func (c *Config) Affected(log io.Writer, diffs []string) ([]Package, error) {
168169

169170
packages := make([]Package, 0, len(changed))
170171
for _, path := range changed {
171-
pkg := Package{Path: path}
172+
pkg := Package{Path: path, Setup: &c.Setup.Defaults}
172173
if c.Setup.FileName != "" {
173174
setup_file := filepath.Join(path, c.Setup.FileName)
174-
setup, err := ReadJsonc(setup_file, &c.Setup.Defaults)
175-
if err != nil {
176-
return nil, err
175+
if fileExists(setup_file) {
176+
err := ReadJsonc(setup_file, pkg.Setup)
177+
if err != nil {
178+
return nil, err
179+
}
177180
}
178-
pkg.Setup = setup
179181
}
180182
packages = append(packages, pkg)
181183
}

.github/cloud-samples-tools/pkg/config/utils.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,17 @@ import (
2626
var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/`)
2727
var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`)
2828

29-
func ReadJsonc[a any](path string, defaults *a) (*a, error) {
29+
func ReadJsonc[a any](path string, ref *a) error {
3030
// Read the JSONC file.
3131
sourceJsonc, err := os.ReadFile(path)
3232
if err != nil {
33-
return nil, err
33+
return err
3434
}
3535

3636
// Strip the comments and load the JSON.
3737
sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{})
3838
sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{})
39-
40-
var value a
41-
if defaults != nil {
42-
value = *defaults
43-
}
44-
err = json.Unmarshal(sourceJson, &value)
45-
if err != nil {
46-
return nil, err
47-
}
48-
return &value, nil
39+
return json.Unmarshal(sourceJson, ref)
4940
}
5041

5142
// fileExists returns true if the file exists.

0 commit comments

Comments
 (0)