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"
2021-11-18 08:24:00 +02:00
"github.com/SAP/jenkins-library/pkg/piperutils"
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 ( ) {
2021-11-18 08:24:00 +02:00
// flags for step documentation
2020-09-23 13:55:17 +02:00
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
2021-10-11 15:22:24 +02:00
var includeAzure bool
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." )
2021-10-11 15:22:24 +02:00
flag . BoolVar ( & includeAzure , "includeAzure" , false , "Include Azure-specifics in step documentation." )
2020-09-30 15:02:00 +02:00
2021-11-18 08:24:00 +02:00
// flags for stage documentation
var generateStageConfig bool
var stageMetadataPath string
var stageTargetPath string
var relativeStepsPath string
flag . BoolVar ( & generateStageConfig , "generateStageConfig" , false , "Create stage documentation instead of step documentation." )
flag . StringVar ( & stageMetadataPath , "stageMetadataPath" , "./resources/com.sap.piper/pipeline/stageDefaults.yml" , "The file containing the stage metadata. Default points to \\'./resources/com.sap.piper/pipeline/stageDefaults.yml\\'." )
flag . StringVar ( & stageTargetPath , "stageTargetPath" , "./documentation/docs/stages/" , "The target path for the generated stage documentation. Default points to \\'./documentation/docs/stages/\\'." )
flag . StringVar ( & relativeStepsPath , "relativeStepsPath" , "../../steps" , "The relative path from stages to steps" )
2020-09-23 13:55:17 +02:00
flag . Parse ( )
2021-11-18 08:24:00 +02:00
if generateStageConfig {
// generating stage documentation
fmt . Println ( "Generating STAGE documentation" )
fmt . Println ( "using Metadata:" , stageMetadataPath )
fmt . Println ( "using stage target directory:" , stageTargetPath )
fmt . Println ( "using relative steps path:" , relativeStepsPath )
2020-09-23 13:55:17 +02:00
2021-11-18 08:24:00 +02:00
utils := & piperutils . Files { }
err := generator . GenerateStageDocumentation ( stageMetadataPath , stageTargetPath , relativeStepsPath , utils )
2020-09-30 15:02:00 +02:00
checkError ( err )
2021-11-18 08:24:00 +02:00
} else {
// generating step documentation
fmt . Println ( "Generating STEP documentation" )
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 ,
} , includeAzure )
2020-09-30 15:02:00 +02:00
checkError ( err )
}
2020-09-23 13:55:17 +02:00
}
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 )
}
}