Skip to content

Commit 1cde3d1

Browse files
author
David Cavazos
committed
load package setup
1 parent a3666f4 commit 1cde3d1

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"io/fs"
2525
"os"
2626
"path/filepath"
27-
"regexp"
2827
"slices"
2928
"strings"
3029
)
@@ -33,6 +32,9 @@ type Config struct {
3332
// Filename to look for the root of a package.
3433
PackageFile []string `json:"package-file"`
3534

35+
// Setup filename, must be located in the same directory as the package file.
36+
SetupFile string `json:"setup-file"`
37+
3638
// Pattern to match filenames or directories.
3739
Match []string `json:"match"`
3840

@@ -43,8 +45,13 @@ type Config struct {
4345
ExcludePackages []string `json:"exclude-packages"`
4446
}
4547

46-
var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/`)
47-
var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`)
48+
type Package struct {
49+
// Package directory path.
50+
Path string `json:"path"`
51+
52+
// Package setup configurations.
53+
Setup *map[string]any `json:"setup"`
54+
}
4855

4956
// Saves the config to the given file.
5057
func (c *Config) Save(file *os.File) error {
@@ -61,18 +68,8 @@ func (c *Config) Save(file *os.File) error {
6168

6269
// LoadConfig loads the config from the given path.
6370
func LoadConfig(path string) (*Config, error) {
64-
// Read the JSONC file.
65-
sourceJsonc, err := os.ReadFile(path)
66-
if err != nil {
67-
return nil, err
68-
}
69-
70-
// Strip the comments and load the JSON.
71-
sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{})
72-
sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{})
73-
74-
var config Config
75-
err = json.Unmarshal(sourceJson, &config)
71+
// Read the config file.
72+
config, err := ReadJsonc[Config](path)
7673
if err != nil {
7774
return nil, err
7875
}
@@ -85,7 +82,7 @@ func LoadConfig(path string) (*Config, error) {
8582
config.Match = []string{"*"}
8683
}
8784

88-
return &config, nil
85+
return config, nil
8986
}
9087

9188
// Match returns true if the path matches any of the patterns.
@@ -156,12 +153,31 @@ func (c *Config) FindAllPackages(root string) ([]string, error) {
156153
// Affected returns the packages that have been affected from diffs.
157154
// If there are diffs on at leat one global file affecting all packages,
158155
// then this returns all packages matched by the config.
159-
func (c *Config) Affected(log io.Writer, diffs []string) ([]string, error) {
156+
func (c *Config) Affected(log io.Writer, diffs []string) ([]Package, error) {
160157
changed := c.Changed(log, diffs)
161158
if slices.Contains(changed, ".") {
162-
return c.FindAllPackages(".")
159+
allPackages, err := c.FindAllPackages(".")
160+
if err != nil {
161+
return nil, err
162+
}
163+
changed = allPackages
163164
}
164-
return changed, nil
165+
166+
packages := make([]Package, 0, len(changed))
167+
for _, path := range changed {
168+
pkg := Package{Path: path}
169+
if c.SetupFile != "" {
170+
setup_file := filepath.Join(path, c.SetupFile)
171+
println(setup_file)
172+
setup, err := ReadJsonc[map[string]any](setup_file)
173+
if err != nil {
174+
return nil, err
175+
}
176+
pkg.Setup = setup
177+
}
178+
packages = append(packages, pkg)
179+
}
180+
return packages, nil
165181
}
166182

167183
// Changed returns the packages that have changed.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,34 @@
1717
package config
1818

1919
import (
20+
"encoding/json"
2021
"errors"
2122
"os"
23+
"regexp"
2224
)
2325

26+
var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/`)
27+
var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`)
28+
29+
func ReadJsonc[a any](path string) (*a, error) {
30+
// Read the JSONC file.
31+
sourceJsonc, err := os.ReadFile(path)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
// Strip the comments and load the JSON.
37+
sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{})
38+
sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{})
39+
40+
var value a
41+
err = json.Unmarshal(sourceJson, &value)
42+
if err != nil {
43+
return nil, err
44+
}
45+
return &value, nil
46+
}
47+
2448
// fileExists returns true if the file exists.
2549
func fileExists(path string) bool {
2650
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {

0 commit comments

Comments
 (0)