Skip to content

Commit

Permalink
build: Use Go 1.17 for golangci linting and update golangci/golangci-…
Browse files Browse the repository at this point in the history
…lint-action (#364)

* Use Go 1.17 for golangci linting and update golangci/golangci-lint-action

Go 1.18 is not supported by golangci-lint yet since generics are not
supported.

Signed-off-by: Ethan Lowman <[email protected]>

* Try to make the linter happy

Signed-off-by: Ethan Lowman <[email protected]>

* Fix go version

Signed-off-by: Ethan Lowman <[email protected]>

* Remove usage of deprecated io/ioutil

Signed-off-by: Ethan Lowman <[email protected]>

* Appease gofmt for 1.19

Signed-off-by: Ethan Lowman <[email protected]>

* Remove one more ioutil instance

Signed-off-by: Ethan Lowman <[email protected]>

* Remove decodeRoot

Signed-off-by: Ethan Lowman <[email protected]>

* Remove more unused functions

Signed-off-by: Ethan Lowman <[email protected]>

Signed-off-by: Ethan Lowman <[email protected]>
  • Loading branch information
ethan-lowman-dd authored Sep 6, 2022
1 parent 64ded18 commit 06ed599
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ jobs:
needs: get-go-versions
steps:
- uses: actions/setup-go@268d8c0ca0432bb2cf416faae41297df9d262d7f
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@537aa1903e5d359d0b27dbc19ddd22c5087f3fbc
with:
version: v1.45.2
version: v1.49.0
args: --timeout 3m
8 changes: 6 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
run:
# Lint using Go 1.17, since some linters are disabled by default for Go 1.18
# until generics are supported.
# See https://github.com/golangci/golangci-lint/issues/2649
go: '1.17'

linters:
disable-all: true
enable:
- staticcheck
- gofmt
- govet
- gosimple
- structcheck
- varcheck
- unused
- typecheck
56 changes: 7 additions & 49 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"

"github.com/theupdateframework/go-tuf/data"
"github.com/theupdateframework/go-tuf/util"
Expand Down Expand Up @@ -551,7 +550,7 @@ func (c *Client) downloadMetaUnsafe(name string, maxMetaSize int64) ([]byte, err
// although the size has been checked above, use a LimitReader in case
// the reported size is inaccurate, or size is -1 which indicates an
// unknown length
return ioutil.ReadAll(io.LimitReader(r, maxMetaSize))
return io.ReadAll(io.LimitReader(r, maxMetaSize))
}

// remoteGetFunc is the type of function the download method uses to download
Expand Down Expand Up @@ -622,7 +621,7 @@ func (c *Client) downloadMeta(name string, version int64, m data.FileMeta) ([]by
stream = r
}

return ioutil.ReadAll(stream)
return io.ReadAll(stream)
}

