Skip to content

Commit d27d3b4

Browse files
authored
Improve code quality based on linter hints (#563)
* all: gardening clean up Changes to remove magic constants, handle errors and clean up. Some changes requested by opinionated editor/linter. Makefile fixed to ensure that build directory exists before starting work. * address pr comments
1 parent 2c33147 commit d27d3b4

File tree

10 files changed

+32
-26
lines changed

10 files changed

+32
-26
lines changed

internal/fields/dependency_manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ func loadECSFieldsSchema(dep buildmanifest.ECSDependency) ([]FieldDefinition, er
8686
return nil, errors.Wrapf(err, "can't download the online schema (URL: %s)", url)
8787
}
8888
defer resp.Body.Close()
89-
if resp.StatusCode == 404 {
90-
return nil, fmt.Errorf("unsatisfied ECS dependency, reference defined in build manifest doesn't exist (HTTP 404, URL: %s)", url)
91-
} else if resp.StatusCode != 200 {
89+
if resp.StatusCode == http.StatusNotFound {
90+
return nil, fmt.Errorf("unsatisfied ECS dependency, reference defined in build manifest doesn't exist (HTTP StatusNotFound, URL: %s)", url)
91+
} else if resp.StatusCode != http.StatusOK {
9292
return nil, fmt.Errorf("unexpected HTTP status code: %d", resp.StatusCode)
9393
}
9494

internal/files/compress.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func Zip(sourcePath, destinationFile string) error {
3636
}
3737
defer os.RemoveAll(tempDir)
3838
workDir := filepath.Join(tempDir, folderNameFromFileName(destinationFile))
39-
os.MkdirAll(workDir, 0755)
39+
err = os.MkdirAll(workDir, 0755)
40+
if err != nil {
41+
return errors.Wrapf(err, "can't prepare work directory: %s", workDir)
42+
}
4043

4144
logger.Debugf("Create work directory for archiving: %s", workDir)
4245
err = CopyAll(sourcePath, workDir)

internal/kibana/agents.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kibana
77
import (
88
"encoding/json"
99
"fmt"
10+
"net/http"
1011
"time"
1112

1213
"github.com/pkg/errors"
@@ -45,8 +46,8 @@ func (c *Client) ListAgents() ([]Agent, error) {
4546
return nil, errors.Wrap(err, "could not list agents")
4647
}
4748

48-
if statusCode != 200 {
49-
return nil, fmt.Errorf("could not list agents; API status code = %d", statusCode)
49+
if statusCode != http.StatusOK {
50+
return nil, fmt.Errorf("could not list agents; API status code = %d; response body = %s", statusCode, respBody)
5051
}
5152

5253
var resp struct {
@@ -70,8 +71,8 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error {
7071
return errors.Wrap(err, "could not assign policy to agent")
7172
}
7273

73-
if statusCode != 200 {
74-
return fmt.Errorf("could not assign policy to agent; API status code = %d; response body = %s", statusCode, string(respBody))
74+
if statusCode != http.StatusOK {
75+
return fmt.Errorf("could not assign policy to agent; API status code = %d; response body = %s", statusCode, respBody)
7576
}
7677

7778
err = c.waitUntilPolicyAssigned(a, p)
@@ -115,8 +116,8 @@ func (c *Client) getAgent(agentID string) (*Agent, error) {
115116
return nil, errors.Wrap(err, "could not list agents")
116117
}
117118

118-
if statusCode != 200 {
119-
return nil, fmt.Errorf("could not list agents; API status code = %d", statusCode)
119+
if statusCode != http.StatusOK {
120+
return nil, fmt.Errorf("could not list agents; API status code = %d; response body = %s", statusCode, respBody)
120121
}
121122

122123
var resp struct {

internal/kibana/dashboards.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ func (c *Client) Export(dashboardIDs []string) ([]common.MapStr, error) {
3535
path := fmt.Sprintf("%s/dashboards/export%s", CoreAPI, query.String())
3636
statusCode, respBody, err := c.get(path)
3737
if err != nil {
38-
return nil, errors.Wrapf(err, "could not export dashboards; API status code = %d; response body = %s", statusCode, string(respBody))
38+
return nil, errors.Wrapf(err, "could not export dashboards; API status code = %d; response body = %s", statusCode, respBody)
3939
}
4040

4141
var exported exportedType
4242
err = json.Unmarshal(respBody, &exported)
4343
if err != nil {
44-
return nil, errors.Wrapf(err, "unmarshalling response failed (body: \n%s)", string(respBody))
44+
return nil, errors.Wrapf(err, "unmarshalling response failed (body: \n%s)", respBody)
4545
}
4646

4747
var multiErr multierror.Error

internal/kibana/packages.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kibana
77
import (
88
"encoding/json"
99
"fmt"
10+
"net/http"
1011

1112
"github.com/pkg/errors"
1213

@@ -38,7 +39,7 @@ func (c *Client) RemovePackage(pkg packages.PackageManifest) ([]packages.Asset,
3839
}
3940

4041
func processResults(action string, statusCode int, respBody []byte) ([]packages.Asset, error) {
41-
if statusCode != 200 {
42+
if statusCode != http.StatusOK {
4243
return nil, fmt.Errorf("could not %s package; API status code = %d; response body = %s", action, statusCode, respBody)
4344
}
4445

internal/kibana/policies.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kibana
77
import (
88
"encoding/json"
99
"fmt"
10+
"net/http"
1011

1112
"github.com/pkg/errors"
1213

@@ -34,7 +35,7 @@ func (c *Client) CreatePolicy(p Policy) (*Policy, error) {
3435
return nil, errors.Wrap(err, "could not create policy")
3536
}
3637

37-
if statusCode != 200 {
38+
if statusCode != http.StatusOK {
3839
return nil, fmt.Errorf("could not create policy; API status code = %d; response body = %s", statusCode, respBody)
3940
}
4041

@@ -56,7 +57,7 @@ func (c *Client) GetPolicy(policyID string) (*Policy, error) {
5657
return nil, errors.Wrap(err, "could not get policy")
5758
}
5859

59-
if statusCode != 200 {
60+
if statusCode != http.StatusOK {
6061
return nil, fmt.Errorf("could not get policy; API status code = %d; response body = %s", statusCode, respBody)
6162
}
6263

@@ -80,7 +81,7 @@ func (c *Client) DeletePolicy(p Policy) error {
8081
return errors.Wrap(err, "could not delete policy")
8182
}
8283

83-
if statusCode != 200 {
84+
if statusCode != http.StatusOK {
8485
return fmt.Errorf("could not delete policy; API status code = %d; response body = %s", statusCode, respBody)
8586
}
8687

@@ -150,7 +151,7 @@ func (c *Client) AddPackageDataStreamToPolicy(r PackageDataStream) error {
150151
return errors.Wrap(err, "could not add package to policy")
151152
}
152153

153-
if statusCode != 200 {
154+
if statusCode != http.StatusOK {
154155
return fmt.Errorf("could not add package to policy; API status code = %d; response body = %s", statusCode, respBody)
155156
}
156157

internal/kibana/saved_objects.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Client) FindDashboards() (DashboardSavedObjects, error) {
6060
logger.Debug("Find dashboards using the Saved Objects API")
6161

6262
var foundObjects DashboardSavedObjects
63-
var page = 1
63+
page := 1
6464

6565
for {
6666
r, err := c.findDashboardsNextPage(page)
@@ -94,7 +94,7 @@ func (c *Client) findDashboardsNextPage(page int) (*savedObjectsResponse, error)
9494
path := fmt.Sprintf("%s/_find?type=dashboard&fields=title&per_page=%d&page=%d", SavedObjectsAPI, findDashboardsPerPage, page)
9595
statusCode, respBody, err := c.get(path)
9696
if err != nil {
97-
return nil, errors.Wrapf(err, "could not find dashboards; API status code = %d; response body = %s", statusCode, string(respBody))
97+
return nil, errors.Wrapf(err, "could not find dashboards; API status code = %d; response body = %s", statusCode, respBody)
9898
}
9999

100100
var r savedObjectsResponse

internal/packages/packages.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ func (vv *VarValue) Unpack(value interface{}) error {
4343
switch u := value.(type) {
4444
case []interface{}:
4545
vv.list = u
46-
return nil
4746
default:
4847
vv.scalar = u
49-
return nil
5048
}
51-
return errors.New("unknown variable value")
49+
return nil
5250
}
5351

5452
// MarshalJSON knows how to serialize a VarValue into the appropriate

internal/registry/revisions.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package registry
77
import (
88
"encoding/json"
99
"fmt"
10+
"net/http"
1011
"sort"
1112

1213
"github.com/Masterminds/semver"
@@ -44,7 +45,7 @@ func (c *Client) Revisions(packageName string, options SearchOptions) ([]package
4445
if err != nil {
4546
return nil, errors.Wrap(err, "could not retrieve package")
4647
}
47-
if statusCode != 200 {
48+
if statusCode != http.StatusOK {
4849
return nil, fmt.Errorf("could not retrieve package; API status code = %d; response body = %s", statusCode, respBody)
4950
}
5051

internal/testrunner/runners/pipeline/ingest_pipeline.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"log"
13+
"net/http"
1314
"os"
1415
"path/filepath"
1516
"regexp"
@@ -169,7 +170,7 @@ func putIngestPipeline(esClient *elasticsearch.Client, pipeline pipelineResource
169170
return errors.Wrapf(err, "failed to read PutPipeline API response body (pipelineName: %s)", pipeline.name)
170171
}
171172

172-
if r.StatusCode != 200 {
173+
if r.StatusCode != http.StatusOK {
173174
return errors.Wrapf(es.NewError(body), "unexpected response status for PutPipeline (%d): %s (pipelineName: %s)",
174175
r.StatusCode, r.Status(), pipeline.name)
175176
}
@@ -190,7 +191,7 @@ func getIngestPipeline(esClient *elasticsearch.Client, pipelineName string) erro
190191
return errors.Wrapf(err, "failed to read GetPipeline API response body (pipelineName: %s)", pipelineName)
191192
}
192193

193-
if r.StatusCode != 200 {
194+
if r.StatusCode != http.StatusOK {
194195
return errors.Wrapf(es.NewError(body), "unexpected response status for GetPipeline (%d): %s (pipelineName: %s)",
195196
r.StatusCode, r.Status(), pipelineName)
196197
}
@@ -237,7 +238,7 @@ func simulatePipelineProcessing(esClient *elasticsearch.Client, pipelineName str
237238
return nil, errors.Wrap(err, "failed to read Simulate API response body")
238239
}
239240

240-
if r.StatusCode != 200 {
241+
if r.StatusCode != http.StatusOK {
241242
return nil, errors.Wrapf(es.NewError(body), "unexpected response status for Simulate (%d): %s", r.StatusCode, r.Status())
242243
}
243244

0 commit comments

Comments
 (0)