Skip to content

Commit eccfdff

Browse files
feat: check generator schema version (#6)
Signed-off-by: Mathew Wicks <[email protected]>
1 parent 357ebd8 commit eccfdff

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test:
8080
.PHONY: lint
8181
lint: check-golangci-lint
8282
@echo "********** running golangci-lint **********"
83-
@golangci-lint run ./...
83+
@golangci-lint run --verbose ./...
8484

8585
# ------------------------------------------------------------------------------
8686
# lint-fix
@@ -92,7 +92,7 @@ lint-fix: check-golangci-lint
9292
@goimports -local github.com/deployKF/cli -w $(shell find . -type f -name '*.go' -not -path "./vendor/*")
9393

9494
@echo "********** running golangci-lint --fix **********"
95-
@golangci-lint run --fix ./...
95+
@golangci-lint run --verbose --fix ./...
9696

9797
# ------------------------------------------------------------------------------
9898
# clean

cmd/deploykf/generate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ func (o *generateOptions) run(out io.Writer) error {
161161
templatesPath := filepath.Join(tempSourcePath, "templates")
162162
helpersPath := filepath.Join(tempSourcePath, "helpers")
163163
defaultValuesPath := filepath.Join(tempSourcePath, "default_values.yaml")
164+
markerPath := filepath.Join(tempSourcePath, ".deploykf_generator")
165+
166+
// verify the generator source is valid, and is supported by this version of the CLI
167+
err = generate.VerifyGeneratorSource(templatesPath, helpersPath, defaultValuesPath, markerPath)
168+
if err != nil {
169+
return err
170+
}
164171

165172
// GENERATOR PHASE 1: render `.gomplateignore_template` files
166173
// - note, we are rendering the `.gomplateignore` files into the generator source

internal/generate/verify.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package generate
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
)
9+
10+
type GeneratorMarker struct {
11+
GeneratorSchema string `json:"generator_schema"`
12+
}
13+
14+
// GetGeneratorSchemaVersion returns the `generator_schema` version from the specified marker file.
15+
func GetGeneratorSchemaVersion(markerPath string) (string, error) {
16+
bytes, err := os.ReadFile(markerPath)
17+
if err != nil {
18+
return "", fmt.Errorf("failed to read generator marker file: %v", err)
19+
}
20+
21+
var marker GeneratorMarker
22+
err = json.Unmarshal(bytes, &marker)
23+
if err != nil {
24+
return "", fmt.Errorf("failed to parse generator marker file: %v", err)
25+
}
26+
27+
if marker.GeneratorSchema == "" {
28+
return "", errors.New("generator marker file is missing 'generator_schema' field")
29+
}
30+
31+
return marker.GeneratorSchema, nil
32+
}
33+
34+
// VerifyGeneratorSource verifies that the specified paths make a valid generator source,
35+
// and that this version of the CLI supports the generator schema version.
36+
func VerifyGeneratorSource(templatesPath string, helpersPath string, defaultValuesPath string, markerPath string) error {
37+
38+
// Verify that the marker file exists
39+
markerFileExists, err := FileExists(markerPath)
40+
if err != nil {
41+
return err
42+
}
43+
if !markerFileExists {
44+
return fmt.Errorf("invalid generator source: marker file is missing")
45+
}
46+
47+
// Verify that we support the generator schema version
48+
generatorSchemaVersion, err := GetGeneratorSchemaVersion(markerPath)
49+
if err != nil {
50+
return err
51+
}
52+
if generatorSchemaVersion != "v1" {
53+
return fmt.Errorf("invalid generator source: unsupported schema version '%s'", generatorSchemaVersion)
54+
}
55+
56+
// Verify that the templates directory exists
57+
templatesDirExists, err := DirectoryExists(templatesPath)
58+
if err != nil {
59+
return err
60+
}
61+
if !templatesDirExists {
62+
return fmt.Errorf("invalid generator source: templates directory is missing")
63+
}
64+
65+
// Verify that the helpers directory exists
66+
helpersDirExists, err := DirectoryExists(helpersPath)
67+
if err != nil {
68+
return err
69+
}
70+
if !helpersDirExists {
71+
return fmt.Errorf("invalid generator source: helpers directory is missing")
72+
}
73+
74+
// Verify that the default values file exists
75+
defaultValuesFileExists, err := FileExists(defaultValuesPath)
76+
if err != nil {
77+
return err
78+
}
79+
if !defaultValuesFileExists {
80+
return fmt.Errorf("invalid generator source: default values file is missing")
81+
}
82+
83+
return nil
84+
}

0 commit comments

Comments
 (0)