2019-10-25 14:58:59 +02:00
package main
import (
2019-11-12 13:56:04 +02:00
"flag"
2019-10-25 14:58:59 +02:00
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
2019-11-08 16:35:11 +02:00
"github.com/SAP/jenkins-library/pkg/generator/helper"
2019-10-25 14:58:59 +02:00
)
func main ( ) {
2019-11-12 13:56:04 +02:00
var docTemplatePath string
var isGenerateDocu bool
flag . StringVar ( & docTemplatePath , "docuDir" , "./documentation/docs/steps/" , "The directory containing the docu stubs. Default points to \\'documentation/docs/steps.\\'" )
flag . BoolVar ( & isGenerateDocu , "docuGen" , false , "Boolean to generate Documentation or Step-MetaData. Default is false" )
flag . Parse ( )
fmt . Printf ( "docuDir: %v, genDocu: %v \n" , docTemplatePath , isGenerateDocu )
2019-10-25 14:58:59 +02:00
metadataPath := "./resources/metadata"
2019-11-08 16:35:11 +02:00
metadataFiles , err := helper . MetadataFiles ( metadataPath )
2019-10-25 14:58:59 +02:00
checkError ( err )
2019-11-13 15:34:04 +02:00
docuHelperData := helper . DocuHelperData { isGenerateDocu , docTemplatePath , openDocTemplate , docFileWriter }
2019-11-21 12:18:56 +02:00
stepHelperData := helper . StepHelperData { openMetaFile , fileWriter , "" }
2019-11-21 12:12:30 +02:00
err = helper . ProcessMetaFiles ( metadataFiles , stepHelperData , docuHelperData )
2019-10-25 14:58:59 +02:00
checkError ( err )
cmd := exec . Command ( "go" , "fmt" , "./cmd" )
err = cmd . Run ( )
checkError ( err )
}
func openMetaFile ( name string ) ( io . ReadCloser , error ) {
return os . Open ( name )
}
func fileWriter ( filename string , data [ ] byte , perm os . FileMode ) error {
return ioutil . WriteFile ( filename , data , perm )
}
func checkError ( err error ) {
if err != nil {
fmt . Printf ( "Error occured: %v\n" , err )
os . Exit ( 1 )
}
}
2019-11-13 15:34:04 +02:00
func openDocTemplate ( docTemplateFilePath string ) ( io . ReadCloser , error ) {
//check if template exists otherwise print No Template found
if _ , err := os . Stat ( docTemplateFilePath ) ; os . IsNotExist ( err ) {
2019-11-13 15:43:53 +02:00
err := fmt . Errorf ( "no template found: %v" , docTemplateFilePath )
2019-11-13 15:34:04 +02:00
return nil , err
}
return os . Open ( docTemplateFilePath )
}
func docFileWriter ( filename string , data [ ] byte , perm os . FileMode ) error {
return ioutil . WriteFile ( filename , data , perm )
}