Skip to content

Commit

Permalink
chore: rekres the source, fix issues
Browse files Browse the repository at this point in the history
The last build instructions were using broken way to install
golangci-lint.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed May 27, 2022
1 parent d9c3a27 commit fccee8b
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 89 deletions.
6 changes: 3 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:28:14Z by kres c4d092b.
# Generated on 2022-05-27T15:56:20Z by kres 65530e7.

kind: pipeline
type: kubernetes
Expand All @@ -19,7 +19,7 @@ steps:
- sleep 5
- git fetch --tags
- install-ci-key
- docker buildx create --driver docker-container --platform linux/amd64 --name local-insecure-v2 --buildkitd-flags '--allow-insecure-entitlement security.insecure' --driver-opt image=ghcr.io/smira/buildkit:cap-fix --use unix:///var/outer-run/docker.sock
- docker buildx create --driver docker-container --platform linux/amd64 --name local-insecure --buildkitd-flags '--allow-insecure-entitlement security.insecure' --use unix:///var/outer-run/docker.sock
- docker buildx inspect --bootstrap
environment:
SSH_KEY:
Expand Down Expand Up @@ -174,7 +174,7 @@ steps:

services:
- name: docker
image: ghcr.io/smira/docker:20.10-dind-hacked
image: docker:20.10-dind
entrypoint:
- dockerd
commands:
Expand Down
11 changes: 10 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:22:12Z by kres c4d092b.
# Generated on 2022-05-27T15:56:20Z by kres 65530e7.

# options for analysis running
run:
Expand Down Expand Up @@ -39,6 +39,13 @@ linters-settings:
local-prefixes: github.com/talos-systems/go-blockdevice
gocognit:
min-complexity: 30
ireturn:
allow:
- anon
- error
- empty
- stdlib
- github.com\/talos-systems\/kres\/internal\/dag.Node
nestif:
min-complexity: 5
goconst:
Expand Down Expand Up @@ -117,6 +124,7 @@ linters:
disable-all: false
fast: false
disable:
- exhaustruct
- exhaustivestruct
- forbidigo
- funlen
Expand All @@ -132,6 +140,7 @@ linters:
- tagliatelle
- thelper
- typecheck
- varnamelen
- wrapcheck
# abandoned linters for which golangci shows the warning that the repo is archived by the owner
- interfacer
Expand Down
3 changes: 3 additions & 0 deletions .license-header.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
31 changes: 20 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:22:12Z by kres c4d092b.
# Generated on 2022-05-27T15:56:20Z by kres 65530e7.

ARG TOOLCHAIN

# cleaned up specs and compiled versions
FROM scratch AS generate

# runs markdownlint
FROM node:14.8.0-alpine AS lint-markdown
RUN npm i -g [email protected]
RUN npm i [email protected]
FROM node:18.2.0-alpine AS lint-markdown
WORKDIR /src
RUN npm i -g [email protected]
RUN npm i [email protected]
COPY .markdownlint.json .
COPY ./README.md ./README.md
RUN markdownlint --ignore "CHANGELOG.md" --ignore "**/node_modules/**" --ignore '**/hack/chglog/**' --rules /node_modules/sentences-per-line/index.js .
RUN markdownlint --ignore "CHANGELOG.md" --ignore "**/node_modules/**" --ignore '**/hack/chglog/**' --rules node_modules/sentences-per-line/index.js .

# base toolchain image
FROM ${TOOLCHAIN} AS toolchain
Expand All @@ -27,10 +27,17 @@ FROM toolchain AS tools
ENV GO111MODULE on
ENV CGO_ENABLED 0
ENV GOPATH /go
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /bin v1.42.1
ARG GOLANGCILINT_VERSION
RUN curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/${GOLANGCILINT_VERSION}/install.sh | bash -s -- -b /bin ${GOLANGCILINT_VERSION}
ARG GOFUMPT_VERSION
RUN go install mvdan.cc/gofumpt/gofumports@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumports /bin/gofumports
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt /bin/gofumpt
ARG GOIMPORTS_VERSION
RUN go install golang.org/x/tools/cmd/goimports@${GOIMPORTS_VERSION} \
&& mv /go/bin/goimports /bin/goimports
ARG DEEPCOPY_VERSION
RUN go install github.com/siderolabs/deep-copy@${DEEPCOPY_VERSION} \
&& mv /go/bin/deep-copy /bin/deep-copy

