Skip to content

Commit dc14f9e

Browse files
RTECO-235 - Refactoring code (#1149)
RTECO-235 - Refactoring code
1 parent d59cca9 commit dc14f9e

File tree

1 file changed

+35
-59
lines changed

1 file changed

+35
-59
lines changed

artifactory/services/props.go

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -106,58 +106,49 @@ func (ps *PropsService) getEncodedParam(propsParams PropsParams, isDelete bool)
106106
return encodedParam, nil
107107
}
108108

109-
func (ps *PropsService) addOrDeletePropertiesForRepo(propsParams PropsParams, isDelete bool, encodedParam string) (int, error) {
110-
var action func(string, string, string) (*http.Response, []byte, error)
109+
func (ps *PropsService) actionTypeBasedOnIsDeleteFlag(isDelete bool, action *func(string, string, string) (*http.Response, []byte, error)) {
111110
if isDelete {
112-
action = ps.sendDeleteRequest
111+
*action = ps.sendDeleteRequest
113112
} else {
114-
action = ps.sendPutRequest
113+
*action = ps.sendPutRequest
115114
}
116-
successCounters := make([]int, ps.GetThreads())
117-
producerConsumer := parallel.NewBounedRunner(ps.GetThreads(), false)
118-
errorsQueue := clientutils.NewErrorsQueue(1)
115+
}
116+
117+
func (ps *PropsService) addOrDeletePropertiesForRepo(propsParams PropsParams, isDelete bool, encodedParam string) (int, error) {
118+
// Determine which action to perform (PUT or DELETE).
119+
var action func(string, string, string) (*http.Response, []byte, error)
120+
ps.actionTypeBasedOnIsDeleteFlag(isDelete, &action)
121+
119122
reader := propsParams.GetReader()
123+
defer reader.Reset()
120124
resultItem := new(utils.ResultItem)
121125
if reader.NextRecord(resultItem) == nil {
122126
repoName := resultItem.Repo
123-
setPropsTask := func(threadId int) error {
124-
var err error
125-
logMsgPrefix := clientutils.GetLogMsgPrefix(threadId, ps.IsDryRun())
126-
127-
restAPI := path.Join("api", "storage", repoName)
128-
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), restAPI, make(map[string]string))
129-
if err != nil {
130-
return err
131-
}
132-
// Because we do set/delete props on search results that took into account the
133-
// recursive flag, we do not want the action itself to be recursive.
134-
setPropertiesURL += "?properties=" + encodedParam + "&recursive=0"
135-
resp, body, err := action(logMsgPrefix, repoName, setPropertiesURL)
127+
logMsgPrefix := clientutils.GetLogMsgPrefix(0, ps.IsDryRun())
136128

137-
if err != nil {
138-
return err
139-
}
140-
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent); err != nil {
141-
return err
142-
}
143-
successCounters[threadId]++
144-
return nil
129+
storageAPI := path.Join("api", "storage", repoName)
130+
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), storageAPI, make(map[string]string))
131+
if err != nil {
132+
return 0, err
133+
}
134+
setPropertiesURL += "?properties=" + encodedParam + "&recursive=0"
135+
136+
resp, body, err := action(logMsgPrefix, repoName, setPropertiesURL)
137+
if err != nil {
138+
return 0, err
139+
}
140+
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusNoContent); err != nil {
141+
return 0, err
145142
}
146143

147-
_, _ = producerConsumer.AddTaskWithError(setPropsTask, errorsQueue.AddError)
144+
return 1, nil
148145
}
149146

150-
defer producerConsumer.Done()
151147
if err := reader.GetError(); err != nil {
152-
errorsQueue.AddError(err)
153-
}
154-
reader.Reset()
155-
producerConsumer.Run()
156-
totalSuccess := 0
157-
for _, v := range successCounters {
158-
totalSuccess += v
148+
return 0, err
159149
}
160-
return totalSuccess, errorsQueue.GetError()
150+
151+
return 0, nil
161152
}
162153

163154
func (ps *PropsService) performRequestForRepoOnly(propsParams PropsParams, isDelete bool) (int, error) {
@@ -170,27 +161,12 @@ func (ps *PropsService) performRequestForRepoOnly(propsParams PropsParams, isDel
170161
}
171162

172163
func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (int, error) {
173-
var encodedParam string
174-
if !isDelete {
175-
props, err := utils.ParseProperties(propsParams.GetProps())
176-
if err != nil {
177-
return 0, err
178-
}
179-
encodedParam = props.ToEncodedString(true)
180-
} else {
181-
propList := strings.Split(propsParams.GetProps(), ",")
182-
for _, prop := range propList {
183-
encodedParam += url.QueryEscape(prop) + ","
184-
}
185-
// Remove trailing comma
186-
encodedParam = strings.TrimSuffix(encodedParam, ",")
164+
encodedParam, err := ps.getEncodedParam(propsParams, isDelete)
165+
if err != nil {
166+
return 0, err
187167
}
188168
var action func(string, string, string) (*http.Response, []byte, error)
189-
if isDelete {
190-
action = ps.sendDeleteRequest
191-
} else {
192-
action = ps.sendPutRequest
193-
}
169+
ps.actionTypeBasedOnIsDeleteFlag(isDelete, &action)
194170
successCounters := make([]int, ps.GetThreads())
195171
producerConsumer := parallel.NewBounedRunner(ps.GetThreads(), false)
196172
errorsQueue := clientutils.NewErrorsQueue(1)
@@ -202,8 +178,8 @@ func (ps *PropsService) performRequest(propsParams PropsParams, isDelete bool) (
202178
var err error
203179
logMsgPrefix := clientutils.GetLogMsgPrefix(threadId, ps.IsDryRun())
204180

205-
restAPI := path.Join("api", "storage", relativePath)
206-
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), restAPI, make(map[string]string))
181+
storageAPI := path.Join("api", "storage", relativePath)
182+
setPropertiesURL, err := clientutils.BuildUrl(ps.GetArtifactoryDetails().GetUrl(), storageAPI, make(map[string]string))
207183
if err != nil {
208184
return err
209185
}

0 commit comments

Comments
 (0)