Skip to content

Commit

Permalink
attend linting errors
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jun 17, 2024
1 parent c1c47b5 commit 8830b49
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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$'
Expand Down
25 changes: 14 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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(&registry, "catalog", "c", "catalog.json", "dependencies catalog")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print build process output")

Expand Down
1 change: 0 additions & 1 deletion cmd/k6build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/grafana/k6build/cmd"
)

//nolint:all
func main() {
root := cmd.New()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ require (
golang.org/x/sys v0.21.0 // indirect
)

retract v0.0.0
retract v0.0.0 // premature publishing
18 changes: 12 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package k6build
import (
"bytes"
"context"
"crypto/sha1"
"crypto/sha1" //nolint:gosec
"errors"
"fmt"
"sort"
Expand Down Expand Up @@ -59,6 +59,7 @@ type buildsrv struct {
cache Cache
}

// NewBuildService creates a build service
func NewBuildService(
catalog k6catalog.Catalog,
builder k6foundry.Builder,
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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])))
Expand Down
2 changes: 2 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

func TestBuild(t *testing.T) {
t.Parallel()

modules := []struct {
path string
version string
Expand Down

0 comments on commit 8830b49

Please sign in to comment.