github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/replication/create.go (about) 1 package replication 2 3 import ( 4 "encoding/json" 5 6 "github.com/jfrog/jfrog-cli-go/artifactory/commands/utils" 7 rtUtils "github.com/jfrog/jfrog-cli-go/artifactory/utils" 8 "github.com/jfrog/jfrog-cli-go/utils/cliutils" 9 "github.com/jfrog/jfrog-cli-go/utils/config" 10 "github.com/jfrog/jfrog-client-go/artifactory/services" 11 "github.com/jfrog/jfrog-client-go/utils/errorutils" 12 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 13 ) 14 15 type ReplicationCreateCommand struct { 16 rtDetails *config.ArtifactoryDetails 17 templatePath string 18 vars string 19 } 20 21 func NewReplicationCreateCommand() *ReplicationCreateCommand { 22 return &ReplicationCreateCommand{} 23 } 24 25 func (rcc *ReplicationCreateCommand) SetTemplatePath(path string) *ReplicationCreateCommand { 26 rcc.templatePath = path 27 return rcc 28 } 29 30 func (rcc *ReplicationCreateCommand) SetVars(vars string) *ReplicationCreateCommand { 31 rcc.vars = vars 32 return rcc 33 } 34 35 func (rcc *ReplicationCreateCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *ReplicationCreateCommand { 36 rcc.rtDetails = rtDetails 37 return rcc 38 } 39 40 func (rcc *ReplicationCreateCommand) RtDetails() (*config.ArtifactoryDetails, error) { 41 return rcc.rtDetails, nil 42 } 43 44 func (rcc *ReplicationCreateCommand) CommandName() string { 45 return "rt_replication_create" 46 } 47 48 func (rcc *ReplicationCreateCommand) Run() (err error) { 49 content, err := fileutils.ReadFile(rcc.templatePath) 50 if errorutils.CheckError(err) != nil { 51 return 52 } 53 // Replace vars string-by-string if needed 54 if len(rcc.vars) > 0 { 55 templateVars := cliutils.SpecVarsStringToMap(rcc.vars) 56 content = cliutils.ReplaceVars(content, templateVars) 57 } 58 // Unmarshal template to a map 59 var replicationConfigMap map[string]interface{} 60 err = json.Unmarshal(content, &replicationConfigMap) 61 if errorutils.CheckError(err) != nil { 62 return 63 } 64 // All the values in the template are strings 65 // Go over the the confMap and write the values with the correct type using the writersMap 66 serverId := "" 67 for key, value := range replicationConfigMap { 68 if key == "serverId" { 69 serverId = value.(string) 70 } else { 71 writersMap[key](&replicationConfigMap, key, value.(string)) 72 } 73 } 74 fillMissingDefaultValue(replicationConfigMap) 75 // Write a JSON with the correct values 76 content, err = json.Marshal(replicationConfigMap) 77 if errorutils.CheckError(err) != nil { 78 return 79 } 80 var params services.CreateReplicationParams 81 err = json.Unmarshal(content, ¶ms) 82 if errorutils.CheckError(err) != nil { 83 return 84 } 85 servicesManager, err := rtUtils.CreateServiceManager(rcc.rtDetails, false) 86 if serverId != "" { 87 updateArtifactoryInfo(¶ms, serverId) 88 } 89 return servicesManager.CreateReplication(params) 90 } 91 92 func fillMissingDefaultValue(replicationConfigMap map[string]interface{}) { 93 if _, ok := replicationConfigMap["socketTimeoutMillis"]; !ok { 94 writersMap["socketTimeoutMillis"](&replicationConfigMap, "socketTimeoutMillis", "15000") 95 } 96 if _, ok := replicationConfigMap["syncProperties"]; !ok { 97 writersMap["syncProperties"](&replicationConfigMap, "syncProperties", "true") 98 } 99 } 100 101 func updateArtifactoryInfo(param *services.CreateReplicationParams, serverId string) error { 102 singleConfig, err := config.GetArtifactorySpecificConfig(serverId) 103 if err != nil { 104 return err 105 } 106 param.Url, param.Password, param.Username = singleConfig.GetUrl(), singleConfig.GetPassword(), singleConfig.GetUser() 107 return nil 108 } 109 110 var writersMap = map[string]utils.AnswerWriter{ 111 ServerId: utils.WriteStringAnswer, 112 RepoKey: utils.WriteStringAnswer, 113 CronExp: utils.WriteStringAnswer, 114 EnableEventReplication: utils.WriteBoolAnswer, 115 Enabled: utils.WriteBoolAnswer, 116 SyncDeletes: utils.WriteBoolAnswer, 117 SyncProperties: utils.WriteBoolAnswer, 118 SyncStatistics: utils.WriteBoolAnswer, 119 PathPrefix: utils.WriteStringAnswer, 120 SocketTimeoutMillis: utils.WriteIntAnswer, 121 }