# tools and sources
FROM tools AS base
Expand All @@ -44,9 +51,11 @@ RUN --mount=type=cache,target=/go/pkg go list -mod=readonly all >/dev/null

# runs gofumpt
FROM base AS lint-gofumpt
RUN find . -name '*.pb.go' | xargs -r rm
RUN find . -name '*.pb.gw.go' | xargs -r rm
RUN FILES="$(gofumports -l -local github.com/talos-systems/go-blockdevice .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumports -w -local github.com/talos-systems/go-blockdevice .':\n${FILES}"; exit 1)
RUN FILES="$(gofumpt -l .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumpt -w .':\n${FILES}"; exit 1)

# runs goimports
FROM base AS lint-goimports
RUN FILES="$(goimports -l -local github.com/talos-systems/go-blockdevice .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/talos-systems/go-blockdevice .':\n${FILES}"; exit 1)

# runs golangci-lint
FROM base AS lint-golangci-lint
Expand Down
37 changes: 26 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2022-03-28T13:15:04Z by kres 8bc4139-dirty.
# Generated on 2022-05-27T15:56:20Z by kres 65530e7.

# common variables

Expand All @@ -11,14 +11,18 @@ ARTIFACTS := _out
REGISTRY ?= ghcr.io
USERNAME ?= siderolabs
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
GOFUMPT_VERSION ?= abc0db2c416aca0f60ea33c23c76665f6e7ba0b6
GO_VERSION ?= 1.17
PROTOBUF_GO_VERSION ?= 1.27.1
GRPC_GO_VERSION ?= 1.1.0
GRPC_GATEWAY_VERSION ?= 2.4.0
VTPROTOBUF_VERSION ?= 81d623a9a700ede8ef765e5ab08b3aa1f5b4d5a8
GOLANGCILINT_VERSION ?= v1.46.2
GOFUMPT_VERSION ?= v0.3.1
GO_VERSION ?= 1.18
GOIMPORTS_VERSION ?= v0.1.10
PROTOBUF_GO_VERSION ?= 1.28.0
GRPC_GO_VERSION ?= 1.2.0
GRPC_GATEWAY_VERSION ?= 2.10.0
VTPROTOBUF_VERSION ?= 0.3.0
DEEPCOPY_VERSION ?= v0.5.5
TESTPKGS ?= ./...
KRES_IMAGE ?= ghcr.io/siderolabs/kres:latest
CONFORMANCE_IMAGE ?= ghcr.io/siderolabs/conform:latest

# docker build settings

Expand All @@ -36,13 +40,16 @@ COMMON_ARGS += --build-arg=SHA=$(SHA)
COMMON_ARGS += --build-arg=TAG=$(TAG)
COMMON_ARGS += --build-arg=USERNAME=$(USERNAME)
COMMON_ARGS += --build-arg=TOOLCHAIN=$(TOOLCHAIN)
COMMON_ARGS += --build-arg=GOLANGCILINT_VERSION=$(GOLANGCILINT_VERSION)
COMMON_ARGS += --build-arg=GOFUMPT_VERSION=$(GOFUMPT_VERSION)
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION=$(GOIMPORTS_VERSION)
COMMON_ARGS += --build-arg=PROTOBUF_GO_VERSION=$(PROTOBUF_GO_VERSION)
COMMON_ARGS += --build-arg=GRPC_GO_VERSION=$(GRPC_GO_VERSION)
COMMON_ARGS += --build-arg=GRPC_GATEWAY_VERSION=$(GRPC_GATEWAY_VERSION)
COMMON_ARGS += --build-arg=VTPROTOBUF_VERSION=$(VTPROTOBUF_VERSION)
COMMON_ARGS += --build-arg=DEEPCOPY_VERSION=$(DEEPCOPY_VERSION)
COMMON_ARGS += --build-arg=TESTPKGS=$(TESTPKGS)
TOOLCHAIN ?= docker.io/golang:1.17-alpine
TOOLCHAIN ?= docker.io/golang:1.18-alpine

