1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/documentation/generator.go
Christopher Fenner 0797f4049a
feat(docs): use custom defaults during docs generation (#2022)
* remove docs generator code from step-generator

* add docs generator to dedicated package

* add test cases

* add entry point for docs generation

* make output more readable

* read additional defaults

* add custo defaults parameters

* remove commented code

* adjust custom default parameter in workflow

* remove conflict leftovers

* handle custom default values

* remove comment

* extract code to function

* extract metadata reading to function

* do not print empty strings in favor of PIPER_* env vars

* extract new code to own metadata file

* only reset default on booleans

* remove obsolete test case
2020-10-16 12:50:39 +02:00

90 lines
2.5 KiB
Go

package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
generator "github.com/SAP/jenkins-library/pkg/documentation/generator"
"github.com/SAP/jenkins-library/pkg/generator/helper"
"github.com/ghodss/yaml"
)
type sliceFlags struct {
list []string
}
func (f *sliceFlags) String() string {
return ""
}
func (f *sliceFlags) Set(value string) error {
f.list = append(f.list, value)
return nil
}
func main() {
var metadataPath string
var docTemplatePath string
var customLibraryStepFile string
var customDefaultFiles sliceFlags
flag.StringVar(&metadataPath, "metadataDir", "./resources/metadata", "The directory containing the step metadata. Default points to \\'resources/metadata\\'.")
flag.StringVar(&docTemplatePath, "docuDir", "./documentation/docs/steps/", "The directory containing the docu stubs. Default points to \\'documentation/docs/steps/\\'.")
flag.StringVar(&customLibraryStepFile, "customLibraryStepFile", "", "")
flag.Var(&customDefaultFiles, "customDefaultFile", "Path to a custom default configuration file.")
flag.Parse()
fmt.Println("using Metadata Directory:", metadataPath)
fmt.Println("using Documentation Directory:", docTemplatePath)
fmt.Println("using Custom Default Files:", strings.Join(customDefaultFiles.list, ", "))
if len(customLibraryStepFile) > 0 {
fmt.Println("Reading custom library step mapping..")
content, err := ioutil.ReadFile(customLibraryStepFile)
checkError(err)
err = yaml.Unmarshal(content, &generator.CustomLibrarySteps)
checkError(err)
fmt.Println(generator.CustomLibrarySteps)
}
metadataFiles, err := helper.MetadataFiles(metadataPath)
checkError(err)
err = generator.GenerateStepDocumentation(metadataFiles, customDefaultFiles.list, generator.DocuHelperData{
DocTemplatePath: docTemplatePath,
OpenDocTemplateFile: openDocTemplateFile,
DocFileWriter: writeFile,
OpenFile: openFile,
})
checkError(err)
}
func openDocTemplateFile(docTemplateFilePath string) (io.ReadCloser, error) {
//check if template exists otherwise print No Template found
if _, err := os.Stat(docTemplateFilePath); os.IsNotExist(err) {
err := fmt.Errorf("no template found: %v", docTemplateFilePath)
return nil, err
}
return os.Open(docTemplateFilePath)
}
func writeFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
func openFile(name string) (io.ReadCloser, error) {
return os.Open(name)
}
func checkError(err error) {
if err != nil {
fmt.Printf("Error occurred: %v\n", err)
os.Exit(1)
}
}