Skip to content

Commit

Permalink
refactor: move off deprecated io/ioutil methods, use http method …
Browse files Browse the repository at this point in the history
…constants, and adjust lint comments (#160)

* chore: adjust `nolint` comments

* chore: disable `ireturn` linter because it's not bringing joy

* refactor: use `http.MethodGet` and `http.MethodPost`

* refactor: move off `io/ioutil` in favor of `os` methods
  • Loading branch information
G-Rath committed Oct 6, 2022
1 parent 038a968 commit e519b86
Show file tree
Hide file tree
Showing 19 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Expand Up @@ -18,6 +18,7 @@ linters:
- godot # comments are fine without full stops
- gomnd # not every number is magic
- wsl # disagree with, for now
- ireturn # disagree with, sort of
presets:
- bugs
- comment
Expand Down
4 changes: 2 additions & 2 deletions internal/configer/load_test.go
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
)

// nolint:unparam
//nolint:unparam
func newReporter(t *testing.T) (*reporter.Reporter, *bytes.Buffer, *bytes.Buffer) {
t.Helper()

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestLoad(t *testing.T) {
}

if isMissingStderrMessage {
// nolint:forbidigo
//nolint:forbidigo
fmt.Println(gotStderr)
}

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
@@ -1,4 +1,4 @@
// nolint:testpackage // main cannot be accessed directly, so cannot use main_test
//nolint:testpackage // main cannot be accessed directly, so cannot use main_test
package main

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/api-check.go
Expand Up @@ -64,7 +64,7 @@ func (db APIDB) checkBatch(pkgs []internal.PackageDetails) ([][]ObjectWithID, er

req, err := http.NewRequestWithContext(
context.Background(),
"POST",
http.MethodPost,
db.bulkEndpoint(),
bytes.NewBuffer(jsonData),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/api-check_test.go
Expand Up @@ -51,7 +51,7 @@ func jsonMarshalQueryBatchResponse(t *testing.T, vulns []objectsWithIDs) []byte
func expectRequestPayload(t *testing.T, r *http.Request, queries []apiQuery) {
t.Helper()

if r.Method != "POST" {
if r.Method != http.MethodPost {
t.Fatalf("api query was not a POST request")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/database/api-fetch.go
Expand Up @@ -23,7 +23,7 @@ func (db APIDB) Fetch(id string) (OSV, error) {

req, err := http.NewRequestWithContext(
context.Background(),
"GET",
http.MethodGet,
db.osvEndpoint(id),
http.NoBody,
)
Expand Down
1 change: 0 additions & 1 deletion pkg/database/config.go
Expand Up @@ -27,7 +27,6 @@ func (dbc Config) Identifier() string {
var ErrUnsupportedDatabaseType = errors.New("unsupported database source type")

// Load initializes a new OSV database based on the given Config
// nolint:ireturn
func Load(config Config, offline bool, batchSize int) (DB, error) {
switch config.Type {
case "zip":
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/zip.go
Expand Up @@ -68,7 +68,7 @@ func (db *ZipDB) fetchZip() ([]byte, error) {
return cache.Body, nil
}

req, err := http.NewRequestWithContext(context.Background(), "GET", db.ArchiveURL, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, db.ArchiveURL, nil)

if err != nil {
return nil, fmt.Errorf("could not retrieve OSV database archive: %w", err)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (db *ZipDB) fetchZip() ([]byte, error) {
cacheContents, err := json.Marshal(cache)

if err == nil {
// nolint:gosec // being world readable is fine
//nolint:gosec // being world readable is fine
err = os.WriteFile(cachePath, cacheContents, 0644)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/zip_test.go
Expand Up @@ -55,7 +55,7 @@ func cacheWrite(t *testing.T, cache database.Cache) {
cacheContents, err := json.Marshal(cache)

if err == nil {
// nolint:gosec // being world readable is fine
//nolint:gosec // being world readable is fine
err = os.WriteFile(cachePath(cache.URL), cacheContents, 0644)
}

Expand All @@ -67,7 +67,7 @@ func cacheWrite(t *testing.T, cache database.Cache) {
func cacheWriteBad(t *testing.T, url string, contents string) {
t.Helper()

// nolint:gosec // being world readable is fine
//nolint:gosec // being world readable is fine
err := os.WriteFile(cachePath(url), []byte(contents), 0644)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/ecosystems_test.go
Expand Up @@ -2,15 +2,15 @@ package lockfile_test

import (
"github.com/g-rath/osv-detector/pkg/lockfile"
"io/ioutil"
"os"
"strings"
"testing"
)

func numberOfLockfileParsers(t *testing.T) int {
t.Helper()

directories, err := ioutil.ReadDir(".")
directories, err := os.ReadDir(".")

if err != nil {
t.Fatalf("unable to read current directory: ")
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-cargo-lock.go
Expand Up @@ -3,7 +3,7 @@ package lockfile
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
)

type CargoLockPackage struct {
Expand All @@ -21,7 +21,7 @@ const CargoEcosystem Ecosystem = "crates.io"
func ParseCargoLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *CargoLockFile

lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-composer-lock.go
Expand Up @@ -3,7 +3,7 @@ package lockfile
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

type ComposerPackage struct {
Expand All @@ -24,7 +24,7 @@ const ComposerEcosystem Ecosystem = "Packagist"
func ParseComposerLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *ComposerLock

lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-gemfile-lock.go
Expand Up @@ -2,8 +2,8 @@ package lockfile

import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (parser *gemfileLockfileParser) parse(contents string) {
func ParseGemfileLock(pathToLockfile string) ([]PackageDetails, error) {
var parser gemfileLockfileParser

bytes, err := ioutil.ReadFile(pathToLockfile)
bytes, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-go-lock.go
Expand Up @@ -3,14 +3,14 @@ package lockfile
import (
"fmt"
"golang.org/x/mod/modfile"
"io/ioutil"
"os"
"strings"
)

const GoEcosystem Ecosystem = "Go"

func ParseGoLock(pathToLockfile string) ([]PackageDetails, error) {
lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/lockfile/parse-maven-lock.go
Expand Up @@ -3,7 +3,6 @@ package lockfile
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"regexp"
)
Expand Down Expand Up @@ -96,7 +95,7 @@ func (p *MavenLockProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
func ParseMavenLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *MavenLockFile

lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-npm-lock.go
Expand Up @@ -3,7 +3,7 @@ package lockfile
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
Expand Down Expand Up @@ -143,7 +143,7 @@ func parseNpmLock(lockfile NpmLockfile) map[string]PackageDetails {
func ParseNpmLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *NpmLockfile

lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse-pnpm-lock.go
Expand Up @@ -3,7 +3,7 @@ package lockfile
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -116,7 +116,7 @@ func parsePnpmLock(lockfile PnpmLockfile) []PackageDetails {
func ParsePnpmLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *PnpmLockfile

lockfileContents, err := ioutil.ReadFile(pathToLockfile)
lockfileContents, err := os.ReadFile(pathToLockfile)

if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/lockfile/parse.go
Expand Up @@ -16,7 +16,7 @@ func FindParser(pathToLockfile string, parseAs string) (PackageDetailsParser, st
return parsers[parseAs], parseAs
}

// nolint:gochecknoglobals // this is an optimisation and read-only
//nolint:gochecknoglobals // this is an optimisation and read-only
var parsers = map[string]PackageDetailsParser{
"Cargo.lock": ParseCargoLock,
"composer.lock": ParseComposerLock,
Expand Down
4 changes: 2 additions & 2 deletions pkg/lockfile/parse_test.go
Expand Up @@ -3,7 +3,7 @@ package lockfile_test
import (
"errors"
"github.com/g-rath/osv-detector/pkg/lockfile"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
Expand All @@ -12,7 +12,7 @@ import (
func expectNumberOfParsersCalled(t *testing.T, numberOfParsersCalled int) {
t.Helper()

directories, err := ioutil.ReadDir(".")
directories, err := os.ReadDir(".")

if err != nil {
t.Fatalf("unable to read current directory: ")
Expand Down

0 comments on commit e519b86

Please sign in to comment.