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