mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
0ba4c2206c
* update all deprecated ioutil usages * forgotten changes * add missing imports * undo changing comment * add missing 'os' import * fix integration test --------- Co-authored-by: I557621 <jordi.van.liempt@sap.com> Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// 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 {
|
|
pipelineEnv, ok := os.LookupEnv("PIPER_pipelineEnv")
|
|
inBytes := []byte(pipelineEnv)
|
|
if !ok {
|
|
var err error
|
|
inBytes, err = io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(inBytes) == 0 {
|
|
return nil
|
|
}
|
|
|
|
commonPipelineEnv := piperenv.CPEMap{}
|
|
decoder := json.NewDecoder(bytes.NewReader(inBytes))
|
|
decoder.UseNumber()
|
|
err := decoder.Decode(&commonPipelineEnv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rootPath := filepath.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
|
err = commonPipelineEnv.WriteToDisk(rootPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
writtenBytes, err := json.MarshalIndent(commonPipelineEnv, "", "\t")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = os.Stdout.Write(writtenBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|