# help menu

Expand Down Expand Up @@ -99,8 +106,11 @@ lint-gofumpt: ## Runs gofumpt linter.
fmt: ## Formats the source code
@docker run --rm -it -v $(PWD):/src -w /src golang:$(GO_VERSION) \
bash -c "export GO111MODULE=on; export GOPROXY=https://proxy.golang.org; \
go install mvdan.cc/gofumpt/gofumports@$(GOFUMPT_VERSION) && \
gofumports -w -local github.com/talos-systems/go-blockdevice ."
go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION) && \
gofumpt -w ."

lint-goimports: ## Runs goimports linter.
@$(MAKE) target-$@

.PHONY: base
base: ## Prepare base toolchain
Expand All @@ -123,7 +133,7 @@ lint-markdown: ## Runs markdownlint.
@$(MAKE) target-$@

.PHONY: lint
lint: lint-golangci-lint lint-gofumpt lint-markdown ## Run all linters for the project.
lint: lint-golangci-lint lint-gofumpt lint-goimports lint-markdown ## Run all linters for the project.

.PHONY: rekres
rekres:
Expand All @@ -140,3 +150,8 @@ release-notes:
mkdir -p $(ARTIFACTS)
@ARTIFACTS=$(ARTIFACTS) ./hack/release.sh $@ $(ARTIFACTS)/RELEASE_NOTES.md $(TAG)

.PHONY: conformance
conformance:
@docker pull $(CONFORMANCE_IMAGE)
@docker run --rm -it -v $(PWD):/src -w /src $(CONFORMANCE_IMAGE) enforce

3 changes: 2 additions & 1 deletion blockdevice/blkpg/blkpg_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ func InformKernelOfDelete(f *os.File, first, length uint64, n int32) error {
return inform(f, first, length, n, unix.BLKPG_DEL_PARTITION)
}

