github.com/SAP/jenkins-library@v1.362.0/cmd/getDefaults.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "os" 8 9 "github.com/pkg/errors" 10 "github.com/spf13/cobra" 11 12 "github.com/SAP/jenkins-library/pkg/config" 13 "github.com/SAP/jenkins-library/pkg/log" 14 "github.com/SAP/jenkins-library/pkg/piperutils" 15 ) 16 17 type defaultsCommandOptions struct { 18 output string //output format of default configs, currently only YAML 19 outputFile string //if set: path to file where the output should be written to 20 defaultsFiles []string 21 useV1 bool 22 openFile func(s string, t map[string]string) (io.ReadCloser, error) 23 } 24 25 var defaultsOptions defaultsCommandOptions 26 27 type getDefaultsUtils interface { 28 FileExists(filename string) (bool, error) 29 DirExists(path string) (bool, error) 30 FileWrite(path string, content []byte, perm os.FileMode) error 31 } 32 33 type getDefaultsUtilsBundle struct { 34 *piperutils.Files 35 } 36 37 func newGetDefaultsUtilsUtils() getDefaultsUtils { 38 utils := getDefaultsUtilsBundle{ 39 Files: &piperutils.Files{}, 40 } 41 return &utils 42 } 43 44 // DefaultsCommand is the entry command for loading the configuration of a pipeline step 45 func DefaultsCommand() *cobra.Command { 46 47 defaultsOptions.openFile = config.OpenPiperFile 48 var createDefaultsCmd = &cobra.Command{ 49 Use: "getDefaults", 50 Short: "Retrieves multiple default configurations and outputs them embedded into a JSON object.", 51 PreRun: func(cmd *cobra.Command, args []string) { 52 path, _ := os.Getwd() 53 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 54 log.RegisterHook(fatalHook) 55 initStageName(false) 56 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 57 }, 58 Run: func(cmd *cobra.Command, _ []string) { 59 utils := newGetDefaultsUtilsUtils() 60 _, err := generateDefaults(utils) 61 if err != nil { 62 log.SetErrorCategory(log.ErrorConfiguration) 63 log.Entry().WithError(err).Fatal("failed to retrieve default configurations") 64 } 65 }, 66 } 67 68 addDefaultsFlags(createDefaultsCmd) 69 return createDefaultsCmd 70 } 71 72 func getDefaults() ([]map[string]string, error) { 73 74 var yamlDefaults []map[string]string 75 76 for _, f := range defaultsOptions.defaultsFiles { 77 fc, err := defaultsOptions.openFile(f, GeneralConfig.GitHubAccessTokens) 78 if err != nil { 79 return yamlDefaults, errors.Wrapf(err, "defaults: retrieving defaults file failed: '%v'", f) 80 } 81 if err == nil { 82 var yamlContent string 83 84 if !defaultsOptions.useV1 { 85 var c config.Config 86 c.ReadConfig(fc) 87 88 yamlContent, err = config.GetYAML(c) 89 if err != nil { 90 return yamlDefaults, errors.Wrapf(err, "defaults: could not marshal YAML default file: '%v", f) 91 } 92 } else { 93 var rc config.RunConfigV1 94 rc.StageConfigFile = fc 95 rc.LoadConditionsV1() 96 97 yamlContent, err = config.GetYAML(rc.PipelineConfig) 98 if err != nil { 99 return yamlDefaults, errors.Wrapf(err, "defaults: could not marshal YAML default file: '%v", f) 100 } 101 } 102 103 yamlDefaults = append(yamlDefaults, map[string]string{"content": yamlContent, "filepath": f}) 104 } 105 } 106 107 return yamlDefaults, nil 108 } 109 110 func generateDefaults(utils getDefaultsUtils) ([]byte, error) { 111 112 var jsonOutput []byte 113 114 yamlDefaults, err := getDefaults() 115 if err != nil { 116 return jsonOutput, err 117 } 118 119 if len(yamlDefaults) > 1 { 120 jsonOutput, err = json.Marshal(yamlDefaults) 121 } else { 122 jsonOutput, err = json.Marshal(yamlDefaults[0]) 123 } 124 125 if err != nil { 126 return jsonOutput, errors.Wrapf(err, "defaults: could not embed YAML defaults into JSON") 127 } 128 129 if len(defaultsOptions.outputFile) > 0 { 130 err := utils.FileWrite(defaultsOptions.outputFile, []byte(jsonOutput), 0666) 131 if err != nil { 132 return jsonOutput, fmt.Errorf("failed to write output file %v: %w", defaultsOptions.outputFile, err) 133 } 134 return jsonOutput, nil 135 } 136 fmt.Println(string(jsonOutput)) 137 138 return jsonOutput, nil 139 } 140 141 func addDefaultsFlags(cmd *cobra.Command) { 142 143 cmd.Flags().StringVar(&defaultsOptions.output, "output", "yaml", "Defines the format of the configs embedded into a JSON object") 144 cmd.Flags().StringVar(&defaultsOptions.outputFile, "outputFile", "", "Defines the output filename") 145 cmd.Flags().StringArrayVar(&defaultsOptions.defaultsFiles, "defaultsFile", []string{}, "Defines the input defaults file(s)") 146 cmd.Flags().BoolVar(&defaultsOptions.useV1, "useV1", false, "Input files are CRD-style stage configuration") 147 cmd.MarkFlagRequired("defaultsFile") 148 }