|
| 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