github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/documentation/generator/helper.go (about)

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  func readAndAdjustTemplate(docFile io.ReadCloser) string {
    11  	//read template content
    12  	content, err := io.ReadAll(docFile)
    13  	checkError(err)
    14  	contentStr := string(content)
    15  
    16  	//replace old placeholder with new ones
    17  	contentStr = strings.ReplaceAll(contentStr, "# ${docGenStepName}", "{{StepName .}}")
    18  	contentStr = strings.ReplaceAll(contentStr, "## ${docGenDescription}", "{{Description .}}")
    19  	contentStr = strings.ReplaceAll(contentStr, "## ${docGenParameters}", "{{Parameters .}}")
    20  	contentStr = strings.ReplaceAll(contentStr, "## ${docGenConfiguration}", "")
    21  	contentStr = strings.ReplaceAll(contentStr, "## ${docJenkinsPluginDependencies}", "")
    22  
    23  	return contentStr
    24  }
    25  
    26  func checkError(err error) {
    27  	if err != nil {
    28  		fmt.Printf("Error occurred: %v\n", err)
    29  		os.Exit(1)
    30  	}
    31  }
    32  
    33  func contains(v []string, s string) bool {
    34  	for _, i := range v {
    35  		if i == s {
    36  			return true
    37  		}
    38  	}
    39  	return false
    40  }
    41  
    42  func ifThenElse(condition bool, positive string, negative string) string {
    43  	if condition {
    44  		return positive
    45  	}
    46  	return negative
    47  }