github.com/xgoffin/jenkins-library@v1.154.0/cmd/readPipelineEnv.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/SAP/jenkins-library/pkg/log"
     6  	"github.com/SAP/jenkins-library/pkg/piperenv"
     7  	"github.com/spf13/cobra"
     8  	"os"
     9  	"path"
    10  )
    11  
    12  // ReadPipelineEnv reads the commonPipelineEnvironment from disk and outputs it as JSON
    13  func ReadPipelineEnv() *cobra.Command {
    14  	return &cobra.Command{
    15  		Use:   "readPipelineEnv",
    16  		Short: "Reads the commonPipelineEnvironment from disk and outputs it as JSON",
    17  		PreRun: func(cmd *cobra.Command, args []string) {
    18  			path, _ := os.Getwd()
    19  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    20  			log.RegisterHook(fatalHook)
    21  		},
    22  
    23  		Run: func(cmd *cobra.Command, args []string) {
    24  			err := runReadPipelineEnv()
    25  			if err != nil {
    26  				log.Entry().Fatalf("error when writing reading Pipeline environment: %v", err)
    27  			}
    28  		},
    29  	}
    30  }
    31  
    32  func runReadPipelineEnv() error {
    33  	cpe := piperenv.CPEMap{}
    34  
    35  	err := cpe.LoadFromDisk(path.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment"))
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	encoder := json.NewEncoder(os.Stdout)
    41  	encoder.SetIndent("", "\t")
    42  	if err := encoder.Encode(cpe); err != nil {
    43  		return err
    44  	}
    45  
    46  	return nil
    47  }