2019-10-25 14:58:59 +02:00
package main
import (
2020-11-02 11:31:00 +02:00
"bufio"
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 ( ) {
2020-07-17 08:06:11 +02:00
var metadataPath string
var targetDir string
2019-11-12 13:56:04 +02:00
2020-07-17 08:06:11 +02:00
flag . StringVar ( & metadataPath , "metadataDir" , "./resources/metadata" , "The directory containing the step metadata. Default points to \\'resources/metadata\\'." )
flag . StringVar ( & targetDir , "targetDir" , "./cmd" , "The target directory for the generated commands." )
2019-11-12 13:56:04 +02:00
flag . Parse ( )
2020-09-23 13:55:17 +02:00
fmt . Printf ( "metadataDir: %v\n, targetDir: %v\n" , metadataPath , targetDir )
2019-10-25 14:58:59 +02:00
2019-11-08 16:35:11 +02:00
metadataFiles , err := helper . MetadataFiles ( metadataPath )
2019-10-25 14:58:59 +02:00
checkError ( err )
2020-09-23 13:55:17 +02:00
err = helper . ProcessMetaFiles ( metadataFiles , targetDir , helper . StepHelperData {
OpenFile : openMetaFile ,
WriteFile : fileWriter ,
ExportPrefix : "" ,
} )
2019-10-25 14:58:59 +02:00
checkError ( err )
2020-07-17 08:06:11 +02:00
fmt . Printf ( "Running go fmt %v\n" , targetDir )
cmd := exec . Command ( "go" , "fmt" , targetDir )
2020-11-02 11:31:00 +02:00
r , _ := cmd . StdoutPipe ( )
cmd . Stderr = cmd . Stdout
done := make ( chan struct { } )
scanner := bufio . NewScanner ( r )
go func ( ) {
for scanner . Scan ( ) {
fmt . Println ( scanner . Text ( ) )
}
done <- struct { } { }
} ( )
2019-10-25 14:58:59 +02:00
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 {
2020-09-24 07:41:06 +02:00
fmt . Printf ( "Error occurred: %v\n" , err )
2019-10-25 14:58:59 +02:00
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 )
}