You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
feat(cpm): Add read and write CPE Go step (#2888)
* add read write cpe go steps * Update pkg/piperenv/CPEMap.go Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Update pkg/piperenv/CPEMap.go Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Update pkg/piperenv/environment.go Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * rename file * add error handling * add error handling Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
@@ -152,6 +152,8 @@ func Execute() {
|
||||
rootCmd.AddCommand(PipelineCreateScanSummaryCommand())
|
||||
rootCmd.AddCommand(TransportRequestDocIDFromGitCommand())
|
||||
rootCmd.AddCommand(TransportRequestReqIDFromGitCommand())
|
||||
rootCmd.AddCommand(WritePipelineEnv())
|
||||
rootCmd.AddCommand(ReadPipelineEnv())
|
||||
|
||||
addRootFlags(rootCmd)
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
|
||||
49
cmd/readPipelineEnv.go
Normal file
49
cmd/readPipelineEnv.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/piperenv"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
// ReadPipelineEnv reads the commonPipelineEnvironment from disk and outputs it as JSON
|
||||
func ReadPipelineEnv() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "readPipelineEnv",
|
||||
Short: "Reads the commonPipelineEnvironment from disk and outputs it as JSON",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
path, _ := os.Getwd()
|
||||
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||||
log.RegisterHook(fatalHook)
|
||||
},
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := runReadPipelineEnv()
|
||||
if err != nil {
|
||||
log.Entry().Fatalf("error when writing reading Pipeline environment: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runReadPipelineEnv() error {
|
||||
cpe := piperenv.CPEMap{}
|
||||
|
||||
err := cpe.LoadFromDisk(path.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bytes, err := json.MarshalIndent(cpe, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = os.Stdout.Write(bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
60
cmd/writePipelineEnv.go
Normal file
60
cmd/writePipelineEnv.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/piperenv"
|
||||
"github.com/spf13/cobra"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// WritePipelineEnv Serializes the commonPipelineEnvironment JSON to disk
|
||||
func WritePipelineEnv() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "writePipelineEnv",
|
||||
Short: "Serializes the commonPipelineEnvironment JSON to disk",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
path, _ := os.Getwd()
|
||||
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||||
log.RegisterHook(fatalHook)
|
||||
},
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := runWritePipelineEnv()
|
||||
if err != nil {
|
||||
log.Entry().Fatalf("error when writing common Pipeline environment: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runWritePipelineEnv() error {
|
||||
inBytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commonPipelineEnv := piperenv.CPEMap{}
|
||||
err = json.Unmarshal(inBytes, &commonPipelineEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rootPath := filepath.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
||||
err = commonPipelineEnv.WriteToDisk(rootPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bytes, err := json.MarshalIndent(commonPipelineEnv, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = os.Stdout.Write(bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user