Skip to content

Commit ca33a1a

Browse files
committed
refactor: extract fetchNameList to reduce duplication in API fetchers
1 parent 0f01af2 commit ca33a1a

1 file changed

Lines changed: 20 additions & 40 deletions

File tree

cmd/kosli/createPolicyFile.go

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -112,42 +112,19 @@ func validateOutputFile(path string) error {
112112
}
113113

114114
func fetchFlowNames() []string {
115-
u, err := url.JoinPath(global.Host, "api/v2/flows", global.Org)
116-
if err != nil {
117-
logger.Debug("failed to build flows URL: %v", err)
118-
return nil
119-
}
120-
121-
reqParams := &requests.RequestParams{
122-
Method: http.MethodGet,
123-
URL: u,
124-
Token: global.ApiToken,
125-
}
126-
response, err := kosliClient.Do(reqParams)
127-
if err != nil {
128-
logger.Debug("failed to fetch flows: %v", err)
129-
return nil
130-
}
131-
132-
var flows []map[string]any
133-
if err := json.Unmarshal([]byte(response.Body), &flows); err != nil {
134-
logger.Debug("failed to parse flows response: %v", err)
135-
return nil
136-
}
137-
138-
names := make([]string, 0, len(flows))
139-
for _, flow := range flows {
140-
if name, ok := flow["name"].(string); ok {
141-
names = append(names, name)
142-
}
143-
}
144-
return names
115+
return fetchNameList("api/v2/flows", nil)
145116
}
146117

147118
func fetchCustomAttestationTypes() []string {
148-
u, err := url.JoinPath(global.Host, "api/v2/custom-attestation-types", global.Org)
119+
return fetchNameList("api/v2/custom-attestation-types", func(name string) string {
120+
return "custom:" + name
121+
})
122+
}
123+
124+
func fetchNameList(apiPath string, transform func(string) string) []string {
125+
u, err := url.JoinPath(global.Host, apiPath, global.Org)
149126
if err != nil {
150-
logger.Debug("failed to build attestation types URL: %v", err)
127+
logger.Debug("failed to build URL for %s: %v", apiPath, err)
151128
return nil
152129
}
153130

@@ -158,20 +135,23 @@ func fetchCustomAttestationTypes() []string {
158135
}
159136
response, err := kosliClient.Do(reqParams)
160137
if err != nil {
161-
logger.Debug("failed to fetch attestation types: %v", err)
138+
logger.Debug("failed to fetch %s: %v", apiPath, err)
162139
return nil
163140
}
164141

165-
var types []map[string]any
166-
if err := json.Unmarshal([]byte(response.Body), &types); err != nil {
167-
logger.Debug("failed to parse attestation types response: %v", err)
142+
var items []map[string]any
143+
if err := json.Unmarshal([]byte(response.Body), &items); err != nil {
144+
logger.Debug("failed to parse %s response: %v", apiPath, err)
168145
return nil
169146
}
170147

171-
names := make([]string, 0, len(types))
172-
for _, t := range types {
173-
if name, ok := t["name"].(string); ok {
174-
names = append(names, "custom:"+name)
148+
names := make([]string, 0, len(items))
149+
for _, item := range items {
150+
if name, ok := item["name"].(string); ok {
151+
if transform != nil {
152+
name = transform(name)
153+
}
154+
names = append(names, name)
175155
}
176156
}
177157
return names

0 commit comments

Comments
 (0)