func (c *Client) downloadMetaFromSnapshot(name string, m data.SnapshotFileMeta) ([]byte, error) {
Expand Down Expand Up @@ -673,17 +672,6 @@ func (c *Client) downloadMetaFromTimestamp(name string, m data.TimestampFileMeta
return b, nil
}

// decodeRoot decodes and verifies root metadata.
func (c *Client) decodeRoot(b json.RawMessage) error {
root := &data.Root{}
if err := c.db.Unmarshal(b, root, "root", c.rootVer); err != nil {
return ErrDecodeFailed{"root.json", err}
}
c.rootVer = root.Version
c.consistentSnapshot = root.ConsistentSnapshot
return nil
}

// decodeSnapshot decodes and verifies snapshot metadata, and returns the new
// root and targets file meta.
func (c *Client) decodeSnapshot(b json.RawMessage) (data.SnapshotFiles, error) {
Expand Down Expand Up @@ -790,36 +778,6 @@ func (c *Client) localMetaFromSnapshot(name string, m data.SnapshotFileMeta) (js
return b, err == nil
}

// hasTargetsMeta checks whether local metadata has the given snapshot meta
//lint:ignore U1000 unused
func (c *Client) hasTargetsMeta(m data.SnapshotFileMeta) bool {
b, ok := c.localMeta["targets.json"]
if !ok {
return false
}
meta, err := util.GenerateSnapshotFileMeta(bytes.NewReader(b), m.Hashes.HashAlgorithms()...)
if err != nil {
return false
}
err = util.SnapshotFileMetaEqual(meta, m)
return err == nil
}

// hasSnapshotMeta checks whether local metadata has the given meta
//lint:ignore U1000 unused
func (c *Client) hasMetaFromTimestamp(name string, m data.TimestampFileMeta) bool {
b, ok := c.localMeta[name]
if !ok {
return false
}
meta, err := util.GenerateTimestampFileMeta(bytes.NewReader(b), m.Hashes.HashAlgorithms()...)
if err != nil {
return false
}
err = util.TimestampFileMetaEqual(meta, m)
return err == nil
}

type Destination interface {
io.Writer
Delete() error
Expand All @@ -829,11 +787,11 @@ type Destination interface {
//
// dest will be deleted and an error returned in the following situations:
//
// * The target does not exist in the local targets.json
// * Failed to fetch the chain of delegations accessible from local snapshot.json
// * The target does not exist in any targets
// * Metadata cannot be generated for the downloaded data
// * Generated metadata does not match local metadata for the given file
// - The target does not exist in the local targets.json
// - Failed to fetch the chain of delegations accessible from local snapshot.json
// - The target does not exist in any targets
// - Metadata cannot be generated for the downloaded data
// - Generated metadata does not match local metadata for the given file
func (c *Client) Download(name string, dest Destination) (err error) {
// delete dest if there is an error
defer func() {
Expand Down
5 changes: 2 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -400,7 +399,7 @@ func newClientWithMeta(baseDir string, relPath string, serverAddr string) (*Clie
c := NewClient(MemoryLocalStore(), remote)
for _, m := range []string{"root.json", "snapshot.json", "timestamp.json", "targets.json"} {
if _, err := os.Stat(initialStateDir + "/" + m); err == nil {
metadataJSON, err := ioutil.ReadFile(initialStateDir + "/" + m)
metadataJSON, err := os.ReadFile(initialStateDir + "/" + m)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1213,7 +1212,7 @@ func generateRepoFS(c *C, dir string, files map[string][]byte, consistentSnapsho
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
c.Assert(ioutil.WriteFile(path, data, 0644), IsNil)
c.Assert(os.WriteFile(path, data, 0644), IsNil)
c.Assert(repo.AddTarget(file, nil), IsNil)
}
c.Assert(repo.Snapshot(), IsNil)
Expand Down
8 changes: 4 additions & 4 deletions client/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -273,10 +273,10 @@ func initTestDelegationClient(t *testing.T, dirPrefix string) (*Client, func() e
assert.Nil(t, err)

c := NewClient(MemoryLocalStore(), remote)
rawFile, err := ioutil.ReadFile(initialStateDir + "/" + "root.json")
rawFile, err := os.ReadFile(initialStateDir + "/" + "root.json")
assert.Nil(t, err)
assert.Nil(t, c.Init(rawFile))
files, err := ioutil.ReadDir(initialStateDir)
files, err := os.ReadDir(initialStateDir)
assert.Nil(t, err)

// load local files
Expand All @@ -287,7 +287,7 @@ func initTestDelegationClient(t *testing.T, dirPrefix string) (*Client, func() e
name := f.Name()
// ignoring consistent snapshot when loading initial state
if len(strings.Split(name, ".")) == 1 && strings.HasSuffix(name, ".json") {
rawFile, err := ioutil.ReadFile(initialStateDir + "/" + name)
rawFile, err := os.ReadFile(initialStateDir + "/" + name)
assert.Nil(t, err)
assert.Nil(t, c.local.SetMeta(name, rawFile))
}
Expand Down
7 changes: 3 additions & 4 deletions client/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -33,7 +32,7 @@ func checkGoIdentity(c *C, consistentSnapshot bool) {
c.Assert(err, IsNil)
testDataDir := filepath.Join(cwd, "testdata")

tempDir, err := ioutil.TempDir("", "")
tempDir, err := os.MkdirTemp("", "")
c.Assert(err, IsNil)
defer os.RemoveAll(tempDir)

Expand All @@ -59,7 +58,7 @@ func computeHashes(c *C, dir string) map[string]string {
return nil
}

bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,7 +107,7 @@ func newTestCase(c *C, name string, consistentSnapshot bool, options *HTTPRemote
c.Assert(err, IsNil)
testDir := filepath.Join(cwd, "testdata", name, fmt.Sprintf("consistent-snapshot-%t", consistentSnapshot))

dirEntries, err := ioutil.ReadDir(testDir)
dirEntries, err := os.ReadDir(testDir)
c.Assert(err, IsNil)
c.Assert(dirEntries, Not(HasLen), 0)

Expand Down
11 changes: 5 additions & 6 deletions client/python_interop/python_interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -58,7 +57,7 @@ func (InteropSuite) TestGoClientPythonGenerated(c *C) {

// initiate a client with the root metadata
client := client.NewClient(client.MemoryLocalStore(), remote)
rootJSON, err := ioutil.ReadFile(filepath.Join(testDataDir, dir, "repository", "metadata", "1.root.json"))
rootJSON, err := os.ReadFile(filepath.Join(testDataDir, dir, "repository", "metadata", "1.root.json"))
c.Assert(err, IsNil)
c.Assert(client.Init(rootJSON), IsNil)

Expand Down Expand Up @@ -99,7 +98,7 @@ func generateRepoFS(c *C, dir string, files map[string][]byte, consistentSnapsho
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
c.Assert(ioutil.WriteFile(path, data, 0644), IsNil)
c.Assert(os.WriteFile(path, data, 0644), IsNil)
c.Assert(repo.AddTarget(file, nil), IsNil)
}
c.Assert(repo.Snapshot(), IsNil)
Expand Down Expand Up @@ -134,9 +133,9 @@ func (InteropSuite) TestPythonClientGoGenerated(c *C) {
prevDir := filepath.Join(clientDir, "tufrepo", "metadata", "previous")
c.Assert(os.MkdirAll(currDir, 0755), IsNil)
c.Assert(os.MkdirAll(prevDir, 0755), IsNil)
rootJSON, err := ioutil.ReadFile(filepath.Join(dir, "repository", "1.root.json"))
rootJSON, err := os.ReadFile(filepath.Join(dir, "repository", "1.root.json"))
c.Assert(err, IsNil)
c.Assert(ioutil.WriteFile(filepath.Join(currDir, "root.json"), rootJSON, 0644), IsNil)
c.Assert(os.WriteFile(filepath.Join(currDir, "root.json"), rootJSON, 0644), IsNil)

args := []string{
filepath.Join(cwd, "testdata", "python-tuf-v1.0.0", "client.py"),
Expand All @@ -155,7 +154,7 @@ func (InteropSuite) TestPythonClientGoGenerated(c *C) {

// check the target files got downloaded
for path, expected := range files {
actual, err := ioutil.ReadFile(filepath.Join(clientDir, "tuftargets", url.QueryEscape(path)))
actual, err := os.ReadFile(filepath.Join(clientDir, "tuftargets", url.QueryEscape(path)))
c.Assert(err, IsNil)
c.Assert(actual, DeepEquals, expected)
}
Expand Down
3 changes: 1 addition & 2 deletions client/testdata/go-tuf-transition-M3/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -64,7 +63,7 @@ func addTargets(repo *tuf.Repo, dir string, files map[string][]byte) {
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
assertNoError(os.MkdirAll(filepath.Dir(path), 0755))
assertNoError(ioutil.WriteFile(path, data, 0644))
assertNoError(os.WriteFile(path, data, 0644))
paths = append(paths, file)
}
assertNoError(repo.AddTargetsWithExpires(paths, nil, expirationDate))
Expand Down
3 changes: 1 addition & 2 deletions client/testdata/go-tuf-transition-M4/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -62,7 +61,7 @@ func addTargets(repo *tuf.Repo, dir string, files map[string][]byte) {
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
assertNoError(os.MkdirAll(filepath.Dir(path), 0755))
assertNoError(ioutil.WriteFile(path, data, 0644))
assertNoError(os.WriteFile(path, data, 0644))
paths = append(paths, file)
}
assertNoError(repo.AddTargetsWithExpires(paths, nil, expirationDate))
Expand Down
3 changes: 1 addition & 2 deletions client/testdata/go-tuf/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -97,7 +96,7 @@ func addTargets(repo *tuf.Repo, dir string, files map[string][]byte) {
for file, data := range files {
path := filepath.Join(dir, "staged", "targets", file)
assertNoError(os.MkdirAll(filepath.Dir(path), 0755))
assertNoError(ioutil.WriteFile(path, data, 0644))
assertNoError(os.WriteFile(path, data, 0644))
paths = append(paths, file)
}
assertNoError(repo.AddTargetsWithExpires(paths, nil, expirationDate))
Expand Down
4 changes: 2 additions & 2 deletions client/testdata/tools/gen-keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/theupdateframework/go-tuf/data"
Expand Down Expand Up @@ -40,7 +40,7 @@ func main() {
s, err := json.MarshalIndent(&roles, "", " ")
assertNoError(err)

ioutil.WriteFile("keys.json", []byte(s), 0644)
os.WriteFile("keys.json", []byte(s), 0644)
}

func assertNoError(err error) {
Expand Down
5 changes: 2 additions & 3 deletions client/testdata/tools/linkify-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -55,7 +54,7 @@ func linkifyDir(rootDir string) error {
}

func readStepDirs(rootDir string) ([]string, error) {
dirEntries, err := ioutil.ReadDir(rootDir)
dirEntries, err := os.ReadDir(rootDir)
if err != nil {
return []string{}, err
}
Expand All @@ -79,7 +78,7 @@ func computeHashes(dir string) map[string][32]byte {
return nil
}

bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/tuf-client/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"io/ioutil"
"os"

"github.com/flynn/go-docopt"
Expand Down Expand Up @@ -35,7 +34,7 @@ func cmdGet(args *docopt.Args, client *tuf.Client) error {
return err
}
target := util.NormalizeTarget(args.String["<target>"])
file, err := ioutil.TempFile("", "go-tuf")
file, err := os.CreateTemp("", "go-tuf")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -276,7 +275,7 @@ func (f *fileSystemStore) GetMeta() (map[string]json.RawMessage, error) {

meta := make(map[string]json.RawMessage)
for name, path := range metaPaths {
f, err := ioutil.ReadFile(path)
f, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 06ed599

Please sign in to comment.