Skip to content

Commit 59b2a3e

Browse files
committed
feat: scan dir for asyncapi files
1 parent 79fc1fb commit 59b2a3e

1 file changed

Lines changed: 78 additions & 11 deletions

File tree

  • cmd/messageflow/commands/docs

cmd/messageflow/commands/docs/docs.go

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package docs
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"log"
77
"os"
8+
"path/filepath"
89
"strings"
910

1011
"github.com/denchenko/messageflow/internal/docs"
1112
"github.com/denchenko/messageflow/pkg/schema"
1213
"github.com/denchenko/messageflow/pkg/schema/target/d2"
1314
"github.com/spf13/cobra"
15+
"gopkg.in/yaml.v3"
1416
)
1517

1618
type Command struct {
@@ -30,15 +32,11 @@ Example:
3032
RunE: c.run,
3133
}
3234

35+
c.cmd.Flags().String("dir", "", "Path to dir to scan asyncapi files automatically")
3336
c.cmd.Flags().String("asyncapi-files", "", "Paths to asyncapi files separated by comma")
3437
c.cmd.Flags().String("output", ".", "Output directory for generated documentation")
3538
c.cmd.Flags().String("title", "Message Flow", "Title of the documentation")
3639

37-
err := c.cmd.MarkFlagRequired("asyncapi-files")
38-
if err != nil {
39-
log.Fatalf("error marking asyncapi-files flag as required: %v", err)
40-
}
41-
4240
return c
4341
}
4442

@@ -53,9 +51,9 @@ func (c *Command) run(cmd *cobra.Command, _ []string) error {
5351
return fmt.Errorf("error getting title flag: %w", err)
5452
}
5553

56-
asyncAPIFilesPath, err := cmd.Flags().GetString("asyncapi-files")
54+
asyncAPIFilesPaths, err := getAsyncAPIFilesPaths(cmd)
5755
if err != nil {
58-
return fmt.Errorf("error getting asyncapi-files flag: %w", err)
56+
return fmt.Errorf("error getting asyncapi files paths: %w", err)
5957
}
6058

6159
outputDir, err := cmd.Flags().GetString("output")
@@ -69,9 +67,7 @@ func (c *Command) run(cmd *cobra.Command, _ []string) error {
6967

7068
ctx := context.Background()
7169

72-
filePaths := strings.Split(asyncAPIFilesPath, ",")
73-
74-
s, err := schema.Load(ctx, filePaths)
70+
s, err := schema.Load(ctx, asyncAPIFilesPaths)
7571
if err != nil {
7672
return fmt.Errorf("error loading schema from files: %w", err)
7773
}
@@ -89,3 +85,74 @@ func (c *Command) run(cmd *cobra.Command, _ []string) error {
8985

9086
return nil
9187
}
88+
89+
func getAsyncAPIFilesPaths(cmd *cobra.Command) ([]string, error) {
90+
asyncAPIFilesPath, err := cmd.Flags().GetString("asyncapi-files")
91+
if err != nil {
92+
return nil, fmt.Errorf("error getting asyncapi-files flag: %w", err)
93+
}
94+
95+
if asyncAPIFilesPath != "" {
96+
return strings.Split(asyncAPIFilesPath, ","), nil
97+
}
98+
99+
asyncAPIFilesDir, err := cmd.Flags().GetString("dir")
100+
if err != nil {
101+
return nil, fmt.Errorf("error getting dir flag: %w", err)
102+
}
103+
104+
if asyncAPIFilesDir == "" {
105+
return nil, errors.New("provide either asyncapi-files or dir")
106+
}
107+
108+
return asyncAPIFilesFromDir(asyncAPIFilesDir)
109+
}
110+
111+
func asyncAPIFilesFromDir(dir string) ([]string, error) {
112+
fmt.Println("Scanning directory for AsyncAPI files:", dir)
113+
114+
var asyncAPIFiles []string
115+
116+
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
117+
if err != nil {
118+
return err
119+
}
120+
121+
if d.IsDir() {
122+
return nil
123+
}
124+
125+
ext := strings.ToLower(filepath.Ext(path))
126+
if ext != ".yml" && ext != ".yaml" {
127+
return nil
128+
}
129+
130+
content, err := os.ReadFile(path)
131+
if err != nil {
132+
return fmt.Errorf("error reading file %s: %w", path, err)
133+
}
134+
135+
var yamlDoc map[string]interface{}
136+
if err := yaml.Unmarshal(content, &yamlDoc); err != nil {
137+
return fmt.Errorf("error unmarshalling yaml file %s: %w", path, err)
138+
}
139+
140+
if _, hasAsyncAPI := yamlDoc["asyncapi"]; hasAsyncAPI {
141+
asyncAPIFiles = append(asyncAPIFiles, path)
142+
}
143+
144+
return nil
145+
})
146+
147+
if err != nil {
148+
return nil, fmt.Errorf("error walking directory %s: %w", dir, err)
149+
}
150+
151+
if len(asyncAPIFiles) == 0 {
152+
return nil, fmt.Errorf("no AsyncAPI specification files found in directory %s", dir)
153+
}
154+
155+
fmt.Println("Found AsyncAPI files:", asyncAPIFiles)
156+
157+
return asyncAPIFiles, nil
158+
}

0 commit comments

Comments
 (0)