2020-09-23 13:55:17 +02:00
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
2020-10-16 12:50:39 +02:00
"strings"
2020-09-23 13:55:17 +02:00
generator "github.com/SAP/jenkins-library/pkg/documentation/generator"
"github.com/SAP/jenkins-library/pkg/generator/helper"
2020-09-30 15:02:00 +02:00
"github.com/ghodss/yaml"
2020-09-23 13:55:17 +02:00
)
2020-10-16 12:50:39 +02:00
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
}
2020-09-23 13:55:17 +02:00
func main ( ) {
var metadataPath string
var docTemplatePath string
2020-09-30 15:02:00 +02:00
var customLibraryStepFile string
2020-10-16 12:50:39 +02:00
var customDefaultFiles sliceFlags
2020-09-23 13:55:17 +02:00
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/\\'." )
2020-09-30 15:02:00 +02:00
flag . StringVar ( & customLibraryStepFile , "customLibraryStepFile" , "" , "" )
2020-10-16 12:50:39 +02:00
flag . Var ( & customDefaultFiles , "customDefaultFile" , "Path to a custom default configuration file." )
2020-09-30 15:02:00 +02:00
2020-09-23 13:55:17 +02:00
flag . Parse ( )
fmt . Println ( "using Metadata Directory:" , metadataPath )
fmt . Println ( "using Documentation Directory:" , docTemplatePath )
2020-10-16 12:50:39 +02:00
fmt . Println ( "using Custom Default Files:" , strings . Join ( customDefaultFiles . list , ", " ) )
2020-09-23 13:55:17 +02:00
2020-09-30 15:02:00 +02:00
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 )
}
2020-09-23 13:55:17 +02:00
metadataFiles , err := helper . MetadataFiles ( metadataPath )
checkError ( err )
2020-10-16 12:50:39 +02:00
err = generator . GenerateStepDocumentation ( metadataFiles , customDefaultFiles . list , generator . DocuHelperData {
2020-09-23 13:55:17 +02:00
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 {
2020-09-24 07:41:06 +02:00
fmt . Printf ( "Error occurred: %v\n" , err )
2020-09-23 13:55:17 +02:00
os . Exit ( 1 )
}
}