Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
General simplification in go part (#1658)
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
ferhatelmas authored and groob committed Dec 4, 2017
1 parent ca5f63d commit 9e0912e
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 74 deletions.
7 changes: 2 additions & 5 deletions server/contexts/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 3 additions & 14 deletions server/datastore/inmem/inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions server/datastore/inmem/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
12 changes: 2 additions & 10 deletions server/datastore/mysql/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions server/datastore/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion server/service/endpoint_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions server/service/endpoint_packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion server/service/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server/service/http_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestLogin(t *testing.T) {
}

// test sessions
testUser, _ := users[tt.username]
testUser := users[tt.username]

params := loginRequest{
Username: tt.username,
Expand Down
2 changes: 1 addition & 1 deletion server/service/service_osquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 3 additions & 12 deletions server/service/service_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/service/validation_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions server/sso/session_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9e0912e

Please sign in to comment.