mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
Add curl examples next to go examples (#204)
This commit is contained in:
@@ -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 }}'`
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user