diff --git a/pkg/cmd/grafana-cli/commands/install_command.go b/pkg/cmd/grafana-cli/commands/install_command.go index d758633fea5..bec1d51bff4 100644 --- a/pkg/cmd/grafana-cli/commands/install_command.go +++ b/pkg/cmd/grafana-cli/commands/install_command.go @@ -14,6 +14,7 @@ import ( "strings" "github.com/fatih/color" + "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models" s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services" @@ -135,7 +136,7 @@ func downloadFile(pluginName, filePath, url string) (err error) { } else { failure := fmt.Sprintf("%v", r) if failure == "runtime error: makeslice: len out of range" { - err = fmt.Errorf("Corrupt http response from source. Please try again.\n") + err = fmt.Errorf("Corrupt http response from source. Please try again") } else { panic(r) } diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index c10212329cf..2ac326ed35d 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -12,36 +12,33 @@ import ( "time" "github.com/facebookgo/inject" + "golang.org/x/sync/errgroup" + "github.com/grafana/grafana/pkg/api" "github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/bus" - "github.com/grafana/grafana/pkg/login" - "github.com/grafana/grafana/pkg/login/social" - "github.com/grafana/grafana/pkg/middleware" - "github.com/grafana/grafana/pkg/registry" - - "golang.org/x/sync/errgroup" - - "github.com/grafana/grafana/pkg/log" - "github.com/grafana/grafana/pkg/services/cache" - "github.com/grafana/grafana/pkg/setting" - - // self registering services _ "github.com/grafana/grafana/pkg/extensions" _ "github.com/grafana/grafana/pkg/infra/metrics" _ "github.com/grafana/grafana/pkg/infra/remotecache" _ "github.com/grafana/grafana/pkg/infra/serverlock" _ "github.com/grafana/grafana/pkg/infra/tracing" _ "github.com/grafana/grafana/pkg/infra/usagestats" + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/login" + "github.com/grafana/grafana/pkg/login/social" + "github.com/grafana/grafana/pkg/middleware" _ "github.com/grafana/grafana/pkg/plugins" + "github.com/grafana/grafana/pkg/registry" _ "github.com/grafana/grafana/pkg/services/alerting" _ "github.com/grafana/grafana/pkg/services/auth" + "github.com/grafana/grafana/pkg/services/cache" _ "github.com/grafana/grafana/pkg/services/cleanup" _ "github.com/grafana/grafana/pkg/services/notifications" _ "github.com/grafana/grafana/pkg/services/provisioning" _ "github.com/grafana/grafana/pkg/services/rendering" _ "github.com/grafana/grafana/pkg/services/search" _ "github.com/grafana/grafana/pkg/services/sqlstore" + "github.com/grafana/grafana/pkg/setting" ) func NewGrafanaServer() *GrafanaServerImpl { @@ -238,7 +235,7 @@ func sendSystemdNotification(state string) error { notifySocket := os.Getenv("NOTIFY_SOCKET") if notifySocket == "" { - return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset.") + return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset") } socketAddr := &net.UnixAddr{ diff --git a/pkg/components/imguploader/webdavuploader.go b/pkg/components/imguploader/webdavuploader.go index ed6b14725c0..d7fa7f4889a 100644 --- a/pkg/components/imguploader/webdavuploader.go +++ b/pkg/components/imguploader/webdavuploader.go @@ -39,11 +39,11 @@ var netClient = &http.Client{ func (u *WebdavUploader) PublicURL(filename string) string { if strings.Contains(u.public_url, "${file}") { return strings.Replace(u.public_url, "${file}", filename, -1) - } else { - publicURL, _ := url.Parse(u.public_url) - publicURL.Path = path.Join(publicURL.Path, filename) - return publicURL.String() } + + publicURL, _ := url.Parse(u.public_url) + publicURL.Path = path.Join(publicURL.Path, filename) + return publicURL.String() } func (u *WebdavUploader) Upload(ctx context.Context, pa string) (string, error) { diff --git a/pkg/infra/usagestats/usage_stats.go b/pkg/infra/usagestats/usage_stats.go index 77951d1c899..8b20391cbb4 100644 --- a/pkg/infra/usagestats/usage_stats.go +++ b/pkg/infra/usagestats/usage_stats.go @@ -171,7 +171,6 @@ func (uss *UsageStatsService) updateTotalStats() { func getEdition() string { if setting.IsEnterprise { return "enterprise" - } else { - return "oss" } + return "oss" } diff --git a/pkg/log/file.go b/pkg/log/file.go index b8430dc6086..b068f3103dc 100644 --- a/pkg/log/file.go +++ b/pkg/log/file.go @@ -150,7 +150,7 @@ func (w *FileLogWriter) initFd() error { fd := w.mw.fd finfo, err := fd.Stat() if err != nil { - return fmt.Errorf("get stat: %s\n", err) + return fmt.Errorf("get stat: %s", err) } w.maxsize_cursize = int(finfo.Size()) w.daily_opendate = time.Now().Day() @@ -180,7 +180,7 @@ func (w *FileLogWriter) DoRotate() error { } // return error if the last file checked still existed if err == nil { - return fmt.Errorf("rotate: cannot find free log number to rename %s\n", w.Filename) + return fmt.Errorf("rotate: cannot find free log number to rename %s", w.Filename) } // block Logger's io.Writer @@ -193,12 +193,12 @@ func (w *FileLogWriter) DoRotate() error { // close fd before rename // Rename the file to its newfound home if err = os.Rename(w.Filename, fname); err != nil { - return fmt.Errorf("Rotate: %s\n", err) + return fmt.Errorf("Rotate: %s", err) } // re-start logger if err = w.StartLogger(); err != nil { - return fmt.Errorf("Rotate StartLogger: %s\n", err) + return fmt.Errorf("Rotate StartLogger: %s", err) } go w.deleteOldLog() diff --git a/pkg/login/auth.go b/pkg/login/auth.go index b766d963328..301927b6ad3 100644 --- a/pkg/login/auth.go +++ b/pkg/login/auth.go @@ -14,7 +14,7 @@ var ( ErrProviderDeniedRequest = errors.New("Login provider denied login request") ErrSignUpNotAllowed = errors.New("Signup is not allowed for this adapter") ErrTooManyLoginAttempts = errors.New("Too many consecutive incorrect login attempts for user. Login for user temporarily blocked") - ErrPasswordEmpty = errors.New("No password provided.") + ErrPasswordEmpty = errors.New("No password provided") ErrUsersQuotaReached = errors.New("Users quota reached") ErrGettingUserQuota = errors.New("Error getting user quota") ) diff --git a/pkg/models/alert.go b/pkg/models/alert.go index e8171f8cf0b..b9bcd3f0615 100644 --- a/pkg/models/alert.go +++ b/pkg/models/alert.go @@ -1,9 +1,8 @@ package models import ( - "time" - "fmt" + "time" "github.com/grafana/grafana/pkg/components/simplejson" ) @@ -36,7 +35,7 @@ const ( var ( ErrCannotChangeStateOnPausedAlert = fmt.Errorf("Cannot change state on pause alert") - ErrRequiresNewState = fmt.Errorf("update alert state requires a new state.") + ErrRequiresNewState = fmt.Errorf("update alert state requires a new state") ) func (s AlertStateType) IsValid() bool { diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index ccfd6ee84db..9bcee74312c 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -11,7 +11,7 @@ var ( ErrNotificationFrequencyNotFound = errors.New("Notification frequency not specified") ErrAlertNotificationStateNotFound = errors.New("alert notification state not found") ErrAlertNotificationStateVersionConflict = errors.New("alert notification state update version conflict") - ErrAlertNotificationStateAlreadyExist = errors.New("alert notification state already exists.") + ErrAlertNotificationStateAlreadyExist = errors.New("alert notification state already exists") ErrAlertNotificationFailedGenerateUniqueUid = errors.New("Failed to generate unique alert notification uid") ) diff --git a/pkg/models/dashboard_acl.go b/pkg/models/dashboard_acl.go index 5fc09bd16b5..d50da271acc 100644 --- a/pkg/models/dashboard_acl.go +++ b/pkg/models/dashboard_acl.go @@ -24,10 +24,10 @@ func (p PermissionType) String() string { // Typed errors var ( - ErrDashboardAclInfoMissing = errors.New("User id and team id cannot both be empty for a dashboard permission.") - ErrDashboardPermissionDashboardEmpty = errors.New("Dashboard Id must be greater than zero for a dashboard permission.") - ErrFolderAclInfoMissing = errors.New("User id and team id cannot both be empty for a folder permission.") - ErrFolderPermissionFolderEmpty = errors.New("Folder Id must be greater than zero for a folder permission.") + ErrDashboardAclInfoMissing = errors.New("User id and team id cannot both be empty for a dashboard permission") + ErrDashboardPermissionDashboardEmpty = errors.New("Dashboard Id must be greater than zero for a dashboard permission") + ErrFolderAclInfoMissing = errors.New("User id and team id cannot both be empty for a folder permission") + ErrFolderPermissionFolderEmpty = errors.New("Folder Id must be greater than zero for a folder permission") ) // Dashboard ACL model diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index 491126e4fc6..10d6b38cc7a 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -30,7 +30,7 @@ var ( ErrDataSourceNotFound = errors.New("Data source not found") ErrDataSourceNameExists = errors.New("Data source with same name already exists") ErrDataSourceUpdatingOldVersion = errors.New("Trying to update old version of datasource") - ErrDatasourceIsReadOnly = errors.New("Data source is readonly. Can only be updated from configuration.") + ErrDatasourceIsReadOnly = errors.New("Data source is readonly. Can only be updated from configuration") ErrDataSourceAccessDenied = errors.New("Data source access denied") ) diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 4b25ecb4dc7..e734dfd8e4a 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -3,7 +3,7 @@ package models import "errors" var ErrInvalidEmailCode = errors.New("Invalid or expired email code") -var ErrSmtpNotEnabled = errors.New("SMTP not configured, check your grafana.ini config file's [smtp] section.") +var ErrSmtpNotEnabled = errors.New("SMTP not configured, check your grafana.ini config file's [smtp] section") type SendEmailCommand struct { To []string diff --git a/pkg/services/alerting/rule.go b/pkg/services/alerting/rule.go index f55488dc795..f62690a2d96 100644 --- a/pkg/services/alerting/rule.go +++ b/pkg/services/alerting/rule.go @@ -130,11 +130,11 @@ func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) { if id, err := jsonModel.Get("id").Int64(); err == nil { model.Notifications = append(model.Notifications, fmt.Sprintf("%09d", id)) } else { - if uid, err := jsonModel.Get("uid").String(); err != nil { + uid, err := jsonModel.Get("uid").String() + if err != nil { return nil, ValidationError{Reason: "Neither id nor uid is specified, " + err.Error(), DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId} - } else { - model.Notifications = append(model.Notifications, uid) } + model.Notifications = append(model.Notifications, uid) } } diff --git a/pkg/services/rendering/rendering.go b/pkg/services/rendering/rendering.go index 0b4f23e93b4..06db73771b2 100644 --- a/pkg/services/rendering/rendering.go +++ b/pkg/services/rendering/rendering.go @@ -105,9 +105,8 @@ func (rs *RenderingService) Render(ctx context.Context, opts Opts) (*RenderResul if rs.renderAction != nil { return rs.renderAction(ctx, opts) - } else { - return nil, fmt.Errorf("No renderer found") } + return nil, fmt.Errorf("No renderer found") } func (rs *RenderingService) getFilePathForNewImage() string { diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index 898ab9ebc66..f054f2b94d0 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -213,11 +213,12 @@ func getAlertNotificationWithUidInternal(query *m.GetAlertNotificationsWithUidQu func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error { return inTransaction(func(sess *DBSession) error { if cmd.Uid == "" { - if uid, uidGenerationErr := generateNewAlertNotificationUid(sess, cmd.OrgId); uidGenerationErr != nil { + uid, uidGenerationErr := generateNewAlertNotificationUid(sess, cmd.OrgId) + if uidGenerationErr != nil { return uidGenerationErr - } else { - cmd.Uid = uid } + + cmd.Uid = uid } existingQuery := &m.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid} err := getAlertNotificationWithUidInternal(existingQuery, sess) diff --git a/pkg/services/sqlstore/tls_mysql.go b/pkg/services/sqlstore/tls_mysql.go index 3c9475e19a0..3697135c717 100644 --- a/pkg/services/sqlstore/tls_mysql.go +++ b/pkg/services/sqlstore/tls_mysql.go @@ -35,7 +35,7 @@ func makeCert(tlsPoolName string, config DatabaseConfig) (*tls.Config, error) { } // Return more meaningful error before it is too late if config.ServerCertName == "" && !tlsConfig.InsecureSkipVerify { - return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify.") + return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify") } return tlsConfig, nil } diff --git a/pkg/services/sqlstore/transactions_test.go b/pkg/services/sqlstore/transactions_test.go index 041359cf1d3..103eb46ef9c 100644 --- a/pkg/services/sqlstore/transactions_test.go +++ b/pkg/services/sqlstore/transactions_test.go @@ -5,12 +5,12 @@ import ( "errors" "testing" - "github.com/grafana/grafana/pkg/models" - . "github.com/smartystreets/goconvey/convey" + + "github.com/grafana/grafana/pkg/models" ) -var ProvokedError = errors.New("testing error.") +var ErrProvokedError = errors.New("testing error") func TestTransaction(t *testing.T) { ss := InitTestDB(t) @@ -42,10 +42,10 @@ func TestTransaction(t *testing.T) { return err } - return ProvokedError + return ErrProvokedError }) - So(err, ShouldEqual, ProvokedError) + So(err, ShouldEqual, ErrProvokedError) query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id} err = GetApiKeyById(query) diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 312877751c9..311081af4f8 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -2,12 +2,11 @@ package sqlstore import ( "context" + "fmt" "strconv" "strings" "time" - "fmt" - "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/events" m "github.com/grafana/grafana/pkg/models" @@ -48,16 +47,15 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error } if has { return org.Id, nil + } + if setting.AutoAssignOrgId == 1 { + org.Name = "Main Org." + org.Id = int64(setting.AutoAssignOrgId) } else { - if setting.AutoAssignOrgId == 1 { - org.Name = "Main Org." - org.Id = int64(setting.AutoAssignOrgId) - } else { - sqlog.Info("Could not create user: organization id %v does not exist", - setting.AutoAssignOrgId) - return 0, fmt.Errorf("Could not create user: organization id %v does not exist", - setting.AutoAssignOrgId) - } + sqlog.Info("Could not create user: organization id %v does not exist", + setting.AutoAssignOrgId) + return 0, fmt.Errorf("Could not create user: organization id %v does not exist", + setting.AutoAssignOrgId) } } else { org.Name = cmd.OrgName diff --git a/pkg/tsdb/influxdb/query.go b/pkg/tsdb/influxdb/query.go index f1a8c745956..169e8945aae 100644 --- a/pkg/tsdb/influxdb/query.go +++ b/pkg/tsdb/influxdb/query.go @@ -2,11 +2,10 @@ package influxdb import ( "fmt" + "regexp" "strconv" "strings" - "regexp" - "github.com/grafana/grafana/pkg/tsdb" ) @@ -160,7 +159,6 @@ func (query *Query) renderTz() string { tz := query.Tz if tz == "" { return "" - } else { - return fmt.Sprintf(" tz('%s')", tz) } + return fmt.Sprintf(" tz('%s')", tz) } diff --git a/pkg/tsdb/postgres/macros.go b/pkg/tsdb/postgres/macros.go index fbba582c262..2efba13d31a 100644 --- a/pkg/tsdb/postgres/macros.go +++ b/pkg/tsdb/postgres/macros.go @@ -108,9 +108,13 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, if m.timescaledb { return fmt.Sprintf("time_bucket('%vs',%s)", interval.Seconds(), args[0]), nil - } else { - return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil } + + return fmt.Sprintf( + "floor(extract(epoch from %s)/%v)*%v", args[0], + interval.Seconds(), + interval.Seconds(), + ), nil case "__timeGroupAlias": tg, err := m.evaluateMacro("__timeGroup", args) if err == nil { diff --git a/scripts/circle-test-backend.sh b/scripts/circle-test-backend.sh index 7f1e67dd5bb..4f853ec55fd 100755 --- a/scripts/circle-test-backend.sh +++ b/scripts/circle-test-backend.sh @@ -10,9 +10,6 @@ function exit_if_fail { fi } -echo "running go fmt" -exit_if_fail test -z \"'$(gofmt -s -l ./pkg | tee /dev/stderr)'\" - echo "building backend with install to cache pkgs" exit_if_fail time go install ./pkg/cmd/grafana-server diff --git a/scripts/revive.toml b/scripts/revive.toml index 1c7e3b92a65..2d4410ee548 100644 --- a/scripts/revive.toml +++ b/scripts/revive.toml @@ -9,6 +9,9 @@ errorCode = 0 [rule.range] [rule.superfluous-else] [rule.modifies-parameter] +[rule.indent-error-flow] +[rule.error-strings] +[rule.error-naming] # This can be checked by other tools like megacheck [rule.unreachable-code] @@ -17,11 +20,6 @@ errorCode = 0 # [rule.unexported-return] # [rule.exported] # [rule.var-naming] -# [error-naming] # [rule.dot-imports] -# [blank-imports] # [rule.receiver-naming] -# [error-strings] -# [rule.if-return] -# [rule.indent-error-flow] # [rule.blank-imports]