Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ jobs:
with:
fetch-depth: 0

- name: Clean duplicate toolchain files
run: rm -rf "$(go env GOPATH)/pkg/mod/golang.org/toolchain*"

- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: go.mod


- name: ci/check-style
run: make check-style

Expand Down
5 changes: 1 addition & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Docs: https://golangci-lint.run/usage/configuration/#config-file

version: 2
run:
timeout: 5m

Expand All @@ -20,8 +19,6 @@ linters-settings:
linters:
disable-all: true
enable:
- gofmt
- gosimple
- govet
- ineffassign
- predeclared
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ GOIMPORTS_VER := master
GOIMPORTS_BIN := goimports
GOIMPORTS := $(TOOLS_BIN_DIR)/$(GOIMPORTS_BIN)

GOLANGCILINT_VER := v1.64.8
GOLANGCILINT_VER := v2.0.2
GOLANGCILINT_BIN := golangci-lint
GOLANGCILINT := $(TOOLS_BIN_DIR)/$(GOLANGCILINT_BIN)

Expand Down Expand Up @@ -96,12 +96,12 @@ check-style: govet lint goformat goimports
## Runs lint against all packages.
lint: $(GOLANGCILINT)
@echo Running golangci-lint
$(GOLANGCILINT) run
$(GOLANGCILINT) run --config .golangci.yml

## Runs lint against all packages for changes only
lint-changes: $(GOLANGCILINT)
@echo Running golangci-lint over changes only
$(GOLANGCILINT) run -n
$(GOLANGCILINT) run -n --config .golangci.yml

