-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto-gen commands.go using schemas.cue as authority
- Loading branch information
1 parent
c6099a9
commit 5aba0b7
Showing
6 changed files
with
154 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/format" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"cuelang.org/go/cue/cuecontext" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
log.Fatal("Usage: gen <schema.cue>") | ||
} | ||
|
||
// Get the project root directory (go up two levels from schema file) | ||
schemaPath := os.Args[1] | ||
projectRoot := filepath.Clean(filepath.Join(filepath.Dir(schemaPath), "..")) | ||
|
||
// Target should be in cmd/commands.go relative to project root | ||
outputFile := filepath.Join(projectRoot, "cmd", "commands.go") | ||
|
||
if err := generateCommands(schemaPath, outputFile); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func generateCommands(schemaFile, outputFile string) error { | ||
content, err := os.ReadFile(schemaFile) | ||
if err != nil { | ||
return fmt.Errorf("error reading schema file: %w", err) | ||
} | ||
|
||
ctx := cuecontext.New() | ||
value := ctx.CompileBytes(content) | ||
if err := value.Err(); err != nil { | ||
return fmt.Errorf("error compiling schema: %w", err) | ||
} | ||
|
||
// Find all fields with @animal attribute and their descriptions | ||
animals := make(map[string]string) | ||
iter, _ := value.Fields() | ||
for iter.Next() { | ||
sel := iter.Selector() | ||
docs := iter.Value().Doc() | ||
for _, doc := range docs { | ||
for _, c := range doc.List { | ||
if strings.Contains(c.Text, "@animal") { | ||
// Look for preset description in the comments | ||
re := regexp.MustCompile(`preset: (.+)`) | ||
for _, c2 := range doc.List { | ||
if matches := re.FindStringSubmatch(c2.Text); len(matches) > 1 { | ||
animals[sel.String()] = matches[1] | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if len(animals) == 0 { | ||
return fmt.Errorf("no animals found in schema") | ||
} | ||
|
||
// Generate commands.go | ||
var buf bytes.Buffer | ||
buf.WriteString("// Code generated by gen; DO NOT EDIT.\n\n") | ||
buf.WriteString("package cmd\n\n") | ||
buf.WriteString("var commands = map[string]struct {\n") | ||
buf.WriteString("\tshort string\n") | ||
buf.WriteString("\tlong string\n") | ||
buf.WriteString("}{\n") | ||
|
||
for animal, desc := range animals { | ||
buf.WriteString(fmt.Sprintf("\t%q: {\n", animal)) | ||
buf.WriteString(fmt.Sprintf("\t\tshort: \"Generate Renovate configuration using %s preset\",\n", animal)) | ||
buf.WriteString(fmt.Sprintf("\t\tlong: \"Generate Renovate configuration using %s preset, which %s.\",\n", animal, desc)) | ||
buf.WriteString("\t},\n") | ||
} | ||
buf.WriteString("}\n") | ||
|
||
formatted, err := format.Source(buf.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("error formatting generated code: %w", err) | ||
} | ||
|
||
// Create output directory if it doesn't exist | ||
if err := os.MkdirAll(filepath.Dir(outputFile), 0o755); err != nil { | ||
return fmt.Errorf("error creating output directory: %w", err) | ||
} | ||
|
||
if err := os.WriteFile(outputFile, formatted, 0o644); err != nil { | ||
return fmt.Errorf("error writing commands.go: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.