func inform(f *os.File, first, length uint64, n, op int32) (err error) {
func inform(f *os.File, first, length uint64, n, op int32) error {
var (
start int64
end int64
err error
)

switch op {
Expand Down
13 changes: 8 additions & 5 deletions blockdevice/blockdevice_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ type BlockDevice struct {

// Open initializes and returns a block device.
// TODO(andrewrynhard): Use BLKGETSIZE ioctl to get the size.
func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
func Open(devname string, setters ...Option) (_ *BlockDevice, rerr error) { //nolint:nonamedreturns
opts := NewDefaultOptions(setters...)
bd = &BlockDevice{}
bd := &BlockDevice{}

var f *os.File
var (
f *os.File
err error
)

if f, err = os.OpenFile(devname, opts.Mode, os.ModeDevice); err != nil {
return nil, err
Expand All @@ -60,7 +63,7 @@ func Open(devname string, setters ...Option) (bd *BlockDevice, err error) {
bd.f = f

defer func() {
if err != nil {
if rerr != nil {
//nolint: errcheck
f.Close()
}
Expand Down Expand Up @@ -131,7 +134,7 @@ func (bd *BlockDevice) RereadPartitionTable() error {
}
// Flush the block device buffers.
if _, _, ret := unix.Syscall(unix.SYS_IOCTL, bd.f.Fd(), unix.BLKFLSBUF, 0); ret != 0 {
return fmt.Errorf("flush block device buffers: %v", ret)
return fmt.Errorf("flush block device buffers: %w", ret)
}

var (
Expand Down
10 changes: 7 additions & 3 deletions blockdevice/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ const (
)

// Probe checks partition type.
func Probe(path string) (sb SuperBlocker, err error) {
var f *os.File
func Probe(path string) (SuperBlocker, error) { //nolint:ireturn
var (
f *os.File
err error
)

// Sleep for up to 5s to wait for kernel to create the necessary device files.
// If we dont sleep this becomes racy in that the device file does not exist
// and it will fail to open.
Expand Down Expand Up @@ -80,5 +84,5 @@ func Probe(path string) (sb SuperBlocker, err error) {
}
}

return nil, nil
return nil, nil //nolint:nilnil
}
19 changes: 12 additions & 7 deletions blockdevice/filesystem/vfat/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ func (fs *FileSystem) Open(path string) (*File, error) {

// fatChain results list of clusters for a file (or directory) based on first
// cluster number and FAT.
func (fs *FileSystem) fatChain(firstCluster uint32) (chain []uint32) {
chain = []uint32{firstCluster}
func (fs *FileSystem) fatChain(firstCluster uint32) []uint32 {
chain := []uint32{firstCluster}

for {
next := fs.fat[firstCluster]
if next == 0 || next&0xFFFFFF8 == 0xFFFFFF8 {
return
return chain
}

chain = append(chain, next)
Expand Down Expand Up @@ -216,14 +216,19 @@ type File struct {
size uint32
}

func (f *File) Read(p []byte) (n int, err error) {
func (f *File) Read(p []byte) (int, error) {
var (
n int
err error
)

remaining := len(p)
if uint32(remaining) > f.size-f.offset {
remaining = int(f.size - f.offset)
if remaining == 0 {
err = io.EOF

return
return n, err
}
}

Expand All @@ -234,7 +239,7 @@ func (f *File) Read(p []byte) (n int, err error) {
if clusterIdx > uint32(len(f.chain)) {
err = fmt.Errorf("FAT chain overrun")

return
return n, err
}

cluster := f.chain[clusterIdx]
Expand All @@ -245,7 +250,7 @@ func (f *File) Read(p []byte) (n int, err error) {
}

if err = readAtFull(f.fs.f, int64(f.fs.dataStart)+int64(cluster-2)*int64(f.fs.clusterSize)+int64(clusterOffset), p[:readLen]); err != nil {
return
return n, err
}

remaining -= int(readLen)
Expand Down
10 changes: 5 additions & 5 deletions blockdevice/lba/lba_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
)

// NewLBA initializes and returns an `LBA`.
func NewLBA(f *os.File) (lba *LBA, err error) {
func NewLBA(f *os.File) (*LBA, error) {
st, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("stat disk error: %w", err)
Expand Down Expand Up @@ -88,7 +88,7 @@ func NewLBA(f *os.File) (lba *LBA, err error) {

tsize := size / lsize

lba = &LBA{
lba := &LBA{
PhysicalBlockSize: psize,
LogicalBlockSize: lsize,
MinimalIOSize: minio,
Expand All @@ -101,8 +101,8 @@ func NewLBA(f *os.File) (lba *LBA, err error) {
}

// ReadAt reads from a file in units of LBA.
func (l *LBA) ReadAt(lba, off, length int64) (b []byte, err error) {
b = make([]byte, length)
func (l *LBA) ReadAt(lba, off, length int64) ([]byte, error) {
b := make([]byte, length)

off = lba*l.LogicalBlockSize + off

Expand All @@ -121,7 +121,7 @@ func (l *LBA) ReadAt(lba, off, length int64) (b []byte, err error) {
}

// WriteAt writes to a file in units of LBA.
func (l *LBA) WriteAt(lba, off int64, b []byte) (err error) {
func (l *LBA) WriteAt(lba, off int64, b []byte) error {
off = lba*l.LogicalBlockSize + off

n, err := l.f.WriteAt(b, off)
Expand Down
2 changes: 1 addition & 1 deletion blockdevice/loopback/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// syscalls will return an errno type (which implements error) for all calls,
// including success (errno 0). We only care about non-zero errnos.
func errnoIsErr(err error) error {
if err.(syscall.Errno) != 0 { //nolint:errorlint
if err.(syscall.Errno) != 0 { //nolint:forcetypeassert,errorlint
return err
}

Expand Down
Loading

0 comments on commit fccee8b

Please sign in to comment.