Skip to content

Commit 625a457

Browse files
authored
refactor: enable linter QF1003 'Convert if/else-if chain to tagged switch' (#123)
1 parent cd32746 commit 625a457

8 files changed

Lines changed: 59 additions & 44 deletions

File tree

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ linters:
3535
- all
3636
- '-QF1001' # disable rule 'Apply De Morgan’s law'
3737
- '-QF1012' # disable rule 'Use fmt.Fprintf instead of x.Write(fmt.Sprintf(...))'
38-
- '-QF1003' # disable rule 'Convert if/else-if chain to tagged switch'
3938
# https://golangci-lint.run/usage/linters/#staticcheck
4039
# https://staticcheck.dev/docs/configuration/options/#initialisms
4140
initialisms:

cmd/triggers/access.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,18 @@ func manageNamedEntities(cmd *cobra.Command, clients *shared.ClientFactory, toke
256256
}
257257

258258
if accessNamedEntities > 0 {
259-
if accessAction == "grant" {
259+
switch accessAction {
260+
case "grant":
260261
accessFlags.grant = true
261262
if !cmdutil.IsFlagChanged(cmd, "include-app-collaborators") && currentAccessType != types.PermissionNamedEntities {
262263
includeAppCollaborators, err = prompts.AddAppCollaboratorsToNamedEntitiesPrompt(ctx, clients.IO)
263264
if err != nil {
264265
return err
265266
}
266267
}
267-
} else if accessAction == "revoke" {
268+
case "revoke":
268269
accessFlags.revoke = true
269-
} else {
270+
default:
270271
return nil
271272
}
272273
} else {
@@ -563,13 +564,14 @@ func printAccess(cmd *cobra.Command, clients *shared.ClientFactory, token string
563564
return err
564565
}
565566

566-
if accessType == types.PermissionEveryone {
567+
switch accessType {
568+
case types.PermissionEveryone:
567569
var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app)
568570
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s", accessFlags.triggerID, everyoneAccessTypeDescription)
569-
} else if accessType == types.PermissionAppCollaborators {
571+
case types.PermissionAppCollaborators:
570572
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s:", accessFlags.triggerID, style.Pluralize("app collaborator", "app collaborators", len(userAccessList)))
571573
err = printAppCollaboratorsHelper(cmd, clients, token, userAccessList)
572-
} else if accessType == types.PermissionNamedEntities {
574+
case types.PermissionNamedEntities:
573575
err = printNamedEntitiesHelper(cmd, clients, token, userAccessList, "list")
574576
}
575577
clients.IO.PrintTrace(ctx, slacktrace.TriggersAccessSuccess)
@@ -581,16 +583,17 @@ func printCurrentAuthorizedEntities(cmd *cobra.Command, clients *shared.ClientFa
581583
ctx := cmd.Context()
582584

583585
cmd.Println()
584-
if currentAccessType == types.PermissionEveryone {
586+
switch currentAccessType {
587+
case types.PermissionEveryone:
585588
var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app)
586589
clients.IO.PrintInfo(ctx, false, "Trigger '%s' can be found and run by %s\n", accessFlags.triggerID, everyoneAccessTypeDescription)
587-
} else if currentAccessType == (types.PermissionAppCollaborators) {
590+
case types.PermissionAppCollaborators:
588591
clients.IO.PrintInfo(ctx, false, "Access is currently granted to %s:", style.Pluralize("app collaborator", "app collaborators", len(currentAccessList)))
589592
err := printAppCollaboratorsHelper(cmd, clients, token, currentAccessList)
590593
if err != nil {
591594
return err
592595
}
593-
} else {
596+
case types.PermissionNamedEntities:
594597
err := printNamedEntitiesHelper(cmd, clients, token, currentAccessList, "manage")
595598
if err != nil {
596599
return err
@@ -622,9 +625,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory,
622625
ctx := cmd.Context()
623626

624627
if len(entitiesAccessList) <= 0 {
625-
if action == "manage" {
628+
switch action {
629+
case "manage":
626630
clients.IO.PrintInfo(ctx, false, "Access is currently granted:")
627-
} else if action == "list" {
631+
case "list":
628632
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by:", accessFlags.triggerID)
629633
}
630634
clients.IO.PrintInfo(ctx, false, "nobody")
@@ -635,9 +639,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory,
635639

636640
if len(namedEntitiesAccessMap["users"]) > 0 {
637641
var userLabel = style.Pluralize("this user", "these users", len(namedEntitiesAccessMap["users"]))
638-
if action == "manage" {
642+
switch action {
643+
case "manage":
639644
clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to %s:", userLabel)
640-
} else if action == "list" {
645+
case "list":
641646
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s:", accessFlags.triggerID, userLabel)
642647
}
643648
for _, entity := range namedEntitiesAccessMap["users"] {
@@ -650,9 +655,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory,
650655
}
651656
if len(namedEntitiesAccessMap["channels"]) > 0 {
652657
var channelLabel = style.Pluralize("this channel", "these channels", len(namedEntitiesAccessMap["channels"]))
653-
if action == "manage" {
658+
switch action {
659+
case "manage":
654660
clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", channelLabel)
655-
} else if action == "list" {
661+
case "list":
656662
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, channelLabel)
657663
}
658664
for _, entity := range namedEntitiesAccessMap["channels"] {
@@ -665,9 +671,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory,
665671
}
666672
if len(namedEntitiesAccessMap["teams"]) > 0 {
667673
var teamLabel = style.Pluralize("this workspace", "these workspaces", len(namedEntitiesAccessMap["teams"]))
668-
if action == "manage" {
674+
switch action {
675+
case "manage":
669676
clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", teamLabel)
670-
} else if action == "list" {
677+
case "list":
671678
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, teamLabel)
672679
}
673680
for _, entity := range namedEntitiesAccessMap["teams"] {
@@ -680,9 +687,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory,
680687
}
681688
if len(namedEntitiesAccessMap["organizations"]) > 0 {
682689
var orgLabel = style.Pluralize("this organization", "these organizations", len(namedEntitiesAccessMap["organizations"]))
683-
if action == "manage" {
690+
switch action {
691+
case "manage":
684692
clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", orgLabel)
685-
} else if action == "list" {
693+
case "list":
686694
clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, orgLabel)
687695
}
688696
for _, entity := range namedEntitiesAccessMap["organizations"] {

cmd/triggers/triggers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ func sprintTrigger(ctx context.Context, t types.DeployedTrigger, clients *shared
142142
"nobody",
143143
))
144144
} else {
145-
if accessType == types.PermissionEveryone {
145+
switch accessType {
146+
case types.PermissionEveryone:
146147
var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app)
147148
triggerText = append(triggerText, fmt.Sprintf(
148149
style.Indent(style.Secondary("Can be found and used by:\n %s")),
149150
style.Indent(style.Secondary(everyoneAccessTypeDescription)),
150151
))
151-
} else if accessType == types.PermissionAppCollaborators {
152+
case types.PermissionAppCollaborators:
152153
triggerText = append(triggerText, fmt.Sprint(
153154
style.Indent(style.Secondary("Can be found and used by:")),
154155
))
@@ -162,7 +163,7 @@ func sprintTrigger(ctx context.Context, t types.DeployedTrigger, clients *shared
162163
userInfo.RealName, style.Secondary("@"+userInfo.Profile.DisplayName), style.Secondary(userInfo.ID),
163164
))
164165
}
165-
} else if accessType == types.PermissionNamedEntities {
166+
case types.PermissionNamedEntities:
166167
triggerText = append(triggerText, fmt.Sprint(
167168
style.Indent(style.Secondary("Can be found and used by:")),
168169
))

internal/api/triggerpermissions.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ func (c *Client) TriggerPermissionsSet(ctx context.Context, token, triggerID, en
119119
values.Add("trigger_id", triggerID)
120120
values.Add("permission_type", string(permissionType))
121121
if permissionType == types.PermissionNamedEntities && len(entities) > 0 {
122-
if entityType == "users" {
122+
switch entityType {
123+
case "users":
123124
values.Add("user_ids", entities)
124-
} else if entityType == "channels" {
125+
case "channels":
125126
values.Add("channel_ids", entities)
126-
} else if entityType == "workspaces" {
127+
case "workspaces":
127128
values.Add("team_ids", entities)
128-
} else if entityType == "organizations" {
129+
case "organizations":
129130
values.Add("org_ids", entities)
130131
}
131132
}
@@ -167,13 +168,14 @@ func (c *Client) TriggerPermissionsAddEntities(ctx context.Context, token, trigg
167168
var values = url.Values{}
168169
values.Add("token", token)
169170
values.Add("trigger_id", triggerID)
170-
if entityType == "users" {
171+
switch entityType {
172+
case "users":
171173
values.Add("user_ids", entities)
172-
} else if entityType == "channels" {
174+
case "channels":
173175
values.Add("channel_ids", entities)
174-
} else if entityType == "workspaces" {
176+
case "workspaces":
175177
values.Add("team_ids", entities)
176-
} else if entityType == "organizations" {
178+
case "organizations":
177179
values.Add("org_ids", entities)
178180
}
179181

@@ -214,13 +216,14 @@ func (c *Client) TriggerPermissionsRemoveEntities(ctx context.Context, token, tr
214216
var values = url.Values{}
215217
values.Add("token", token)
216218
values.Add("trigger_id", triggerID)
217-
if entityType == "users" {
219+
switch entityType {
220+
case "users":
218221
values.Add("user_ids", entities)
219-
} else if entityType == "channels" {
222+
case "channels":
220223
values.Add("channel_ids", entities)
221-
} else if entityType == "workspaces" {
224+
case "workspaces":
222225
values.Add("team_ids", entities)
223-
} else if entityType == "organizations" {
226+
case "organizations":
224227
values.Add("org_ids", entities)
225228
}
226229

internal/goutils/strings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ func ExtractFirstJSONFromString(s string) string {
5252

5353
var stack = []rune{}
5454
for i := start; i < len(s); i++ {
55-
if s[i] == '{' {
55+
switch s[i] {
56+
case '{':
5657
stack = append(stack, rune(s[i]))
57-
} else if s[i] == '}' {
58+
case '}':
5859
stack = stack[:len(stack)-1] // pop last item
5960
end = i // we found a potential ending bracket
6061
}

internal/pkg/apps/install.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,14 @@ func printNonSuccessInstallState(ctx context.Context, clients *shared.ClientFact
236236
primary string
237237
secondary string
238238
)
239-
if installState == types.InstallRequestPending {
239+
switch installState {
240+
case types.InstallRequestPending:
240241
primary = "Your request to install the app is pending"
241242
secondary = fmt.Sprintf("You will receive a Slackbot message after an admin has reviewed your request\nOnce your request is approved, complete installation by re-running %s", style.Commandf(clients.Config.Command, true))
242-
} else if installState == types.InstallRequestCancelled {
243+
case types.InstallRequestCancelled:
243244
primary = "Your request to install the app has been cancelled"
244245
secondary = ""
245-
} else if installState == types.InstallRequestNotSent {
246+
case types.InstallRequestNotSent:
246247
primary = "You've declined to send a request to an admin"
247248
secondary = "Please submit a request to install or update your app"
248249
}

internal/pkg/platform/activity.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,16 @@ func triggerExecutedToString(activity api.Activity) (result string) {
366366
msgs := []string{}
367367

368368
// Additional consideration for errors
369-
if activity.Level == types.INFO {
369+
switch activity.Level {
370+
case types.INFO:
370371
trigger, ok := activity.Payload["trigger"].(map[string]interface{})
371372
if ok {
372373
caser := cases.Title(language.English)
373374
msgs = append(msgs, fmt.Sprintf("%s trigger successfully started execution of function '%s'", caser.String(trigger["type"].(string)), activity.Payload["function_name"]))
374375
} else {
375376
msgs = append(msgs, fmt.Sprintf("Trigger successfully started execution of function '%s'", activity.Payload["function_name"]))
376377
}
377-
} else if activity.Level == types.ERROR {
378+
case types.ERROR:
378379
msgs = append(msgs, fmt.Sprintf("Trigger for workflow '%s' failed: %s", activity.Payload["function_name"], activity.Payload["reason"]))
379380
// Default format for the errors message is the raw, json-encoded string
380381
var payloadErrors = []string{fmt.Sprintf("• %s", activity.Payload["errors"])}

internal/prompts/trigger_permissions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func TriggerChooseNamedEntityPrompt(ctx context.Context, clients *shared.ClientF
7575
"cancel",
7676
}
7777

78-
if accessAction == "grant" {
78+
switch accessAction {
79+
case "grant":
7980
actions = []string{
8081
"add_user",
8182
"add_channel",
@@ -90,7 +91,7 @@ func TriggerChooseNamedEntityPrompt(ctx context.Context, clients *shared.ClientF
9091
"grant an organization access",
9192
"cancel",
9293
}
93-
} else if accessAction == "revoke" {
94+
case "revoke":
9495
actions = []string{
9596
"remove_user",
9697
"remove_channel",

0 commit comments

Comments
 (0)