Skip to content

Commit 7ae99d2

Browse files
Copilotpelikhan
andcommitted
Extract content extractors to separate file (Phase 2)
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 445e76e commit 7ae99d2

2 files changed

Lines changed: 155 additions & 146 deletions

File tree

pkg/parser/content_extractor.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package parser
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
7+
"github.com/goccy/go-yaml"
8+
)
9+
10+
// extractToolsFromContent extracts tools and mcp-servers sections from frontmatter as JSON string
11+
func extractToolsFromContent(content string) (string, error) {
12+
result, err := ExtractFrontmatterFromContent(content)
13+
if err != nil {
14+
return "{}", nil // Return empty object on error to match bash behavior
15+
}
16+
17+
// Create a map to hold the merged result
18+
extracted := make(map[string]any)
19+
20+
// Helper function to merge a field into extracted map
21+
mergeField := func(fieldName string) {
22+
if fieldValue, exists := result.Frontmatter[fieldName]; exists {
23+
if fieldMap, ok := fieldValue.(map[string]any); ok {
24+
for key, value := range fieldMap {
25+
extracted[key] = value
26+
}
27+
}
28+
}
29+
}
30+
31+
// Extract and merge tools section (tools are stored as tool_name: tool_config)
32+
mergeField("tools")
33+
34+
// Extract and merge mcp-servers section (mcp-servers are stored as server_name: server_config)
35+
mergeField("mcp-servers")
36+
37+
// If nothing was extracted, return empty object
38+
if len(extracted) == 0 {
39+
return "{}", nil
40+
}
41+
42+
// Convert to JSON string
43+
extractedJSON, err := json.Marshal(extracted)
44+
if err != nil {
45+
return "{}", nil
46+
}
47+
48+
return strings.TrimSpace(string(extractedJSON)), nil
49+
}
50+
51+
// extractSafeOutputsFromContent extracts safe-outputs section from frontmatter as JSON string
52+
func extractSafeOutputsFromContent(content string) (string, error) {
53+
return extractFrontmatterField(content, "safe-outputs", "{}")
54+
}
55+
56+
// extractSafeInputsFromContent extracts safe-inputs section from frontmatter as JSON string
57+
func extractSafeInputsFromContent(content string) (string, error) {
58+
return extractFrontmatterField(content, "safe-inputs", "{}")
59+
}
60+
61+
// extractMCPServersFromContent extracts mcp-servers section from frontmatter as JSON string
62+
func extractMCPServersFromContent(content string) (string, error) {
63+
return extractFrontmatterField(content, "mcp-servers", "{}")
64+
}
65+
66+
// extractStepsFromContent extracts steps section from frontmatter as YAML string
67+
func extractStepsFromContent(content string) (string, error) {
68+
result, err := ExtractFrontmatterFromContent(content)
69+
if err != nil {
70+
return "", nil // Return empty string on error
71+
}
72+
73+
// Extract steps section
74+
steps, exists := result.Frontmatter["steps"]
75+
if !exists {
76+
return "", nil
77+
}
78+
79+
// Convert to YAML string (similar to how CustomSteps are handled in compiler)
80+
stepsYAML, err := yaml.Marshal(steps)
81+
if err != nil {
82+
return "", nil
83+
}
84+
85+
return strings.TrimSpace(string(stepsYAML)), nil
86+
}
87+
88+
// extractEngineFromContent extracts engine section from frontmatter as JSON string
89+
func extractEngineFromContent(content string) (string, error) {
90+
return extractFrontmatterField(content, "engine", "")
91+
}
92+
93+
// extractRuntimesFromContent extracts runtimes section from frontmatter as JSON string
94+
func extractRuntimesFromContent(content string) (string, error) {
95+
return extractFrontmatterField(content, "runtimes", "{}")
96+
}
97+
98+
// extractServicesFromContent extracts services section from frontmatter as YAML string
99+
func extractServicesFromContent(content string) (string, error) {
100+
result, err := ExtractFrontmatterFromContent(content)
101+
if err != nil {
102+
return "", nil // Return empty string on error
103+
}
104+
105+
// Extract services section
106+
services, exists := result.Frontmatter["services"]
107+
if !exists {
108+
return "", nil
109+
}
110+
111+
// Convert to YAML string (similar to how steps are handled)
112+
servicesYAML, err := yaml.Marshal(services)
113+
if err != nil {
114+
return "", nil
115+
}
116+
117+
return strings.TrimSpace(string(servicesYAML)), nil
118+
}
119+
120+
// extractNetworkFromContent extracts network section from frontmatter as JSON string
121+
func extractNetworkFromContent(content string) (string, error) {
122+
return extractFrontmatterField(content, "network", "{}")
123+
}
124+
125+
// ExtractPermissionsFromContent extracts permissions section from frontmatter as JSON string
126+
func ExtractPermissionsFromContent(content string) (string, error) {
127+
return extractFrontmatterField(content, "permissions", "{}")
128+
}
129+
130+
// extractSecretMaskingFromContent extracts secret-masking section from frontmatter as JSON string
131+
func extractSecretMaskingFromContent(content string) (string, error) {
132+
return extractFrontmatterField(content, "secret-masking", "{}")
133+
}
134+
135+
// extractFrontmatterField extracts a specific field from frontmatter as JSON string
136+
func extractFrontmatterField(content, fieldName, emptyValue string) (string, error) {
137+
result, err := ExtractFrontmatterFromContent(content)
138+
if err != nil {
139+
return emptyValue, nil // Return empty value on error
140+
}
141+
142+
// Extract the requested field
143+
fieldValue, exists := result.Frontmatter[fieldName]
144+
if !exists {
145+
return emptyValue, nil
146+
}
147+
148+
// Convert to JSON string
149+
fieldJSON, err := json.Marshal(fieldValue)
150+
if err != nil {
151+
return emptyValue, nil
152+
}
153+
154+
return strings.TrimSpace(string(fieldJSON)), nil
155+
}

