Add curl examples next to go examples (#204)

This commit is contained in:
Janos Dobronszki
2021-09-14 10:32:18 +01:00
committed by GitHub
parent 6cb1a023aa
commit b50d585e02
2 changed files with 34 additions and 0 deletions

View File

@@ -70,3 +70,8 @@ import(
fmt.Println(rsp, err)
}
`
const curlExampleTemplate = `{{ $service := .service }}curl "https://api.m3o.com/v1/{{ $service.Name }}/{{ title .endpoint }}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MICRO_API_TOKEN" \
-d '{{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }}'`

View File

@@ -345,6 +345,35 @@ func main() {
fmt.Println(fmt.Sprintf("Problem with '%v' example '%v': %v", serviceName, endpoint, string(outp)))
os.Exit(1)
}
// curl example
templ, err = template.New("curl" + serviceName + endpoint).Funcs(funcs).Parse(curlExampleTemplate)
if err != nil {
fmt.Println("Failed to unmarshal", err)
os.Exit(1)
}
b = bytes.Buffer{}
buf = bufio.NewWriter(&b)
err = templ.Execute(buf, map[string]interface{}{
"service": service,
"example": example,
"endpoint": endpoint,
"funcName": strcase.UpperCamelCase(title),
})
curlExampleFile := filepath.Join(goPath, serviceName, "examples", endpoint, title+".sh")
f, err = os.OpenFile(curlExampleFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
if err != nil {
fmt.Println("Failed to open schema file", err)
os.Exit(1)
}
buf.Flush()
_, err = f.Write(b.Bytes())
if err != nil {
fmt.Println("Failed to append to schema file", err)
os.Exit(1)
}
}
// only build after each example is generated as old files from
// previous generation might not compile