forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadPipelineEnv.go
47 lines (40 loc) · 1.13 KB
/
readPipelineEnv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", "\t")
if err := encoder.Encode(cpe); err != nil {
return err
}
return nil
}