## Runs govet against all packages.
.PHONY: vet
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud/clicontext/clicontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func WriteContexts(contexts *Contexts) error {
if err != nil {
return err
}
defer file.Close()
defer func() { _ = file.Close() }()

encoder := json.NewEncoder(file)
err = encoder.Encode(contexts)
Expand Down
15 changes: 11 additions & 4 deletions cmd/cloud/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"reflect"

log "github.com/sirupsen/logrus"

"github.com/mattermost/mattermost-cloud/cmd/cloud/clicontext"
"github.com/mattermost/mattermost-cloud/internal/auth"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -90,7 +92,9 @@ type updateContextFlags struct {
func (f *updateContextFlags) addFlags(command *cobra.Command) {
f.createContextFlags.addFlags(command)
command.Flags().StringVar(&f.Context, "context", "", "Name of the context to update")
command.MarkFlagRequired("context")
if err := command.MarkFlagRequired("context"); err != nil {
log.Fatalf("failed to mark flag required: %v", err)
}
command.Flags().BoolVar(&f.ClearAuth, "clear-auth", false, "Turns off authentication for this context")
command.Flags().BoolVar(&f.ConfirmationRequired, "confirmation-required", false, "Require confirmation for commands run in this context")
}
Expand Down Expand Up @@ -244,8 +248,9 @@ func newCmdContextCreate() *cobra.Command {
}

flags.addFlags(cmd)
cmd.MarkFlagRequired("server-url")

if err := cmd.MarkFlagRequired("server-url"); err != nil {
log.Fatalf("failed to mark flag required: %v", err)
}
return cmd
}

Expand All @@ -255,7 +260,9 @@ type deleteContextFlags struct {

func (f *deleteContextFlags) addFlags(command *cobra.Command) {
command.Flags().StringVar(&f.contextName, "context", "", "Name of the context to delete")
command.MarkFlagRequired("context")
if err := command.MarkFlagRequired("context"); err != nil {
log.Fatalf("failed to mark flag required: %v", err)
}
}

func newCmdContextDelete() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func newCmdInstallationUpdateDeletion() *cobra.Command {
client := createClient(command.Context(), flags.clusterFlags)

request := &model.PatchInstallationDeletionRequest{}
if flags.installationDeletionPatchRequestOptionsChanged.futureDeletionTimeChanged {
if flags.futureDeletionTimeChanged {
newExpiryTimeMillis := model.GetMillisAtTime(time.Now().Add(flags.futureDeletionTime))
request.DeletionPendingExpiry = &newExpiryTimeMillis
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/cloud/installation_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ func (flags *installationUpdateFlags) GetPatchInstallationRequest() (*model.Patc

if flags.allowedIPRangesChanged {
allowedIPRanges := &model.AllowedIPRanges{}
allowedIPRanges.FromJSONString(flags.allowedIPRanges)
_, jsonErr := allowedIPRanges.FromJSONString(flags.allowedIPRanges)
if jsonErr != nil {
return nil, jsonErr
}
allowedIPRanges, err := allowedIPRanges.FromJSONString(flags.allowedIPRanges)
if err != nil {
return nil, err
Expand Down
11 changes: 9 additions & 2 deletions cmd/cloud/logger_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ func (hook *ClusterLoggerHook) fileWrite(entry *logrus.Entry, kind string) error
log.Println("failed to open logfile:", path, err)
return err
}
defer fd.Close()
defer func() {
if err := fd.Close(); err != nil {
log.Printf("failed to close fd: %v", err)
}
}()

// use our formatter instead of entry.String()
msg, err := hook.formatter.Format(entry)
Expand All @@ -91,7 +95,10 @@ func (hook *ClusterLoggerHook) fileWrite(entry *logrus.Entry, kind string) error
log.Println("failed to generate string for entry:", err)
return err
}
fd.Write(msg)
if _, err := fd.Write(msg); err != nil {
log.Println("failed to write to logfile:", err)
return err
}
return nil
}

Expand Down
12 changes: 10 additions & 2 deletions cmd/cloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,11 @@ func executeServerCmd(flags serverFlags) error {
}

standardSupervisor := supervisor.NewScheduler(multiDoer, time.Duration(flags.poll)*time.Second, logger)
defer standardSupervisor.Close()
defer func() {
if err := standardSupervisor.Close(); err != nil {
logrus.WithError(err).Error("failed to close standardSupervisor")
}
}()

if flags.slowPoll == 0 {
logger.WithField("slow-poll", flags.slowPoll).Info("Slow scheduler is disabled")
Expand All @@ -422,7 +426,11 @@ func executeServerCmd(flags serverFlags) error {
var slowMultiDoer supervisor.MultiDoer
slowMultiDoer = append(slowMultiDoer, supervisor.NewInstallationDeletionSupervisor(instanceID, flags.installationDeletionPendingTime, flags.installationDeletionMaxUpdating, sqlStore, eventsProducer, logger))
slowSupervisor := supervisor.NewScheduler(slowMultiDoer, time.Duration(flags.slowPoll)*time.Second, logger)
defer slowSupervisor.Close()
defer func() {
if err := slowSupervisor.Close(); err != nil {
logrus.WithError(err).Error("failed to close slowSupervisor")
}
}()
}

metricsRouter := mux.NewRouter()
Expand Down
9 changes: 7 additions & 2 deletions cmd/provisioner-code-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ func newRootCmd() *cobra.Command {

// Binds all flags as viper values
func bindFlags(cmd *cobra.Command) {
viper.BindPFlags(cmd.PersistentFlags())
viper.BindPFlags(cmd.Flags())
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
logrus.WithError(err).Fatal("failed to bind persistent flags")
}
if err := viper.BindPFlags(cmd.Flags()); err != nil {
logrus.WithError(err).Fatal("failed to bind command flags")
}

for _, c := range cmd.Commands() {
bindFlags(c)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/tools/cloudburst/cloudburst.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ func (b *Blaster) cleanupInstallations(installations map[string]*cloud.Installat
fallthrough
case cloud.InstallationStateDeletionInProgress:
default:
b.client.DeleteInstallation(fetched.ID)
if err := b.client.DeleteInstallation(fetched.ID); err != nil {
log.WithError(err).Error("failed to delete installation")
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/tools/ctest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"

"github.com/mattermost/mattermost-cloud/model"
"github.com/pkg/errors"
)
Expand All @@ -36,7 +38,11 @@ func runInstallationLifecycleTest(request *model.CreateInstallationRequest, clie
if err != nil {
return errors.Wrap(err, "failed to run enhanced ping test")
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Error("failed to close resp.Body")
}
}()
Comment on lines -39 to +45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this being enforced. Can we disable this rule?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide some reasoning against? We can disable it for sure but I would like to know why you oppose to this which linter suggests as best practice. I already disabled some static-checks because they were leading to huge code refactoring, so to disable also this to be honest I want a good objection.


if resp.StatusCode != http.StatusOK {
var body []byte
Expand Down
5 changes: 2 additions & 3 deletions cmd/tools/ctest/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"net/http"
"time"

"github.com/mattermost/mattermost-cloud/model"
cloud "github.com/mattermost/mattermost-cloud/model"
)

func webhookHandler(w http.ResponseWriter, r *http.Request, c chan *model.WebhookPayload) {
func webhookHandler(w http.ResponseWriter, r *http.Request, c chan *cloud.WebhookPayload) {
webhook, err := cloud.WebhookPayloadFromReader(r.Body)
if err != nil {
logger.WithError(err).Error("Faled to parse webhook")
Expand All @@ -41,7 +40,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request, c chan *model.Webhoo
w.WriteHeader(http.StatusOK)
}

func startWebhookListener(port string, c chan *model.WebhookPayload) func() {
func startWebhookListener(port string, c chan *cloud.WebhookPayload) func() {
logger.Infof("Starting cloud webhook listener on port %s", port)

srv := &http.Server{Addr: fmt.Sprintf(":%s", port)}
Expand Down
7 changes: 6 additions & 1 deletion cmd/tools/cwl/cwl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/http"
"os"

"github.com/sirupsen/logrus"

cloud "github.com/mattermost/mattermost-cloud/model"

"github.com/0xAX/notificator"
Expand Down Expand Up @@ -51,7 +53,10 @@ func handler(w http.ResponseWriter, r *http.Request) {

message := fmt.Sprintf("[ %s | %s ] %s -> %s", wType, webhook.ID[0:4], webhook.OldState, webhook.NewState)
log.Print(message)
notify.Push("Cloud Webhook Listener", message, icon, notificator.UR_NORMAL)
err = notify.Push("Cloud Webhook Listener", message, icon, notificator.UR_NORMAL)
if err != nil {
logrus.Errorf("failed to send notification: %v", err)
}

w.WriteHeader(http.StatusOK)
}
Expand Down
Loading