|
| 1 | +/* |
| 2 | +Copyright © 2024 diggerhq |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "reflect" |
| 23 | + |
| 24 | + "github.com/diggerhq/digger/libs/digger_config" |
| 25 | + "github.com/invopop/jsonschema" |
| 26 | + "github.com/spf13/cobra" |
| 27 | +) |
| 28 | + |
| 29 | +var outputFile string |
| 30 | + |
| 31 | +// schemaCmd represents the schema command |
| 32 | +var schemaCmd = &cobra.Command{ |
| 33 | + Use: "schema", |
| 34 | + Short: "Generate JSON Schema for digger.yml", |
| 35 | + Long: `Generate a JSON Schema from the digger.yml configuration structure. |
| 36 | +
|
| 37 | +Examples: |
| 38 | + dgctl schema # Output schema to stdout |
| 39 | + dgctl schema -o schema.json # Write schema to file |
| 40 | + dgctl schema --output docs/schema.json`, |
| 41 | + Run: func(cmd *cobra.Command, args []string) { |
| 42 | + schema := generateSchema() |
| 43 | + |
| 44 | + jsonBytes, err := json.MarshalIndent(schema, "", " ") |
| 45 | + if err != nil { |
| 46 | + fmt.Fprintf(os.Stderr, "Error marshaling schema: %v\n", err) |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + if outputFile != "" { |
| 51 | + err = os.WriteFile(outputFile, jsonBytes, 0644) |
| 52 | + if err != nil { |
| 53 | + fmt.Fprintf(os.Stderr, "Error writing schema to file: %v\n", err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + fmt.Printf("Schema written to %s\n", outputFile) |
| 57 | + } else { |
| 58 | + fmt.Println(string(jsonBytes)) |
| 59 | + } |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +func generateSchema() *jsonschema.Schema { |
| 64 | + r := &jsonschema.Reflector{ |
| 65 | + KeyNamer: jsonschema.ToSnakeCase, |
| 66 | + RequiredFromJSONSchemaTags: true, |
| 67 | + AllowAdditionalProperties: true, |
| 68 | + DoNotReference: true, |
| 69 | + Namer: func(t reflect.Type) string { |
| 70 | + return t.Name() |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + schema := r.Reflect(&digger_config.DiggerConfigYaml{}) |
| 75 | + schema.ID = jsonschema.ID("https://digger.dev/schemas/digger.yml.json") |
| 76 | + |
| 77 | + return schema |
| 78 | +} |
| 79 | + |
| 80 | +func init() { |
| 81 | + rootCmd.AddCommand(schemaCmd) |
| 82 | + schemaCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file path (default: stdout)") |
| 83 | +} |
0 commit comments