From 9e0912e2f3692235c60a755c7dedd9c66680986a Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Mon, 4 Dec 2017 15:43:43 +0100 Subject: [PATCH] General simplification in go part (#1658) * don't check if error is nil, return it * don't compare bool to bool, use it * don't supply capacity to make for slice when len is equal to cap --- server/contexts/viewer/viewer.go | 7 ++----- server/datastore/inmem/inmem.go | 17 +++-------------- server/datastore/inmem/packs.go | 19 ++++++++----------- server/datastore/mysql/hosts.go | 12 ++---------- server/datastore/mysql/mysql.go | 15 +++------------ server/service/endpoint_hosts.go | 2 +- server/service/endpoint_packs.go | 4 ++-- server/service/frontend.go | 1 - server/service/http_auth_test.go | 2 +- server/service/service_osquery.go | 2 +- server/service/service_users.go | 15 +++------------ server/service/validation_users.go | 2 +- server/sso/session_store.go | 3 --- 13 files changed, 27 insertions(+), 74 deletions(-) diff --git a/server/contexts/viewer/viewer.go b/server/contexts/viewer/viewer.go index 41a9affde..88deedbc3 100644 --- a/server/contexts/viewer/viewer.go +++ b/server/contexts/viewer/viewer.go @@ -104,13 +104,10 @@ func (v Viewer) CanPerformActions() bool { // IsUserID returns true if the given user id the same as the user which is // represented by this ViewerContext func (v Viewer) IsUserID(id uint) bool { - if v.UserID() == id { - return true - } - return false + return v.UserID() == id } -// CanPerformReadActionsOnUser returns a bool indicating the current user's +// CanPerformReadActionOnUser returns a bool indicating the current user's // ability to perform read actions on the given user func (v Viewer) CanPerformReadActionOnUser(uid uint) bool { if v.User != nil { diff --git a/server/datastore/inmem/inmem.go b/server/datastore/inmem/inmem.go index f9bef1cfa..def81f0d4 100644 --- a/server/datastore/inmem/inmem.go +++ b/server/datastore/inmem/inmem.go @@ -131,11 +131,7 @@ func (d *Datastore) MigrateData() error { SMTPVerifySSLCerts: true, } - if err := d.createBuiltinLabels(); err != nil { - return err - } - - return nil + return d.createBuiltinLabels() } func (m *Datastore) MigrationStatus() (kolide.MigrationStatus, error) { @@ -271,11 +267,7 @@ func (d *Datastore) createDevPacksAndQueries() error { PackID: pack2.ID, Interval: 60, }) - if err != nil { - return err - } - - return nil + return err } func (d *Datastore) createBuiltinLabels() error { @@ -489,10 +481,7 @@ func (d *Datastore) createDevOrgInfo() error { SMTPEnableStartTLS: true, } _, err := d.NewAppConfig(devOrgInfo) - if err != nil { - return err - } - return nil + return err } func (d *Datastore) createDevLabels() error { diff --git a/server/datastore/inmem/packs.go b/server/datastore/inmem/packs.go index f386f4671..aff924074 100644 --- a/server/datastore/inmem/packs.go +++ b/server/datastore/inmem/packs.go @@ -260,13 +260,7 @@ func (d *Datastore) ListHostsInPack(pid uint, opt kolide.ListOptions) ([]uint, e // Apply limit/offset low, high := d.getLimitOffsetSliceBounds(opt, len(hosts)) hosts = hosts[low:high] - - results := make([]uint, len(hosts), len(hosts)) - for _, h := range hosts { - results = append(results, h.ID) - } - - return results, nil + return extractHostIDs(hosts), nil } func (d *Datastore) ListExplicitHostsInPack(pid uint, opt kolide.ListOptions) ([]uint, error) { @@ -314,11 +308,14 @@ func (d *Datastore) ListExplicitHostsInPack(pid uint, opt kolide.ListOptions) ([ // Apply limit/offset low, high := d.getLimitOffsetSliceBounds(opt, len(hosts)) hosts = hosts[low:high] + return extractHostIDs(hosts), nil +} - results := make([]uint, len(hosts), len(hosts)) - for _, h := range hosts { - results = append(results, h.ID) +func extractHostIDs(hosts []*kolide.Host) []uint { + ids := make([]uint, len(hosts)) + for i, h := range hosts { + ids[i] = h.ID } - return results, nil + return ids } diff --git a/server/datastore/mysql/hosts.go b/server/datastore/mysql/hosts.go index 23292a569..fdcad79cc 100644 --- a/server/datastore/mysql/hosts.go +++ b/server/datastore/mysql/hosts.go @@ -62,11 +62,7 @@ func removedUnusedNics(tx *sqlx.Tx, host *kolide.Host) error { sql = tx.Rebind(sql) _, err = tx.Exec(sql, args...) - if err != nil { - return err - } - - return nil + return err } func updateNicsForHost(tx *sqlx.Tx, host *kolide.Host) ([]*kolide.NetworkInterface, error) { @@ -405,11 +401,7 @@ func (d *Datastore) getNetInterfacesForHost(host *kolide.Host) error { SELECT * FROM network_interfaces WHERE host_id = ? ` - if err := d.db.Select(&host.NetworkInterfaces, sqlStatement, host.ID); err != nil { - return err - } - - return nil + return d.db.Select(&host.NetworkInterfaces, sqlStatement, host.ID) } // EnrollHost enrolls a host diff --git a/server/datastore/mysql/mysql.go b/server/datastore/mysql/mysql.go index 88ec70b44..029102726 100644 --- a/server/datastore/mysql/mysql.go +++ b/server/datastore/mysql/mysql.go @@ -39,8 +39,7 @@ type dbfunctions interface { } func (d *Datastore) getTransaction(opts []kolide.OptionalArg) dbfunctions { - var result dbfunctions - result = d.db + var result dbfunctions = d.db for _, opt := range opts { switch t := opt().(type) { case dbfunctions: @@ -111,19 +110,11 @@ func (d *Datastore) Name() string { } func (d *Datastore) MigrateTables() error { - if err := tables.MigrationClient.Up(d.db.DB, ""); err != nil { - return err - } - - return nil + return tables.MigrationClient.Up(d.db.DB, "") } func (d *Datastore) MigrateData() error { - if err := data.MigrationClient.Up(d.db.DB, ""); err != nil { - return err - } - - return nil + return data.MigrationClient.Up(d.db.DB, "") } func (d *Datastore) MigrationStatus() (kolide.MigrationStatus, error) { diff --git a/server/service/endpoint_hosts.go b/server/service/endpoint_hosts.go index be08ae3e4..bf372497f 100644 --- a/server/service/endpoint_hosts.go +++ b/server/service/endpoint_hosts.go @@ -79,7 +79,7 @@ func makeListHostsEndpoint(svc kolide.Service) endpoint.Endpoint { return listHostsResponse{Err: err}, nil } - hostResponses := make([]hostResponse, len(hosts), len(hosts)) + hostResponses := make([]hostResponse, len(hosts)) for i, host := range hosts { h, err := hostResponseForHost(ctx, svc, host) if err != nil { diff --git a/server/service/endpoint_packs.go b/server/service/endpoint_packs.go index 2bbef3b78..4e722239b 100644 --- a/server/service/endpoint_packs.go +++ b/server/service/endpoint_packs.go @@ -33,7 +33,7 @@ func packResponseForPack(ctx context.Context, svc kolide.Service, pack kolide.Pa } labels, err := svc.ListLabelsForPack(ctx, pack.ID) - labelIDs := make([]uint, len(labels), len(labels)) + labelIDs := make([]uint, len(labels)) for i, label := range labels { labelIDs[i] = label.ID } @@ -113,7 +113,7 @@ func makeListPacksEndpoint(svc kolide.Service) endpoint.Endpoint { return getPackResponse{Err: err}, nil } - resp := listPacksResponse{Packs: make([]packResponse, len(packs), len(packs))} + resp := listPacksResponse{Packs: make([]packResponse, len(packs))} for i, pack := range packs { packResp, err := packResponseForPack(ctx, svc, *pack) if err != nil { diff --git a/server/service/frontend.go b/server/service/frontend.go index f9f3e00ee..0802b2e04 100644 --- a/server/service/frontend.go +++ b/server/service/frontend.go @@ -22,7 +22,6 @@ func ServeFrontend(logger log.Logger) http.Handler { herr := func(w http.ResponseWriter, err string) { logger.Log("err", err) http.Error(w, err, http.StatusInternalServerError) - return } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fs := newBinaryFileSystem("/frontend") diff --git a/server/service/http_auth_test.go b/server/service/http_auth_test.go index e83e4a7c6..fc7f5916f 100644 --- a/server/service/http_auth_test.go +++ b/server/service/http_auth_test.go @@ -80,7 +80,7 @@ func TestLogin(t *testing.T) { } // test sessions - testUser, _ := users[tt.username] + testUser := users[tt.username] params := loginRequest{ Username: tt.username, diff --git a/server/service/service_osquery.go b/server/service/service_osquery.go index dfdce1ee8..ca5069753 100644 --- a/server/service/service_osquery.go +++ b/server/service/service_osquery.go @@ -170,7 +170,7 @@ func (svc service) GetClientConfig(ctx context.Context) (*kolide.OsqueryConfig, Version: query.Version, } - if query.Snapshot != nil && *query.Snapshot == true { + if query.Snapshot != nil && *query.Snapshot { queryContent.Snapshot = query.Snapshot } diff --git a/server/service/service_users.go b/server/service/service_users.go index 7b8abcf69..805c18d13 100644 --- a/server/service/service_users.go +++ b/server/service/service_users.go @@ -40,7 +40,7 @@ func (svc service) NewAdminCreatedUser(ctx context.Context, p kolide.UserPayload func (svc service) newUser(p kolide.UserPayload) (*kolide.User, error) { var ssoEnabled bool // if user is SSO generate a fake password - if p.SSOInvite != nil && *p.SSOInvite == true { + if p.SSOInvite != nil && *p.SSOInvite { fakePassword, err := generateRandomText(14) if err != nil { return nil, err @@ -165,11 +165,7 @@ func (svc service) modifyEmailAddress(ctx context.Context, user *kolide.User, em KolideServerURL: template.URL(config.KolideServerURL), }, } - err = svc.mailService.SendEmail(changeEmail) - if err != nil { - return err - } - return nil + return svc.mailService.SendEmail(changeEmail) } func (svc service) ChangeUserEmail(ctx context.Context, token string) (string, error) { @@ -371,12 +367,7 @@ func (svc service) RequestPasswordReset(ctx context.Context, email string) error }, } - err = svc.mailService.SendEmail(resetEmail) - if err != nil { - return err - } - - return nil + return svc.mailService.SendEmail(resetEmail) } // saves user in datastore. diff --git a/server/service/validation_users.go b/server/service/validation_users.go index 7438ad042..07efb4b23 100644 --- a/server/service/validation_users.go +++ b/server/service/validation_users.go @@ -25,7 +25,7 @@ func (mw validationMiddleware) NewUser(ctx context.Context, p kolide.UserPayload } // we don't need a password for single sign on - if p.SSOInvite == nil || *p.SSOInvite == false { + if p.SSOInvite == nil || !*p.SSOInvite { if p.Password == nil { invalid.Append("password", "missing required argument") } else { diff --git a/server/sso/session_store.go b/server/sso/session_store.go index 887c691bd..30f12a845 100644 --- a/server/sso/session_store.go +++ b/server/sso/session_store.go @@ -54,9 +54,6 @@ func (s *store) create(requestID, originalURL, metadata string, lifetimeSecs uin return err } _, err = conn.Do("SETEX", requestID, lifetimeSecs, writer.String()) - if err != nil { - return err - } return err }