|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/getarcaneapp/arcane/backend/internal/configschema" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var configSchemaCmd = &cobra.Command{ |
| 12 | + Use: "config-schema", |
| 13 | + Short: "Export the config/settings schema for docs", |
| 14 | + Long: "Export the canonical JSON schema for runtime config and settings environment overrides", |
| 15 | + Run: func(cmd *cobra.Command, args []string) { |
| 16 | + outputFile, err := cmd.Flags().GetString("output") |
| 17 | + if err != nil { |
| 18 | + fmt.Fprintf(os.Stderr, "Error reading --output flag: %v\n", err) |
| 19 | + os.Exit(1) |
| 20 | + } |
| 21 | + |
| 22 | + sourceRoot, err := cmd.Flags().GetString("source-root") |
| 23 | + if err != nil { |
| 24 | + fmt.Fprintf(os.Stderr, "Error reading --source-root flag: %v\n", err) |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | + |
| 28 | + doc, err := configschema.GenerateWithSourceRoot(sourceRoot) |
| 29 | + if err != nil { |
| 30 | + fmt.Fprintf(os.Stderr, "Error generating config schema: %v\n", err) |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + |
| 34 | + output, err := configschema.MarshalJSON(doc) |
| 35 | + if err != nil { |
| 36 | + fmt.Fprintf(os.Stderr, "Error encoding config schema: %v\n", err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + |
| 40 | + if outputFile != "" { |
| 41 | + if err := os.WriteFile(outputFile, output, 0o600); err != nil { |
| 42 | + fmt.Fprintf(os.Stderr, "Error writing file: %v\n", err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + fmt.Fprintf(os.Stderr, "Config schema written to %s\n", outputFile) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + fmt.Print(string(output)) |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +func init() { |
| 54 | + configSchemaCmd.Flags().StringP("output", "o", "", "Output file (default: stdout)") |
| 55 | + configSchemaCmd.Flags().String("source-root", "", "Repository root or backend source root (default: auto-detect from working directory)") |
| 56 | + rootCmd.AddCommand(configSchemaCmd) |
| 57 | +} |
0 commit comments