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
This commit is contained in:
ferhat elmas 2017-12-04 15:43:43 +01:00 committed by Victor Vrantchan
parent ca5f63dd4f
commit 9e0912e2f3
13 changed files with 27 additions and 74 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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
}

View File

@ -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

View File

@ -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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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")

View File

@ -80,7 +80,7 @@ func TestLogin(t *testing.T) {
}
// test sessions
testUser, _ := users[tt.username]
testUser := users[tt.username]
params := loginRequest{
Username: tt.username,

View File

@ -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
}

View File

@ -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.

View File

@ -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 {

View File

@ -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
}