github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/swagger/template.go (about)

     1  package swagger
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/template"
     7  )
     8  
     9  type SchemaProvider interface {
    10  	Execute() error
    11  }
    12  
    13  type Template struct {
    14  	SwaggerFilesPath string
    15  	Templates        map[string]string
    16  }
    17  
    18  func NewTemplate(swaggerFilesPath string, templates map[string]string) *Template {
    19  	return &Template{
    20  		SwaggerFilesPath: swaggerFilesPath,
    21  		Templates:        templates,
    22  	}
    23  }
    24  
    25  func (t *Template) Execute() error {
    26  	templateSchemaPath := t.SwaggerFilesPath + "/schema/swagger.yaml"
    27  	// this path is also set in the files/swagger/index.html file
    28  	outputSchemaPath := t.SwaggerFilesPath + "/swagger.yaml"
    29  
    30  	schema, err := template.ParseFiles(templateSchemaPath)
    31  	if err != nil {
    32  		return fmt.Errorf("while parsing files: %w", err)
    33  	}
    34  	output, err := os.OpenFile(outputSchemaPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
    35  	if err != nil {
    36  		return fmt.Errorf("while opening file: %w", err)
    37  	}
    38  
    39  	defer output.Close()
    40  
    41  	err = schema.Execute(output, t.Templates)
    42  	if err != nil {
    43  		return fmt.Errorf("while executing template: %w", err)
    44  	}
    45  	return nil
    46  }