Skip to content

Commit 02892a7

Browse files
author
David Cavazos
committed
support default values
1 parent 0917968 commit 02892a7

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ type Config struct {
3232
// Filename to look for the root of a package.
3333
PackageFile []string `json:"package-file"`
3434

35-
// Setup filename, must be located in the same directory as the package file.
36-
SetupFile string `json:"setup-file"`
35+
// Setup file, must be located in the same directory as the package file.
36+
Setup struct {
37+
FileName string `json:"filename"`
38+
Defaults map[string]any `json:"defaults"`
39+
} `json:"setup"`
3740

3841
// Pattern to match filenames or directories.
3942
Match []string `json:"match"`
@@ -69,7 +72,7 @@ func (c *Config) Save(file *os.File) error {
6972
// LoadConfig loads the config from the given path.
7073
func LoadConfig(path string) (*Config, error) {
7174
// Read the config file.
72-
config, err := ReadJsonc[Config](path)
75+
config, err := ReadJsonc[Config](path, nil)
7376
if err != nil {
7477
return nil, err
7578
}
@@ -166,10 +169,9 @@ func (c *Config) Affected(log io.Writer, diffs []string) ([]Package, error) {
166169
packages := make([]Package, 0, len(changed))
167170
for _, path := range changed {
168171
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)
172+
if c.Setup.FileName != "" {
173+
setup_file := filepath.Join(path, c.Setup.FileName)
174+
setup, err := ReadJsonc(setup_file, &c.Setup.Defaults)
173175
if err != nil {
174176
return nil, err
175177
}

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

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

29-
func ReadJsonc[a any](path string) (*a, error) {
29+
func ReadJsonc[a any](path string, defaults *a) (*a, error) {
3030
// Read the JSONC file.
3131
sourceJsonc, err := os.ReadFile(path)
3232
if err != nil {
@@ -38,6 +38,9 @@ func ReadJsonc[a any](path string) (*a, error) {
3838
sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{})
3939

4040
var value a
41+
if defaults != nil {
42+
value = *defaults
43+
}
4144
err = json.Unmarshal(sourceJson, &value)
4245
if err != nil {
4346
return nil, err

.github/config/nodejs-dev.jsonc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
{
1818
"package-file": [ "package.json" ],
19-
"setup-file": "setup.json",
19+
"setup": {
20+
"filename": "setup.json",
21+
"defaults": {
22+
"nodejs_version": 20,
23+
"secrets": null
24+
}
25+
},
2026
"ignore": [
2127
".eslintignore",
2228
".eslintrc.json",

generative-ai/snippets/setup.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"nodejs_version": 18,
23
"secrets": [
34
"CAIP_PROJECT_ID:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id",
45
"LOCATION:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location",

0 commit comments

Comments
 (0)