Skip to content

Commit a3f3d78

Browse files
committed
prettified comments
1 parent c7395bf commit a3f3d78

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

cmd/export.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ func getCollections() []keyfactor_command_client_api.KeyfactorApiModelsCertifica
157157
kfClient := initGenClient()
158158
collections, _, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionGetCollections(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
159159
if reqErr != nil {
160-
fmt.Printf("Error, unable to get collections %s\n", reqErr)
161-
log.Fatalf("Error: %s", reqErr)
160+
fmt.Printf("%s Error! Unable to get collections %s%s\n", colorRed, reqErr, colorWhite)
162161
}
163162
var lCollectionReq []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest
164163
for _, collection := range collections {
@@ -180,8 +179,7 @@ func getMetadata() []keyfactor_command_client_api.KeyfactorApiModelsMetadataFiel
180179
kfClient := initGenClient()
181180
metadata, _, reqErr := kfClient.MetadataFieldApi.MetadataFieldGetAllMetadataFields(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
182181
if reqErr != nil {
183-
fmt.Printf("Error, unable to get metadata %s\n", reqErr)
184-
log.Fatalf("Error: %s", reqErr)
182+
fmt.Printf("%s Error! Unable to get metadata %s%s\n", colorRed, reqErr, colorWhite)
185183
}
186184
var lMetadataReq []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest
187185
for _, metadataItem := range metadata {
@@ -202,8 +200,7 @@ func getExpirationAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAler
202200
kfClient := initGenClient()
203201
alerts, _, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertGetExpirationAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
204202
if reqErr != nil {
205-
fmt.Printf("Error, unable to get expiration alerts %s\n", reqErr)
206-
log.Fatalf("Error: %s", reqErr)
203+
fmt.Printf("%s Error! Unable to get expiration alerts %s%s\n", colorRed, reqErr, colorWhite)
207204
}
208205
var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest
209206
for _, alert := range alerts {
@@ -223,8 +220,7 @@ func getIssuedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsIs
223220
kfClient := initGenClient()
224221
alerts, _, reqErr := kfClient.IssuedAlertApi.IssuedAlertGetIssuedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
225222
if reqErr != nil {
226-
fmt.Printf("Error, unable to get issued cert alerts %s\n", reqErr)
227-
log.Fatalf("Error: %s", reqErr)
223+
fmt.Printf("%s Error! Unable to get issued cert alerts %s%s\n", colorRed, reqErr, colorWhite)
228224
}
229225
var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest
230226
for _, alert := range alerts {
@@ -245,8 +241,7 @@ func getDeniedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsDe
245241
kfClient := initGenClient()
246242
alerts, _, reqErr := kfClient.DeniedAlertApi.DeniedAlertGetDeniedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
247243
if reqErr != nil {
248-
fmt.Printf("Error, unable to get denied cert alerts %s\n", reqErr)
249-
log.Fatalf("Error: %s", reqErr)
244+
fmt.Printf("%s Error! Unable to get denied cert alerts %s%s\n", colorRed, reqErr, colorWhite)
250245
}
251246
var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest
252247
for _, alert := range alerts {
@@ -267,8 +262,7 @@ func getPendingAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsP
267262
kfClient := initGenClient()
268263
alerts, _, reqErr := kfClient.PendingAlertApi.PendingAlertGetPendingAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
269264
if reqErr != nil {
270-
fmt.Printf("Error, unable to get pending cert alerts %s\n", reqErr)
271-
log.Fatalf("Error: %s", reqErr)
265+
fmt.Printf("%s Error! Unable to get pending cert alerts %s%s\n", colorRed, reqErr, colorWhite)
272266
}
273267
var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest
274268
for _, alert := range alerts {
@@ -289,8 +283,7 @@ func getSslNetworks() []keyfactor_command_client_api.KeyfactorApiModelsSslCreate
289283
kfClient := initGenClient()
290284
networks, _, reqErr := kfClient.SslApi.SslGetNetworks(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
291285
if reqErr != nil {
292-
fmt.Printf("Error, unable to get SSL networks %s\n", reqErr)
293-
log.Fatalf("Error: %s", reqErr)
286+
fmt.Printf("%s Error! Unable to get SSL networks %s%s\n", colorRed, reqErr, colorWhite)
294287
}
295288
var lNetworkReq []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest
296289
for _, network := range networks {
@@ -310,8 +303,7 @@ func getWorkflowDefinitions() []exportKeyfactorApiModelsWorkflowsDefinitionCreat
310303
kfClient := initGenClient()
311304
workflowDefs, _, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionQuery(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
312305
if reqErr != nil {
313-
fmt.Printf("Error, unable to get workflow definitions %s\n", reqErr)
314-
log.Fatalf("Error: %s", reqErr)
306+
fmt.Printf("%s Error! Unable to get workflow definitions %s%s\n", colorRed, reqErr, colorWhite)
315307
}
316308
var lWorkflowReq []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest
317309
for _, workflowDef := range workflowDefs {
@@ -339,8 +331,7 @@ func getReports() ([]exportModelsReport, []keyfactor_command_client_api.ModelsCu
339331
//Gets all built-in reports
340332
bReports, _, bErr := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
341333
if bErr != nil {
342-
fmt.Printf("Error, unable to get built-in reports %s\n", bErr)
343-
log.Fatalf("Error: %s", bErr)
334+
fmt.Printf("%s Error! Unable to get built-in reports %s%s\n", colorRed, bErr, colorWhite)
344335
}
345336
var lbReportsReq []exportModelsReport
346337
for _, bReport := range bReports {
@@ -357,8 +348,7 @@ func getReports() ([]exportModelsReport, []keyfactor_command_client_api.ModelsCu
357348
//Gets all custom reports
358349
cReports, _, cErr := kfClient.ReportsApi.ReportsQueryCustomReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
359350
if cErr != nil {
360-
fmt.Printf("Error, unable to get custom reports %s\n", cErr)
361-
log.Fatalf("Error: %s", cErr)
351+
fmt.Printf("%s Error! Unable to get custom reports %s%s\n", colorRed, cErr, colorWhite)
362352
}
363353
var lcReportReq []keyfactor_command_client_api.ModelsCustomReportCreationRequest
364354
for _, cReport := range cReports {
@@ -378,8 +368,7 @@ func getRoles() []api.CreateSecurityRoleArg {
378368
kfClient, _ := initClient()
379369
roles, reqErr := kfClient.GetSecurityRoles()
380370
if reqErr != nil {
381-
fmt.Printf("Error, unable to get roles %s\n", reqErr)
382-
log.Fatalf("Error: %s", reqErr)
371+
fmt.Printf("%s Error! Unable to get security roles %s%s\n", colorRed, reqErr, colorWhite)
383372
}
384373
var lRoleReq []api.CreateSecurityRoleArg
385374
for _, role := range roles {

cmd/import.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
"os"
1313
)
1414

15+
type Body struct {
16+
ErrorCode string
17+
Message string
18+
}
19+
20+
func parseError(error io.ReadCloser) string {
21+
bytes, _ := io.ReadAll(error)
22+
var newError Body
23+
json.Unmarshal(bytes, &newError)
24+
return newError.Message
25+
}
26+
1527
var importCmd = &cobra.Command{
1628
Use: "import",
1729
Short: "Keyfactor instance import utilities.",
@@ -85,7 +97,7 @@ func importCollections(collections []keyfactor_command_client_api.KeyfactorApiMo
8597
Request(collection).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
8698
name, _ := json.Marshal(collection.Name)
8799
if reqErr != nil {
88-
fmt.Printf("Error! Unable to create collection %s: %s\n", string(name), httpResp.Body)
100+
fmt.Printf("%s Error! Unable to create collection %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
89101
} else {
90102
name, _ := json.Marshal(collection.Name)
91103
fmt.Println("Added", string(name), "to collections")
@@ -100,7 +112,7 @@ func importMetadataFields(metadataFields []keyfactor_command_client_api.Keyfacto
100112
XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
101113
name, _ := json.Marshal(metadata.Name)
102114
if reqErr != nil {
103-
fmt.Printf("Error! Unable to create metadata field type %s: %s\n", string(name), httpResp.Body)
115+
fmt.Printf("%s Error! Unable to create metadata field type %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
104116
} else {
105117
fmt.Println("Added", string(name), "to metadata field types.")
106118
}
@@ -112,7 +124,7 @@ func importIssuedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiMo
112124
_, httpResp, reqErr := kfClient.IssuedAlertApi.IssuedAlertAddIssuedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
113125
name, _ := json.Marshal(alert.DisplayName)
114126
if reqErr != nil {
115-
fmt.Printf("Error! Unable to create issued cert alert %s: %s\n", string(name), httpResp.Body)
127+
fmt.Printf("%s Error! Unable to create issued cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
116128
} else {
117129
fmt.Println("Added", string(name), "to issued cert alerts.")
118130
}
@@ -124,7 +136,7 @@ func importDeniedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiMo
124136
_, httpResp, reqErr := kfClient.DeniedAlertApi.DeniedAlertAddDeniedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
125137
name, _ := json.Marshal(alert.DisplayName)
126138
if reqErr != nil {
127-
fmt.Printf("Error, unable to create denied cert alert %s: %s\n", string(name), httpResp.Body)
139+
fmt.Printf("%s Error! Unable to create denied cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
128140
} else {
129141
fmt.Println("Added", string(name), "to denied cert alerts.")
130142
}
@@ -136,7 +148,7 @@ func importPendingCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiM
136148
_, httpResp, reqErr := kfClient.PendingAlertApi.PendingAlertAddPendingAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
137149
name, _ := json.Marshal(alert.DisplayName)
138150
if reqErr != nil {
139-
fmt.Printf("Error, unable to create pending cert alert %s: %s\n", string(name), httpResp.Body)
151+
fmt.Printf("%s Error! Unable to create pending cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
140152
} else {
141153
fmt.Println("Added", string(name), "to pending cert alerts.")
142154
}
@@ -148,7 +160,7 @@ func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSs
148160
_, httpResp, reqErr := kfClient.SslApi.SslCreateNetwork(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Network(network).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
149161
name, _ := json.Marshal(network.Name)
150162
if reqErr != nil {
151-
fmt.Printf("Error, unable to create SSL network %s: %s\n", string(name), httpResp.Body)
163+
fmt.Printf("%s Error! Unable to create SSL network %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
152164
} else {
153165
fmt.Println("Added", string(name), "to SSL networks.")
154166
}
@@ -185,7 +197,7 @@ func importWorkflowDefinitions(workflowDefs []exportKeyfactorApiModelsWorkflowsD
185197
_, httpResp, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionCreateNewDefinition(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(workflowDefReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
186198
name, _ := json.Marshal(workflowDef.DisplayName)
187199
if reqErr != nil {
188-
fmt.Printf("Error! Unable to create a new workflow definition %s: %s\n", string(name), httpResp.Body)
200+
fmt.Printf("%s Error! Unable to create workflow definition %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
189201
} else {
190202
fmt.Println("Added", string(name), "to workflow definitions.")
191203
}
@@ -233,7 +245,7 @@ func importBuiltInReports(reports []exportModelsReport, kfClient *keyfactor_comm
233245
_, httpResp, reqErr := kfClient.ReportsApi.ReportsUpdateReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
234246
name, _ := json.Marshal(report.DisplayName)
235247
if reqErr != nil {
236-
fmt.Printf("Error! Unable to update built-in report %s: %s\n", string(name), httpResp.Body)
248+
fmt.Printf("%s Error! Unable to update built-in report %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
237249
} else {
238250
fmt.Println("Updated", string(name), "in built-in reports.")
239251
}
@@ -246,7 +258,7 @@ func importCustomReports(reports []keyfactor_command_client_api.ModelsCustomRepo
246258
_, httpResp, reqErr := kfClient.ReportsApi.ReportsCreateCustomReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(report).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
247259
name, _ := json.Marshal(report.DisplayName)
248260
if reqErr != nil {
249-
fmt.Printf("Error! Unable to create custom report %s: %s\n", string(name), httpResp.Body)
261+
fmt.Printf("%s Error! Unable to create custom report %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite)
250262
} else {
251263
fmt.Println("Added", string(name), "to custom reports.")
252264
}
@@ -258,7 +270,7 @@ func importSecurityRoles(roles []api.CreateSecurityRoleArg, kfClient *api.Client
258270
_, reqErr := kfClient.CreateSecurityRole(&role)
259271
name, _ := json.Marshal(role.Name)
260272
if reqErr != nil {
261-
fmt.Printf("Error! Unable to create security role %s: %s\n", string(name), reqErr)
273+
fmt.Printf("%s Error! Unable to create security role %s - %s%s\n", colorRed, string(name), reqErr, colorWhite)
262274
} else {
263275
fmt.Println("Added", string(name), "to security roles.")
264276
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"time"
1818
)
1919

20+
var colorRed = "\033[31m"
21+
var colorWhite = "\033[37m"
22+
2023
var xKeyfactorRequestedWith = "APIClient"
2124
var xKeyfactorApiVersion = "1"
2225

0 commit comments

Comments
 (0)