|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/denchenko/messageflow" |
| 12 | + "github.com/denchenko/messageflow/source/asyncapi" |
| 13 | + "github.com/denchenko/messageflow/target/d2" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type Command struct { |
| 18 | + cmd *cobra.Command |
| 19 | +} |
| 20 | + |
| 21 | +// NewCommand creates a new gen-schema command |
| 22 | +func NewCommand() *Command { |
| 23 | + c := &Command{} |
| 24 | + |
| 25 | + c.cmd = &cobra.Command{ |
| 26 | + Use: "gen-schema", |
| 27 | + Short: "Generate schema from AsyncAPI files", |
| 28 | + Long: `Generate schema from AsyncAPI files and optionally format or render to output files. |
| 29 | + |
| 30 | +Example: |
| 31 | + messageflow gen-schema --target d2 --render-to-file schema.svg --asyncapi-files asyncapi.yaml`, |
| 32 | + RunE: c.run, |
| 33 | + } |
| 34 | + |
| 35 | + c.cmd.Flags().String("target", "d2", "Target type (d2)") |
| 36 | + c.cmd.Flags().String("format-to-file", "", "Output file for the formatted schema") |
| 37 | + c.cmd.Flags().String("render-to-file", "", "Output file for the rendered diagram") |
| 38 | + c.cmd.Flags().String("asyncapi-files", "", "Paths to asyncapi files separated by comma") |
| 39 | + c.cmd.Flags().String("channel", "", "Channel") |
| 40 | + c.cmd.Flags().String("service", "", "Service") |
| 41 | + c.cmd.Flags().String("format-mode", "service_channels", "Format mode") |
| 42 | + |
| 43 | + // Mark required flags |
| 44 | + err := c.cmd.MarkFlagRequired("asyncapi-files") |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("error marking asyncapi-files flag as required: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + return c |
| 50 | +} |
| 51 | + |
| 52 | +// GetCommand returns the cobra command |
| 53 | +func (c *Command) GetCommand() *cobra.Command { |
| 54 | + return c.cmd |
| 55 | +} |
| 56 | + |
| 57 | +// run executes the gen-schema command |
| 58 | +func (c *Command) run(cmd *cobra.Command, _ []string) error { |
| 59 | + targetType, err := cmd.Flags().GetString("target") |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("error getting target flag: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + formatToFile, err := cmd.Flags().GetString("format-to-file") |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("error getting format-to-file flag: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + renderToFile, err := cmd.Flags().GetString("render-to-file") |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("error getting render-to-file flag: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + asyncAPIFilesPath, err := cmd.Flags().GetString("asyncapi-files") |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("error getting asyncapi-files flag: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + channel, err := cmd.Flags().GetString("channel") |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("error getting channel flag: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + service, err := cmd.Flags().GetString("service") |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("error getting service flag: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + formatMode, err := cmd.Flags().GetString("format-mode") |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("error getting format-mode flag: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + // Validate that at least one output is specified |
| 95 | + if formatToFile == "" && renderToFile == "" { |
| 96 | + return errors.New("either --format-to-file or --render-to-file must be specified") |
| 97 | + } |
| 98 | + |
| 99 | + target, err := pickTarget(targetType) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("error picking target: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + targetCaps := target.Capabilities() |
| 105 | + |
| 106 | + if !targetCaps.Format { |
| 107 | + return errors.New("target doesn't support formatting") |
| 108 | + } |
| 109 | + |
| 110 | + if renderToFile != "" && !targetCaps.Render { |
| 111 | + return errors.New("target doesn't support render") |
| 112 | + } |
| 113 | + |
| 114 | + ctx := context.Background() |
| 115 | + |
| 116 | + filePaths := strings.Split(asyncAPIFilesPath, ",") |
| 117 | + schemas := make([]messageflow.Schema, 0, len(filePaths)) |
| 118 | + |
| 119 | + for _, filePath := range filePaths { |
| 120 | + trimmedPath := strings.TrimSpace(filePath) |
| 121 | + s, err := asyncapi.NewSource(trimmedPath) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("error creating schema source from %s: %w", trimmedPath, err) |
| 124 | + } |
| 125 | + |
| 126 | + schema, err := s.ExtractSchema(ctx) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("error extracting schema from %s: %w", trimmedPath, err) |
| 129 | + } |
| 130 | + |
| 131 | + schemas = append(schemas, schema) |
| 132 | + } |
| 133 | + |
| 134 | + schema := messageflow.MergeSchemas(schemas...) |
| 135 | + |
| 136 | + formatOpts := messageflow.FormatOptions{ |
| 137 | + Mode: messageflow.FormatMode(formatMode), |
| 138 | + Service: service, |
| 139 | + Channel: channel, |
| 140 | + } |
| 141 | + |
| 142 | + fs, err := target.FormatSchema(ctx, schema, formatOpts) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("error formatting schema: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + if formatToFile != "" { |
| 148 | + err = os.WriteFile(formatToFile, fs.Data, 0600) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("error writing to file %s: %w", formatToFile, err) |
| 151 | + } |
| 152 | + fmt.Printf("Formatted schema written to: %s\n", formatToFile) |
| 153 | + } |
| 154 | + |
| 155 | + if renderToFile != "" { |
| 156 | + diagram, err := target.RenderSchema(ctx, fs) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("error rendering schema: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + err = os.WriteFile(renderToFile, diagram, 0600) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("error writing to file %s: %w", renderToFile, err) |
| 164 | + } |
| 165 | + fmt.Printf("Rendered diagram written to: %s\n", renderToFile) |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +// pickTarget selects the appropriate target based on the target type |
| 172 | +func pickTarget(targetType string) (messageflow.Target, error) { |
| 173 | + switch targetType { |
| 174 | + case "d2": |
| 175 | + return d2.NewTarget() |
| 176 | + default: |
| 177 | + return nil, fmt.Errorf("unknown target: %s", targetType) |
| 178 | + } |
| 179 | +} |
0 commit comments