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

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/config"
     9  	"github.com/ghodss/yaml"
    10  )
    11  
    12  // StepHelperData is used to transport the needed parameters and functions from the step generator to the step generation.
    13  type StepHelperData struct {
    14  	OpenFile     func(s string) (io.ReadCloser, error)
    15  	WriteFile    func(filename string, data []byte, perm os.FileMode) error
    16  	ExportPrefix string
    17  }
    18  
    19  // ContextDefaultData holds the meta data and the default data for the context default parameter descriptions
    20  type ContextDefaultData struct {
    21  	Metadata   ContextDefaultMetadata     `json:"metadata"`
    22  	Parameters []ContextDefaultParameters `json:"params"`
    23  }
    24  
    25  // ContextDefaultMetadata holds meta data for the context default parameter descripten (name, description, long description)
    26  type ContextDefaultMetadata struct {
    27  	Name            string `json:"name"`
    28  	Description     string `json:"description"`
    29  	LongDescription string `json:"longDescription,omitempty"`
    30  }
    31  
    32  // ContextDefaultParameters holds the description for the context default parameters
    33  type ContextDefaultParameters struct {
    34  	Name        string   `json:"name"`
    35  	Description string   `json:"description"`
    36  	Scope       []string `json:"scope"`
    37  }
    38  
    39  // ReadPipelineContextDefaultData loads step definition in yaml format
    40  func (c *ContextDefaultData) readPipelineContextDefaultData(metadata io.ReadCloser) {
    41  	defer metadata.Close()
    42  	content, err := io.ReadAll(metadata)
    43  	checkError(err)
    44  	err = yaml.Unmarshal(content, &c)
    45  	checkError(err)
    46  }
    47  
    48  // ReadContextDefaultMap maps the default descriptions into a map
    49  func (c *ContextDefaultData) readContextDefaultMap() map[string]interface{} {
    50  	var m map[string]interface{} = make(map[string]interface{})
    51  
    52  	for _, param := range c.Parameters {
    53  		m[param.Name] = param
    54  	}
    55  
    56  	return m
    57  }
    58  
    59  func readContextInformation(contextDetailsPath string, contextDetails *config.StepData) {
    60  	contextDetailsFile, err := os.Open(contextDetailsPath)
    61  	checkError(err)
    62  	defer contextDetailsFile.Close()
    63  
    64  	err = contextDetails.ReadPipelineStepData(contextDetailsFile)
    65  	checkError(err)
    66  }
    67  
    68  func checkError(err error) {
    69  	if err != nil {
    70  		fmt.Printf("Error occurred: %v\n", err)
    71  		os.Exit(1)
    72  	}
    73  }
    74  
    75  func contains(v []string, s string) bool {
    76  	for _, i := range v {
    77  		if i == s {
    78  			return true
    79  		}
    80  	}
    81  	return false
    82  }
    83  
    84  func ifThenElse(condition bool, positive string, negative string) string {
    85  	if condition {
    86  		return positive
    87  	}
    88  	return negative
    89  }