From 8830b492805fae87491e5963348c07abbd5e461b Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Mon, 17 Jun 2024 12:25:17 +0200 Subject: [PATCH] attend linting errors Signed-off-by: Pablo Chacin --- .golangci.yml | 2 -- cache.go | 25 ++++++++++++++----------- cache_test.go | 8 ++++---- cmd/cmd.go | 9 ++++++--- cmd/k6build/main.go | 1 - go.mod | 2 +- service.go | 18 ++++++++++++------ service_test.go | 2 ++ 8 files changed, 39 insertions(+), 28 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 61cc210..c450228 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -60,8 +60,6 @@ linters-settings: forbidigo: forbid: - '^(fmt\\.Print(|f|ln)|print|println)$' - # Forbid everything in os, except os.Signal and os.SyscalError - - '^os\.(.*)$(# Using anything except Signal and SyscallError from the os package is forbidden )?' # Forbid everything in syscall except the uppercase constants - '^syscall\.[^A-Z_]+$(# Using anything except constants from the syscall package is forbidden )?' - '^logrus\.Logger$' diff --git a/cache.go b/cache.go index 6e07d66..fd25782 100644 --- a/cache.go +++ b/cache.go @@ -12,10 +12,10 @@ import ( ) var ( - ErrObjectNotFound = errors.New("object not found") - ErrAccessingObject = errors.New("accessing object") - ErrCreatingObject = errors.New("creating object") - ErrInitializingCache = errors.New("initializing cache") + ErrObjectNotFound = errors.New("object not found") //nolint:revive + ErrAccessingObject = errors.New("accessing object") //nolint:revive + ErrCreatingObject = errors.New("creating object") //nolint:revive + ErrInitializingCache = errors.New("initializing cache") //nolint:revive ) // Object represents an object stored in the Cache @@ -40,13 +40,14 @@ type fileObjectStore struct { path string } +// NewTempFileCache creates a file cache using a temporary file func NewTempFileCache() (Cache, error) { return NewFileCache(filepath.Join(os.TempDir(), "buildcache")) } // NewFileCache creates an cached backed by a directory func NewFileCache(path string) (Cache, error) { - err := os.MkdirAll(path, 0o777) + err := os.MkdirAll(path, 0o750) if err != nil { return nil, fmt.Errorf("%w: %w", ErrInitializingCache, err) } @@ -56,15 +57,16 @@ func NewFileCache(path string) (Cache, error) { }, nil } -func (f *fileObjectStore) Store(ctx context.Context, id string, content io.Reader) (Object, error) { +// Store stores the object and returns the metadata +func (f *fileObjectStore) Store(_ context.Context, id string, content io.Reader) (Object, error) { objectDir := filepath.Join(f.path, id) // TODO: check permissions - err := os.MkdirAll(objectDir, 0o777) + err := os.MkdirAll(objectDir, 0o750) if err != nil { return Object{}, fmt.Errorf("%w: %w", ErrCreatingObject, err) } - objectFile, err := os.Create(filepath.Join(objectDir, "data")) + objectFile, err := os.Create(filepath.Join(objectDir, "data")) //nolint:gosec if err != nil { return Object{}, fmt.Errorf("%w: %w", ErrCreatingObject, err) } @@ -83,7 +85,7 @@ func (f *fileObjectStore) Store(ctx context.Context, id string, content io.Reade checksum := fmt.Sprintf("%x", checksumHash.Sum(nil)) // write metadata - err = os.WriteFile(filepath.Join(objectDir, "checksum"), []byte(checksum), 0o644) + err = os.WriteFile(filepath.Join(objectDir, "checksum"), []byte(checksum), 0o644) //nolint:gosec if err != nil { return Object{}, fmt.Errorf("%w: %w", ErrCreatingObject, err) } @@ -95,7 +97,8 @@ func (f *fileObjectStore) Store(ctx context.Context, id string, content io.Reade }, nil } -func (f *fileObjectStore) Get(ctx context.Context, id string) (Object, error) { +// Get retrieves an objects if exists in the cache or an error otherwise +func (f *fileObjectStore) Get(_ context.Context, id string) (Object, error) { objectDir := filepath.Join(f.path, id) _, err := os.Stat(objectDir) @@ -107,7 +110,7 @@ func (f *fileObjectStore) Get(ctx context.Context, id string) (Object, error) { return Object{}, fmt.Errorf("%w: %w", ErrAccessingObject, err) } - checksum, err := os.ReadFile(filepath.Join(objectDir, "checksum")) + checksum, err := os.ReadFile(filepath.Join(objectDir, "checksum")) //nolint:gosec if err != nil { return Object{}, fmt.Errorf("%w: %w", ErrAccessingObject, err) } diff --git a/cache_test.go b/cache_test.go index 0462555..d2c7579 100644 --- a/cache_test.go +++ b/cache_test.go @@ -42,12 +42,12 @@ func TestCreateObject(t *testing.T) { t.Fatalf("expected %v got %v", tc.expectErr, err) } - fileUrl, err := url.Parse(obj.URL) + fileURL, err := url.Parse(obj.URL) if err != nil { t.Fatalf("invalid url %v", err) } - content, err := os.ReadFile(fileUrl.Path) + content, err := os.ReadFile(fileURL.Path) if err != nil { t.Fatalf("reading object url %v", err) } @@ -104,12 +104,12 @@ func TestGetObjectCache(t *testing.T) { return } - fileUrl, err := url.Parse(obj.URL) + fileURL, err := url.Parse(obj.URL) if err != nil { t.Fatalf("invalid url %v", err) } - data, err := os.ReadFile(fileUrl.Path) + data, err := os.ReadFile(fileURL.Path) if err != nil { t.Fatalf("reading object url %v", err) } diff --git a/cmd/cmd.go b/cmd/cmd.go index 2ed628f..c4e48e2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -78,14 +78,17 @@ func New() *cobra.Command { buildDeps = append(buildDeps, k6build.Dependency{Name: name, Constraints: constrains}) } - artifact, err := srv.Build(context.TODO(), platform, k6Constrains, buildDeps) + artifact, err := srv.Build(cmd.Context(), platform, k6Constrains, buildDeps) if err != nil { return fmt.Errorf("building %w", err) } encoder := json.NewEncoder(os.Stdout) encoder.SetIndent("", " ") - encoder.Encode(artifact) + err = encoder.Encode(artifact) + if err != nil { + return fmt.Errorf("processing object %w", err) + } return nil }, @@ -94,7 +97,7 @@ func New() *cobra.Command { cmd.Flags().StringArrayVarP(&deps, "dependency", "d", nil, "list of dependencies in form package:constrains") cmd.Flags().StringVarP(&k6Constrains, "k6-constrains", "k", "*", "k6 version constrains") cmd.Flags().StringVarP(&platform, "platform", "p", "", "target platform (default GOOS/GOARCH)") - cmd.MarkFlagRequired("platform") + _ = cmd.MarkFlagRequired("platform") cmd.Flags().StringVarP(®istry, "catalog", "c", "catalog.json", "dependencies catalog") cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print build process output") diff --git a/cmd/k6build/main.go b/cmd/k6build/main.go index c86cbfd..b43c137 100644 --- a/cmd/k6build/main.go +++ b/cmd/k6build/main.go @@ -8,7 +8,6 @@ import ( "github.com/grafana/k6build/cmd" ) -//nolint:all func main() { root := cmd.New() diff --git a/go.mod b/go.mod index fb61777..e8c597f 100644 --- a/go.mod +++ b/go.mod @@ -19,4 +19,4 @@ require ( golang.org/x/sys v0.21.0 // indirect ) -retract v0.0.0 +retract v0.0.0 // premature publishing diff --git a/service.go b/service.go index 09982e2..635e05d 100644 --- a/service.go +++ b/service.go @@ -4,7 +4,7 @@ package k6build import ( "bytes" "context" - "crypto/sha1" + "crypto/sha1" //nolint:gosec "errors" "fmt" "sort" @@ -59,6 +59,7 @@ type buildsrv struct { cache Cache } +// NewBuildService creates a build service func NewBuildService( catalog k6catalog.Catalog, builder k6foundry.Builder, @@ -95,7 +96,12 @@ func DefaultLocalBuildService() (BuildService, error) { }, nil } -func (b *buildsrv) Build(ctx context.Context, platform string, k6Constrains string, deps []Dependency) (Artifact, error) { +func (b *buildsrv) Build( + ctx context.Context, + platform string, + k6Constrains string, + deps []Dependency, +) (Artifact, error) { buildPlatform, err := k6foundry.ParsePlatform(platform) if err != nil { return Artifact{}, fmt.Errorf("invalid platform %w", err) @@ -111,9 +117,9 @@ func (b *buildsrv) Build(ctx context.Context, platform string, k6Constrains stri mods := []k6foundry.Module{} for _, d := range deps { - m, err := b.catalog.Resolve(ctx, k6catalog.Dependency{Name: d.Name, Constrains: d.Constraints}) - if err != nil { - return Artifact{}, err + m, modErr := b.catalog.Resolve(ctx, k6catalog.Dependency{Name: d.Name, Constrains: d.Constraints}) + if modErr != nil { + return Artifact{}, modErr } mods = append(mods, k6foundry.Module{Path: m.Path, Version: m.Version}) resolved[d.Name] = m.Version @@ -123,7 +129,7 @@ func (b *buildsrv) Build(ctx context.Context, platform string, k6Constrains stri sort.Strings(sorted) // generate id form sorted list of dependencies - hash := sha1.New() + hash := sha1.New() //nolint:gosec hash.Sum([]byte(platform)) for _, d := range sorted { hash.Sum([]byte(fmt.Sprintf("%s:%s", d, resolved[d]))) diff --git a/service_test.go b/service_test.go index ac9e45a..d8cc850 100644 --- a/service_test.go +++ b/service_test.go @@ -15,6 +15,8 @@ import ( ) func TestBuild(t *testing.T) { + t.Parallel() + modules := []struct { path string version string