diff --git a/.golangci.yml b/.golangci.yml index 438cf44a..3931f2d5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,7 +35,6 @@ linters: - all - '-QF1001' # disable rule 'Apply De Morgan’s law' - '-QF1012' # disable rule 'Use fmt.Fprintf instead of x.Write(fmt.Sprintf(...))' - - '-QF1003' # disable rule 'Convert if/else-if chain to tagged switch' # https://golangci-lint.run/usage/linters/#staticcheck # https://staticcheck.dev/docs/configuration/options/#initialisms initialisms: diff --git a/cmd/triggers/access.go b/cmd/triggers/access.go index 65b5e9a7..1e8f05a7 100644 --- a/cmd/triggers/access.go +++ b/cmd/triggers/access.go @@ -256,7 +256,8 @@ func manageNamedEntities(cmd *cobra.Command, clients *shared.ClientFactory, toke } if accessNamedEntities > 0 { - if accessAction == "grant" { + switch accessAction { + case "grant": accessFlags.grant = true if !cmdutil.IsFlagChanged(cmd, "include-app-collaborators") && currentAccessType != types.PermissionNamedEntities { includeAppCollaborators, err = prompts.AddAppCollaboratorsToNamedEntitiesPrompt(ctx, clients.IO) @@ -264,9 +265,9 @@ func manageNamedEntities(cmd *cobra.Command, clients *shared.ClientFactory, toke return err } } - } else if accessAction == "revoke" { + case "revoke": accessFlags.revoke = true - } else { + default: return nil } } else { @@ -563,13 +564,14 @@ func printAccess(cmd *cobra.Command, clients *shared.ClientFactory, token string return err } - if accessType == types.PermissionEveryone { + switch accessType { + case types.PermissionEveryone: var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app) clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s", accessFlags.triggerID, everyoneAccessTypeDescription) - } else if accessType == types.PermissionAppCollaborators { + case types.PermissionAppCollaborators: clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s:", accessFlags.triggerID, style.Pluralize("app collaborator", "app collaborators", len(userAccessList))) err = printAppCollaboratorsHelper(cmd, clients, token, userAccessList) - } else if accessType == types.PermissionNamedEntities { + case types.PermissionNamedEntities: err = printNamedEntitiesHelper(cmd, clients, token, userAccessList, "list") } clients.IO.PrintTrace(ctx, slacktrace.TriggersAccessSuccess) @@ -581,16 +583,17 @@ func printCurrentAuthorizedEntities(cmd *cobra.Command, clients *shared.ClientFa ctx := cmd.Context() cmd.Println() - if currentAccessType == types.PermissionEveryone { + switch currentAccessType { + case types.PermissionEveryone: var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app) clients.IO.PrintInfo(ctx, false, "Trigger '%s' can be found and run by %s\n", accessFlags.triggerID, everyoneAccessTypeDescription) - } else if currentAccessType == (types.PermissionAppCollaborators) { + case types.PermissionAppCollaborators: clients.IO.PrintInfo(ctx, false, "Access is currently granted to %s:", style.Pluralize("app collaborator", "app collaborators", len(currentAccessList))) err := printAppCollaboratorsHelper(cmd, clients, token, currentAccessList) if err != nil { return err } - } else { + case types.PermissionNamedEntities: err := printNamedEntitiesHelper(cmd, clients, token, currentAccessList, "manage") if err != nil { return err @@ -622,9 +625,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory, ctx := cmd.Context() if len(entitiesAccessList) <= 0 { - if action == "manage" { + switch action { + case "manage": clients.IO.PrintInfo(ctx, false, "Access is currently granted:") - } else if action == "list" { + case "list": clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by:", accessFlags.triggerID) } clients.IO.PrintInfo(ctx, false, "nobody") @@ -635,9 +639,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory, if len(namedEntitiesAccessMap["users"]) > 0 { var userLabel = style.Pluralize("this user", "these users", len(namedEntitiesAccessMap["users"])) - if action == "manage" { + switch action { + case "manage": clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to %s:", userLabel) - } else if action == "list" { + case "list": clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by %s:", accessFlags.triggerID, userLabel) } for _, entity := range namedEntitiesAccessMap["users"] { @@ -650,9 +655,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory, } if len(namedEntitiesAccessMap["channels"]) > 0 { var channelLabel = style.Pluralize("this channel", "these channels", len(namedEntitiesAccessMap["channels"])) - if action == "manage" { + switch action { + case "manage": clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", channelLabel) - } else if action == "list" { + case "list": clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, channelLabel) } for _, entity := range namedEntitiesAccessMap["channels"] { @@ -665,9 +671,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory, } if len(namedEntitiesAccessMap["teams"]) > 0 { var teamLabel = style.Pluralize("this workspace", "these workspaces", len(namedEntitiesAccessMap["teams"])) - if action == "manage" { + switch action { + case "manage": clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", teamLabel) - } else if action == "list" { + case "list": clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, teamLabel) } for _, entity := range namedEntitiesAccessMap["teams"] { @@ -680,9 +687,10 @@ func printNamedEntitiesHelper(cmd *cobra.Command, clients *shared.ClientFactory, } if len(namedEntitiesAccessMap["organizations"]) > 0 { var orgLabel = style.Pluralize("this organization", "these organizations", len(namedEntitiesAccessMap["organizations"])) - if action == "manage" { + switch action { + case "manage": clients.IO.PrintInfo(ctx, false, "\nAccess is currently granted to all members of %s:", orgLabel) - } else if action == "list" { + case "list": clients.IO.PrintInfo(ctx, false, "\nTrigger '%s' can be found and run by all members of %s:", accessFlags.triggerID, orgLabel) } for _, entity := range namedEntitiesAccessMap["organizations"] { diff --git a/cmd/triggers/triggers.go b/cmd/triggers/triggers.go index 8bb0a25c..e5d1f45f 100644 --- a/cmd/triggers/triggers.go +++ b/cmd/triggers/triggers.go @@ -142,13 +142,14 @@ func sprintTrigger(ctx context.Context, t types.DeployedTrigger, clients *shared "nobody", )) } else { - if accessType == types.PermissionEveryone { + switch accessType { + case types.PermissionEveryone: var everyoneAccessTypeDescription = types.GetAccessTypeDescriptionForEveryone(app) triggerText = append(triggerText, fmt.Sprintf( style.Indent(style.Secondary("Can be found and used by:\n %s")), style.Indent(style.Secondary(everyoneAccessTypeDescription)), )) - } else if accessType == types.PermissionAppCollaborators { + case types.PermissionAppCollaborators: triggerText = append(triggerText, fmt.Sprint( style.Indent(style.Secondary("Can be found and used by:")), )) @@ -162,7 +163,7 @@ func sprintTrigger(ctx context.Context, t types.DeployedTrigger, clients *shared userInfo.RealName, style.Secondary("@"+userInfo.Profile.DisplayName), style.Secondary(userInfo.ID), )) } - } else if accessType == types.PermissionNamedEntities { + case types.PermissionNamedEntities: triggerText = append(triggerText, fmt.Sprint( style.Indent(style.Secondary("Can be found and used by:")), )) diff --git a/internal/api/triggerpermissions.go b/internal/api/triggerpermissions.go index 9d4786d4..e667b0a7 100644 --- a/internal/api/triggerpermissions.go +++ b/internal/api/triggerpermissions.go @@ -119,13 +119,14 @@ func (c *Client) TriggerPermissionsSet(ctx context.Context, token, triggerID, en values.Add("trigger_id", triggerID) values.Add("permission_type", string(permissionType)) if permissionType == types.PermissionNamedEntities && len(entities) > 0 { - if entityType == "users" { + switch entityType { + case "users": values.Add("user_ids", entities) - } else if entityType == "channels" { + case "channels": values.Add("channel_ids", entities) - } else if entityType == "workspaces" { + case "workspaces": values.Add("team_ids", entities) - } else if entityType == "organizations" { + case "organizations": values.Add("org_ids", entities) } } @@ -167,13 +168,14 @@ func (c *Client) TriggerPermissionsAddEntities(ctx context.Context, token, trigg var values = url.Values{} values.Add("token", token) values.Add("trigger_id", triggerID) - if entityType == "users" { + switch entityType { + case "users": values.Add("user_ids", entities) - } else if entityType == "channels" { + case "channels": values.Add("channel_ids", entities) - } else if entityType == "workspaces" { + case "workspaces": values.Add("team_ids", entities) - } else if entityType == "organizations" { + case "organizations": values.Add("org_ids", entities) } @@ -214,13 +216,14 @@ func (c *Client) TriggerPermissionsRemoveEntities(ctx context.Context, token, tr var values = url.Values{} values.Add("token", token) values.Add("trigger_id", triggerID) - if entityType == "users" { + switch entityType { + case "users": values.Add("user_ids", entities) - } else if entityType == "channels" { + case "channels": values.Add("channel_ids", entities) - } else if entityType == "workspaces" { + case "workspaces": values.Add("team_ids", entities) - } else if entityType == "organizations" { + case "organizations": values.Add("org_ids", entities) } diff --git a/internal/goutils/strings.go b/internal/goutils/strings.go index 5313a2b0..f80cb491 100644 --- a/internal/goutils/strings.go +++ b/internal/goutils/strings.go @@ -52,9 +52,10 @@ func ExtractFirstJSONFromString(s string) string { var stack = []rune{} for i := start; i < len(s); i++ { - if s[i] == '{' { + switch s[i] { + case '{': stack = append(stack, rune(s[i])) - } else if s[i] == '}' { + case '}': stack = stack[:len(stack)-1] // pop last item end = i // we found a potential ending bracket } diff --git a/internal/pkg/apps/install.go b/internal/pkg/apps/install.go index 273ebef7..e94c3e8c 100644 --- a/internal/pkg/apps/install.go +++ b/internal/pkg/apps/install.go @@ -236,13 +236,14 @@ func printNonSuccessInstallState(ctx context.Context, clients *shared.ClientFact primary string secondary string ) - if installState == types.InstallRequestPending { + switch installState { + case types.InstallRequestPending: primary = "Your request to install the app is pending" 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)) - } else if installState == types.InstallRequestCancelled { + case types.InstallRequestCancelled: primary = "Your request to install the app has been cancelled" secondary = "" - } else if installState == types.InstallRequestNotSent { + case types.InstallRequestNotSent: primary = "You've declined to send a request to an admin" secondary = "Please submit a request to install or update your app" } diff --git a/internal/pkg/platform/activity.go b/internal/pkg/platform/activity.go index 3a418b5e..f089dcc4 100644 --- a/internal/pkg/platform/activity.go +++ b/internal/pkg/platform/activity.go @@ -366,7 +366,8 @@ func triggerExecutedToString(activity api.Activity) (result string) { msgs := []string{} // Additional consideration for errors - if activity.Level == types.INFO { + switch activity.Level { + case types.INFO: trigger, ok := activity.Payload["trigger"].(map[string]interface{}) if ok { caser := cases.Title(language.English) @@ -374,7 +375,7 @@ func triggerExecutedToString(activity api.Activity) (result string) { } else { msgs = append(msgs, fmt.Sprintf("Trigger successfully started execution of function '%s'", activity.Payload["function_name"])) } - } else if activity.Level == types.ERROR { + case types.ERROR: msgs = append(msgs, fmt.Sprintf("Trigger for workflow '%s' failed: %s", activity.Payload["function_name"], activity.Payload["reason"])) // Default format for the errors message is the raw, json-encoded string var payloadErrors = []string{fmt.Sprintf("• %s", activity.Payload["errors"])} diff --git a/internal/prompts/trigger_permissions.go b/internal/prompts/trigger_permissions.go index 0babed4f..5c4a9f00 100644 --- a/internal/prompts/trigger_permissions.go +++ b/internal/prompts/trigger_permissions.go @@ -75,7 +75,8 @@ func TriggerChooseNamedEntityPrompt(ctx context.Context, clients *shared.ClientF "cancel", } - if accessAction == "grant" { + switch accessAction { + case "grant": actions = []string{ "add_user", "add_channel", @@ -90,7 +91,7 @@ func TriggerChooseNamedEntityPrompt(ctx context.Context, clients *shared.ClientF "grant an organization access", "cancel", } - } else if accessAction == "revoke" { + case "revoke": actions = []string{ "remove_user", "remove_channel",