pkg/parser/frontmatter.go

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/githubnext/gh-aw/pkg/console"
1313
"github.com/githubnext/gh-aw/pkg/logger"
14-
"github.com/goccy/go-yaml"
1514
)
1615

1716
var log = logger.New("parser:frontmatter")
@@ -682,151 +681,6 @@ func processIncludedFileWithVisited(filePath, sectionName string, extractTools b
682681
}
683682

684683
// extractToolsFromContent extracts tools and mcp-servers sections from frontmatter as merged JSON string
685-
func extractToolsFromContent(content string) (string, error) {
686-
result, err := ExtractFrontmatterFromContent(content)
687-
if err != nil {
688-
return "{}", nil // Return empty object on error to match bash behavior
689-
}
690-
691-
// Create a map to hold the merged result
692-
extracted := make(map[string]any)
693-
694-
// Helper function to merge a field into extracted map
695-
mergeField := func(fieldName string) {
696-
if fieldValue, exists := result.Frontmatter[fieldName]; exists {
697-
if fieldMap, ok := fieldValue.(map[string]any); ok {
698-
for key, value := range fieldMap {
699-
extracted[key] = value
700-
}
701-
}
702-
}
703-
}
704-
705-
// Extract and merge tools section (tools are stored as tool_name: tool_config)
706-
mergeField("tools")
707-
708-
// Extract and merge mcp-servers section (mcp-servers are stored as server_name: server_config)
709-
mergeField("mcp-servers")
710-
711-
// If nothing was extracted, return empty object
712-
if len(extracted) == 0 {
713-
return "{}", nil
714-
}
715-
716-
// Convert to JSON string
717-
extractedJSON, err := json.Marshal(extracted)
718-
if err != nil {
719-
return "{}", nil
720-
}
721-
722-
return strings.TrimSpace(string(extractedJSON)), nil
723-
}
724-
725-
// extractSafeOutputsFromContent extracts safe-outputs section from frontmatter as JSON string
726-
func extractSafeOutputsFromContent(content string) (string, error) {
727-
return extractFrontmatterField(content, "safe-outputs", "{}")
728-
}
729-
730-
// extractSafeInputsFromContent extracts safe-inputs section from frontmatter as JSON string
731-
func extractSafeInputsFromContent(content string) (string, error) {
732-
return extractFrontmatterField(content, "safe-inputs", "{}")
733-
}
734-
735-
// extractMCPServersFromContent extracts mcp-servers section from frontmatter as JSON string
736-
func extractMCPServersFromContent(content string) (string, error) {
737-
return extractFrontmatterField(content, "mcp-servers", "{}")
738-
}
739-
740-
// extractStepsFromContent extracts steps section from frontmatter as YAML string
741-
func extractStepsFromContent(content string) (string, error) {
742-
result, err := ExtractFrontmatterFromContent(content)
743-
if err != nil {
744-
return "", nil // Return empty string on error
745-
}
746-
747-
// Extract steps section
748-
steps, exists := result.Frontmatter["steps"]
749-
if !exists {
750-
return "", nil
751-
}
752-
753-
// Convert to YAML string (similar to how CustomSteps are handled in compiler)
754-
stepsYAML, err := yaml.Marshal(steps)
755-
if err != nil {
756-
return "", nil
757-
}
758-
759-
return strings.TrimSpace(string(stepsYAML)), nil
760-
}
761-
762-
// extractEngineFromContent extracts engine section from frontmatter as JSON string
763-
func extractEngineFromContent(content string) (string, error) {
764-
return extractFrontmatterField(content, "engine", "")
765-
}
766-
767-
// extractRuntimesFromContent extracts runtimes section from frontmatter as JSON string
768-
func extractRuntimesFromContent(content string) (string, error) {
769-
return extractFrontmatterField(content, "runtimes", "{}")
770-
}
771-
772-
// extractServicesFromContent extracts services section from frontmatter as YAML string
773-
func extractServicesFromContent(content string) (string, error) {
774-
result, err := ExtractFrontmatterFromContent(content)
775-
if err != nil {
776-
return "", nil // Return empty string on error
777-
}
778-
779-
// Extract services section
780-
services, exists := result.Frontmatter["services"]
781-
if !exists {
782-
return "", nil
783-
}
784-
785-
// Convert to YAML string (similar to how steps are handled)
786-
servicesYAML, err := yaml.Marshal(services)
787-
if err != nil {
788-
return "", nil
789-
}
790-
791-
return strings.TrimSpace(string(servicesYAML)), nil
792-
}
793-
794-
// extractNetworkFromContent extracts network section from frontmatter as JSON string
795-
func extractNetworkFromContent(content string) (string, error) {
796-
return extractFrontmatterField(content, "network", "{}")
797-
}
798-
799-
// ExtractPermissionsFromContent extracts permissions section from frontmatter as JSON string
800-
func ExtractPermissionsFromContent(content string) (string, error) {
801-
return extractFrontmatterField(content, "permissions", "{}")
802-
}
803-
804-
// extractSecretMaskingFromContent extracts secret-masking section from frontmatter as JSON string
805-
func extractSecretMaskingFromContent(content string) (string, error) {
806-
return extractFrontmatterField(content, "secret-masking", "{}")
807-
}
808-
809-
// extractFrontmatterField extracts a specific field from frontmatter as JSON string
810-
func extractFrontmatterField(content, fieldName, emptyValue string) (string, error) {
811-
result, err := ExtractFrontmatterFromContent(content)
812-
if err != nil {
813-
return emptyValue, nil // Return empty value on error
814-
}
815-
816-
// Extract the requested field
817-
fieldValue, exists := result.Frontmatter[fieldName]
818-
if !exists {
819-
return emptyValue, nil
820-
}
821-
822-
// Convert to JSON string
823-
fieldJSON, err := json.Marshal(fieldValue)
824-
if err != nil {
825-
return emptyValue, nil
826-
}
827-
828-
return strings.TrimSpace(string(fieldJSON)), nil
829-
}
830684

831685
// ExpandIncludes recursively expands @include and @import directives until no more remain
832686
// This matches the bash expand_includes function behavior

0 commit comments

Comments
 (0)