From b50d585e02c2b72fac59002f5d689111a1cc3de6 Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Tue, 14 Sep 2021 10:32:18 +0100 Subject: [PATCH] Add curl examples next to go examples (#204) --- cmd/clients/go_template.go | 5 +++++ cmd/clients/main.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/cmd/clients/go_template.go b/cmd/clients/go_template.go index c6cdb41..a6f55cf 100644 --- a/cmd/clients/go_template.go +++ b/cmd/clients/go_template.go @@ -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 }}'` diff --git a/cmd/clients/main.go b/cmd/clients/main.go index 07ab619..8f36c15 100644 --- a/cmd/clients/main.go +++ b/cmd/clients/main.go @@ -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