Skip to content

Commit

Permalink
feat: update golangci-lint to 1.61.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmcgee committed Oct 1, 2024
1 parent 93cb24a commit 19ba216
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.53.3
version: v1.61.0

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ linters:
- unparam
- wastedassign
- whitespace
- wsl
- wsl
3 changes: 3 additions & 0 deletions pkg/derivation/derivation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestWriter(t *testing.T) {
}

var sb strings.Builder

err = drv.WriteDerivation(&sb)
if err != nil {
panic(err)
Expand Down Expand Up @@ -312,9 +313,11 @@ func TestValidate(t *testing.T) {
// the first input derivation, and re-insert it with an empty key.
k := "/nix/store/073gancjdr3z1scm2p553v0k3cxj2cpy-fix-tests-when-building-without-regex-supports.patch.drv"
firstInputDrv, ok := drv.InputDerivations[k]

if !ok {
panic("missing key")
}

delete(drv.InputDerivations, k)
drv.InputDerivations[""] = firstInputDrv

Expand Down
7 changes: 7 additions & 0 deletions pkg/derivation/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ func (d *Derivation) writeDerivation(
outputNames := make([]string, len(d.Outputs))
{
i := 0

for k := range d.Outputs {
outputNames[i] = k
i++
}

sort.Strings(outputNames)
}

Expand All @@ -129,6 +131,7 @@ func (d *Derivation) writeDerivation(
if !ok {
return fmt.Errorf("unable to find replacement for %s, but replacement requested", replacement)
}

inputDerivations[replacement] = outputNames
}
}
Expand All @@ -138,10 +141,12 @@ func (d *Derivation) writeDerivation(
inputDerivationPaths := make([]string, len(inputDerivations))
{
i := 0

for inputDerivationPath := range inputDerivations {
inputDerivationPaths[i] = inputDerivationPath
i++
}

sort.Strings(inputDerivationPaths)
}

Expand All @@ -150,10 +155,12 @@ func (d *Derivation) writeDerivation(
envKeys := make([]string, len(d.Env))
{
i := 0

for k := range d.Env {
envKeys[i] = k
i++
}

sort.Strings(envKeys)
}

Expand Down
10 changes: 1 addition & 9 deletions pkg/derivation/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package derivation

import (
"reflect"
"unsafe"
)

Expand All @@ -16,12 +15,5 @@ func unsafeString(b []byte) string {
// It's safe to use in situations like hash calculations or
// writing into buffers.
func unsafeBytes(s string) []byte {
return unsafe.Slice(
(*byte)(
unsafe.Pointer(
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data,
),
),
len(s),
)
return unsafe.Slice(unsafe.StringData(s), len(s))
}
22 changes: 16 additions & 6 deletions pkg/derivation/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
// keep track of the previous path read (if any), so we detect
// invalid encodings.
prevOutputName := ""
err = arrayEach(value, func(value []byte, index int) error {
err = arrayEach(value, func(value []byte, _ int) error {
output := &Output{}
outputName := ""

// Get every output field
err := arrayEach(value, func(value []byte, index int) error {
var err error

switch index {
case 0:
outputName, err = unquoteSlice(value)
if err != nil {
return err
}

if outputName <= prevOutputName {
return fmt.Errorf("invalid output order, %s <= %s", outputName, prevOutputName)
}
Expand Down Expand Up @@ -114,6 +116,7 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
if outputName == "" {
return fmt.Errorf("output name for %s may not be empty", output.Path)
}

drv.Outputs[outputName] = output
prevOutputName = outputName

Expand All @@ -124,28 +127,31 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
drv.InputDerivations = make(map[string][]string)
// InputDerivations are always lexicographically sorted by their path
prevInputDrvPath := ""
err = arrayEach(value, func(value []byte, index int) error {
err = arrayEach(value, func(value []byte, _ int) error {
inputDrvPath := ""
inputDrvNames := []string{}

err := arrayEach(value, func(value []byte, index int) error {
var err error

switch index {
case 0:
inputDrvPath, err = unquoteSlice(value)
if err != nil {
return err
}

if inputDrvPath <= prevInputDrvPath {
return fmt.Errorf("invalid input derivation order: %s <= %s", inputDrvPath, prevInputDrvPath)
}

case 1:
err := arrayEach(value, func(value []byte, index int) error {
err := arrayEach(value, func(value []byte, _ int) error {
unquoted, err := unquoteSlice(value)
if err != nil {
return err
}

inputDrvNames = append(inputDrvNames, unquoted)

return nil
Expand All @@ -171,11 +177,12 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
})

case 2: // InputSources
err = arrayEach(value, func(value []byte, index int) error {
err = arrayEach(value, func(value []byte, _ int) error {
unquoted, err := unquoteSlice(value)
if err != nil {
return err
}

drv.InputSources = append(drv.InputSources, unquoted)

return nil
Expand All @@ -188,11 +195,12 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
drv.Builder, err = unquoteSlice(value)

case 5: // Arguments
err = arrayEach(value, func(value []byte, index int) error {
err = arrayEach(value, func(value []byte, _ int) error {
unquoted, err := unquoteSlice(value)
if err != nil {
return err
}

drv.Arguments = append(drv.Arguments, unquoted)

return nil
Expand All @@ -201,19 +209,21 @@ func parseDerivation(derivationBytes []byte) (*Derivation, error) {
case 6: // Env
drv.Env = make(map[string]string)
prevEnvKey := ""
err = arrayEach(value, func(value []byte, index int) error {
err = arrayEach(value, func(value []byte, _ int) error {
envValue := ""
envKey := ""

// For every field
err := arrayEach(value, func(value []byte, index int) error {
var err error

switch index {
case 0:
envKey, err = unquoteSlice(value)
if err != nil {
return err
}

if envKey <= prevEnvKey {
return fmt.Errorf("invalid env var order: %s <= %s", envKey, prevEnvKey)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/derivation/store/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (bs *BadgerStore) Put(ctx context.Context, drv *derivation.Derivation) (str
err = bs.db.Update(func(txn *badger.Txn) error {
// store derivation itself
drvEntry := badger.NewEntry([]byte("drv:"+drvPath), buf.Bytes())

err := txn.SetEntry(drvEntry)
if err != nil {
return fmt.Errorf("unable to store derivation: %w", err)
Expand All @@ -120,15 +121,14 @@ func (bs *BadgerStore) Put(ctx context.Context, drv *derivation.Derivation) (str

// Store replacement string
replacementEntry := badger.NewEntry([]byte("replacement:"+drvPath), []byte(drvReplacement))
err = txn.SetEntry(replacementEntry)

err = txn.SetEntry(replacementEntry)
if err != nil {
return fmt.Errorf("unable to store replacement string: %w", err)
}

return nil
})

if err != nil {
return "", err
}
Expand Down Expand Up @@ -175,6 +175,7 @@ func (bs *BadgerStore) Has(_ context.Context, derivationPath string) (bool, erro
err := bs.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false

it := txn.NewIterator(opts)
defer it.Close()

Expand All @@ -183,6 +184,7 @@ func (bs *BadgerStore) Has(_ context.Context, derivationPath string) (bool, erro
for it.Seek(key); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()

if bytes.Equal(k, key) {
found = true

Expand Down
3 changes: 2 additions & 1 deletion pkg/derivation/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var stores = []struct {
}{
{
Title: "MemoryStore",
NewStore: func(tmpDir string) derivation.Store {
NewStore: func(_ string) derivation.Store {
return store.NewMapStore()
},
}, {
Expand Down Expand Up @@ -120,6 +120,7 @@ func TestStores(t *testing.T) {
if err != nil {
panic(err)
}

assert.Equal(t, spExpected.Absolute(), drvPath)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hash/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var hashtypeToNixHashString = map[int]string{

// Multihash returns the digest, in multihash format.
func (h *Hash) Multihash() []byte {
d, _ := multihash.Encode(h.Digest(), uint64(h.HashType))
d, _ := multihash.Encode(h.Digest(), uint64(h.HashType)) //nolint:gosec
// "The error return is legacy; it is always nil."
return d
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestDigest(t *testing.T) {
t.Run("valid sha256", func(t *testing.T) {
nixString := "sha256:1rjs6c23nyf8zkmf7yxglz2q2m7v5kp51nc2m0lk4h998d0qiixs"
sriString := "sha256-useIQUMpQTIpqILZUO4s+1SBxaev++Pq/Mh5OwQzWuY="

h, err := hash.ParseNixBase32(nixString)
if assert.NoError(t, err) {
assert.Equal(t, mh.SHA2_256, h.HashType)
Expand All @@ -32,6 +33,7 @@ func TestDigest(t *testing.T) {
t.Run("valid sha512", func(t *testing.T) {
nixString := "sha512:37iwwa5iw4m6pkd6qs2c5lw13q7y16hw2rv4i1cx6jax6yibhn6fgajbwc8p4j1fc6iicpy5r1vi7hpfq3n6z1ikhm5kcyz2b1frk80" //nolint:lll
sriString := "sha512-AM3swhLfs1kqnDF8Ywd2F564Qy7+shgNc0GSixhfUj1nLFzRm66k6SxEsrPg0AR/8AicFiY0Nm1eUwmPRXEezw=="

h, err := hash.ParseNixBase32(nixString)
if assert.NoError(t, err) {
assert.Equal(t, mh.SHA2_512, h.HashType)
Expand Down
4 changes: 2 additions & 2 deletions pkg/hash/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func New(hashType int) (*Hash, error) {
h, err := mh.GetHasher(uint64(hashType))
h, err := mh.GetHasher(uint64(hashType)) //nolint:gosec
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -38,7 +38,7 @@ func (h *Hash) Write(p []byte) (n int, err error) {
return 0, fmt.Errorf("unable to write to hash function: %w", err)
}

h.bytesWritten += uint64(n)
h.bytesWritten += uint64(n) //nolint:gosec

return n, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/hash/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestWriter(t *testing.T) {
if err != nil {
panic(err)
}

assert.Equal(t, expectedDigest, h.Multihash())
})

Expand Down Expand Up @@ -62,6 +63,7 @@ func TestWriter(t *testing.T) {
t.Run("reset", func(t *testing.T) {
h.Reset()
assert.Equal(t, uint64(0), h.BytesWritten())

if assert.NoError(t, err, "calling sum shouldn't error") {
assert.Equal(t, sha256DgstEmpty, h.Digest())
// calculate multihash
Expand Down
4 changes: 3 additions & 1 deletion pkg/nar/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestDumpPathOneByteRegular(t *testing.T) {
if runtime.GOOS == "windows" {
return
}

tmpDir := t.TempDir()
p := filepath.Join(tmpDir, "a")

Expand All @@ -70,6 +71,7 @@ func TestDumpPathOneByteRegular(t *testing.T) {
if err != nil {
panic(err)
}

assert.True(t, hdr.Executable, "regular should be executable")
}
})
Expand Down Expand Up @@ -174,7 +176,7 @@ func TestDumpPathFilter(t *testing.T) {

var buf bytes.Buffer

err = nar.DumpPathFilter(&buf, tmpDir, func(name string, nodeType nar.NodeType) bool {
err = nar.DumpPathFilter(&buf, tmpDir, func(name string, _ nar.NodeType) bool {
return name != p
})
if assert.NoError(t, err) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/nar/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (nw *Writer) emitNode(currentHeader *Header) (*Header, error) {
return nil, err
}

nw.contentWriter, err = wire.NewBytesWriter(nw.w, uint64(currentHeader.Size))
nw.contentWriter, err = wire.NewBytesWriter(nw.w, uint64(currentHeader.Size)) //nolint:gosec
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/narinfo/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (n *NarInfo) Check() error {

for i, r := range n.References {
_, err = storepath.FromString(r)

if err != nil {
return fmt.Errorf("invalid Reference[%d]: %v", i, r)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/narinfo/narinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func BenchmarkNarInfo(b *testing.B) {
defer f.Close()

var buf bytes.Buffer

_, err = io.ReadAll(&buf)
if err != nil {
panic(err)
Expand Down
1 change: 0 additions & 1 deletion pkg/narinfo/signature/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func decode(s string, dataSize int) (name string, data []byte, err error) {
}

data, err = base64.StdEncoding.DecodeString(dataStr)

if err != nil {
return "", nil, fmt.Errorf("data is corrupt: %w", err)
}
Expand Down
Loading

0 comments on commit 19ba216

Please sign in to comment.