mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-19 13:34:19 +00:00
Update script to also modify the json file
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
parent
948a522ad8
commit
6ac432b509
@ -15,7 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
docsPath = "docs.go"
|
goDocsPath = "docs.go"
|
||||||
|
jsonDocsPath = "swagger.json"
|
||||||
openMarker = "const docTemplate = `"
|
openMarker = "const docTemplate = `"
|
||||||
closeMarker = "`\n\n// SwaggerInfo"
|
closeMarker = "`\n\n// SwaggerInfo"
|
||||||
definitionsKey = `"definitions": {`
|
definitionsKey = `"definitions": {`
|
||||||
@ -38,16 +39,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run(receiveDir string) error {
|
func run(receiveDir string) error {
|
||||||
content, err := os.ReadFile(docsPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("read %s: %w", docsPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
template, err := extractDocTemplate(string(content))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
definitions := make(map[string]interface{})
|
definitions := make(map[string]interface{})
|
||||||
|
|
||||||
titleByFile, err := addReceiveSchemas(definitions, receiveDir)
|
titleByFile, err := addReceiveSchemas(definitions, receiveDir)
|
||||||
@ -61,41 +52,90 @@ func run(receiveDir string) error {
|
|||||||
|
|
||||||
addEnvelopeWrapperDefinition(definitions)
|
addEnvelopeWrapperDefinition(definitions)
|
||||||
|
|
||||||
newEntries, err := renderManagedDefinitions(definitions)
|
managedDefinitions, err := renderManagedDefinitions(definitions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedTemplate, err := appendDefinitionsEntries(template, newEntries)
|
if err := updateDocsGo(managedDefinitions); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := updateSwaggerJSON(managedDefinitions); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("updated %s\n", goDocsPath)
|
||||||
|
fmt.Printf("updated %s\n", jsonDocsPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateDocsGo(managedDefinitions string) error {
|
||||||
|
content, err := os.ReadFile(goDocsPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read %s: %w", goDocsPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
template, err := extractDocTemplate(string(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedTemplate, err = replaceReceiveResponseSchema(updatedTemplate)
|
updatedTemplate, err := applyReceiveSchemaUpdates(template, managedDefinitions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
updated := strings.Replace(string(content), template, updatedTemplate, 1)
|
updated := strings.Replace(string(content), template, updatedTemplate, 1)
|
||||||
|
if err := os.WriteFile(goDocsPath, []byte(updated), 0644); err != nil {
|
||||||
if err := os.WriteFile(docsPath, []byte(updated), 0644); err != nil {
|
return fmt.Errorf("write %s: %w", goDocsPath, err)
|
||||||
return fmt.Errorf("write %s: %w", docsPath, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("updated %s\n", docsPath)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateSwaggerJSON(managedDefinitions string) error {
|
||||||
|
content, err := os.ReadFile(jsonDocsPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read %s: %w", jsonDocsPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
updated, err := applyReceiveSchemaUpdates(string(content), managedDefinitions)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(jsonDocsPath, []byte(updated), 0644); err != nil {
|
||||||
|
return fmt.Errorf("write %s: %w", jsonDocsPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyReceiveSchemaUpdates(content string, managedDefinitions string) (string, error) {
|
||||||
|
updated, err := appendDefinitionsEntries(content, managedDefinitions)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
updated, err = replaceReceiveResponseSchema(updated)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return updated, nil
|
||||||
|
}
|
||||||
|
|
||||||
func extractDocTemplate(content string) (string, error) {
|
func extractDocTemplate(content string) (string, error) {
|
||||||
start := strings.Index(content, openMarker)
|
start := strings.Index(content, openMarker)
|
||||||
if start == -1 {
|
if start == -1 {
|
||||||
return "", fmt.Errorf("could not find docTemplate start in %s", docsPath)
|
return "", fmt.Errorf("could not find docTemplate start in %s", goDocsPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
start += len(openMarker)
|
start += len(openMarker)
|
||||||
endOffset := strings.Index(content[start:], closeMarker)
|
endOffset := strings.Index(content[start:], closeMarker)
|
||||||
if endOffset == -1 {
|
if endOffset == -1 {
|
||||||
return "", fmt.Errorf("could not find docTemplate end in %s", docsPath)
|
return "", fmt.Errorf("could not find docTemplate end in %s", goDocsPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
return content[start : start+endOffset], nil
|
return content[start : start+endOffset], nil
|
||||||
@ -320,7 +360,7 @@ func replaceReceiveResponseSchema(template string) (string, error) {
|
|||||||
|
|
||||||
updatedPathBlock := strings.Replace(pathBlock, oldSchema, newSchema, 1)
|
updatedPathBlock := strings.Replace(pathBlock, oldSchema, newSchema, 1)
|
||||||
if updatedPathBlock == pathBlock {
|
if updatedPathBlock == pathBlock {
|
||||||
return "", fmt.Errorf("could not replace /v1/receive schema; ensure docs.go is freshly generated by swag")
|
return "", fmt.Errorf("could not replace /v1/receive schema; ensure generated docs are freshly generated by swag")
|
||||||
}
|
}
|
||||||
|
|
||||||
return template[:pathOpenBrace] + updatedPathBlock + template[pathCloseBrace+1:], nil
|
return template[:pathOpenBrace] + updatedPathBlock + template[pathCloseBrace+1:], nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user