Skip to content

Commit 2c87443

Browse files
refactor: add more linters and implement suggestions (#53)
* frefactor: convert ErrorWithDetails to a method on Detailer struct * feat: add context handling for Detailer with NewContext and FromContext functions * refactor: pass context with detailer to functions returning errors with details * refactor: enhance codebase with additional linters suggestions * chore: bump version to 0.2.3
1 parent f9da99c commit 2c87443

File tree

25 files changed

+66
-22
lines changed

25 files changed

+66
-22
lines changed

.golangci.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
version: "2"
22

33
linters:
4+
enable:
5+
- govet
6+
- errcheck
7+
- staticcheck
8+
- ineffassign
9+
- misspell
10+
- gocritic
11+
- revive
12+
- gosec
13+
- nolintlint
14+
- gocyclo
415
exclusions:
516
warn-unused: true
617
presets:

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Cuestomize
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/workday/cuestomize.svg)](https://pkg.go.dev/github.com/workday/cuestomize)
4+
25
Cuestomize is a Kubernetes Package Manager using CUE-lang and integrated in Kustomize.
36

47
It is implemented as a Kustomize KRM function that reads a CUE model, and optionally some input resources from the Kustomize stream, and passes back to Kustomize the generated resources.

api/convert.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package api provides utilities for converting Go values into CUE values and defines
2+
// interfaces for types that can be converted to CUE format.
13
package api
24

35
import (

api/krm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type KRMInput struct {
3030
// ExtractIncludes populates the includes structure from the provided KRMInput and items.
3131
// It searches items for matches against the includes defined in the KRMInput's spec
3232
// and returns the includes map.
33-
func ExtractIncludes(krm *KRMInput, items []*kyaml.RNode, ctx context.Context) (Includes, error) {
33+
func ExtractIncludes(ctx context.Context, krm *KRMInput, items []*kyaml.RNode) (Includes, error) {
3434
log := logr.FromContextOrDiscard(ctx)
3535

3636
includes := make(Includes)

api/krm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestKRMInput_ExtractIncludes(t *testing.T) {
4747
krmInput := testhelpers.LoadFromFile[KRMInput](t, tt.testdataDir+"/"+TestKRMInputFileName)
4848
items := testhelpers.LoadResourceList(t, tt.testdataDir+"/"+TestKRMInputFileName, tt.testdataDir+"/"+TestItemsFileName)
4949

50-
includes, err := ExtractIncludes(krmInput, items, t.Context())
50+
includes, err := ExtractIncludes(t.Context(), krmInput, items)
5151

5252
if tt.expectedError {
5353
require.Error(t, err)

api/remote_module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import "sigs.k8s.io/kustomize/api/types"
44

5+
// RemoteModule defines the structure to describe a remote CUE module to fetch from an OCI registry.
56
type RemoteModule struct {
67
Registry string `yaml:"registry" json:"registry"`
78
Repo string `yaml:"repo" json:"repo"`

e2e/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main provides an e2e test setup utility that uploads CUE modules to local registries.
12
package main
23

34
import (

hack/push-examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main provides a utility to push CUE module examples to an OCI registry.
12
package main
23

34
import (

internal/pkg/cuerrors/details.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package cuerrors provides utilities for enhanced error formatting and detailed error reporting
2+
// when dealing with CUE language errors. It includes functionality to format errors with
3+
// additional context and details using CUE's built-in error configuration system.
4+
//
5+
// The main component is the Detailer type, which wraps CUE's errors.Config to provide
6+
// consistent error formatting.
17
package cuerrors
28

39
import (
@@ -6,10 +12,13 @@ import (
612
"cuelang.org/go/cue/errors"
713
)
814

15+
// Detailer struct can be used to format CUE errors with additional details.
916
type Detailer struct {
1017
Cfg errors.Config
1118
}
1219

20+
// NewDetailerWithCwd creates a new Detailer with the specified current working directory (cwd).
21+
// In formatted errors, file paths will be made relative to this directory.
1322
func NewDetailerWithCwd(cwd string) Detailer {
1423
return Detailer{
1524
Cfg: errors.Config{

internal/pkg/cuestomize/builder.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package cuestomize provides a builder for creating KRM (Kubernetes Resource Model) functions
2+
// that generate Kubernetes resources from CUE configurations. It enables users to define
3+
// resource transformations using CUE language and apply them as part of a Kustomize pipeline.
14
package cuestomize
25

36
import (
@@ -13,8 +16,8 @@ const (
1316
DefaultResourcesPath = "/cue-resources"
1417
)
1518

16-
// CUEstomizeFuncBuilder is a builder for the CUEstomize KRM function.
17-
type CUEstomizeFuncBuilder struct {
19+
// KRMFuncBuilder is a builder for the Cuestomize KRM function.
20+
type KRMFuncBuilder struct {
1821
// resourcesPath is the path to the directory containing the CUE resources.
1922
// If not set, it defaults to "/cue-resources".
2023
resourcesPath string
@@ -23,29 +26,29 @@ type CUEstomizeFuncBuilder struct {
2326
config *api.KRMInput
2427
}
2528

26-
// NewBuilder creates a new CUEstomizeFuncBuilder with the default resources path.
27-
func NewBuilder() *CUEstomizeFuncBuilder {
28-
return &CUEstomizeFuncBuilder{
29+
// NewBuilder creates a new KRMFuncBuilder with the default resources path.
30+
func NewBuilder() *KRMFuncBuilder {
31+
return &KRMFuncBuilder{
2932
resourcesPath: DefaultResourcesPath,
3033
}
3134
}
3235

3336
// SetConfig sets the reference to the configuration object that the KRM function will receive in input.
34-
func (b *CUEstomizeFuncBuilder) SetConfig(config *api.KRMInput) *CUEstomizeFuncBuilder {
37+
func (b *KRMFuncBuilder) SetConfig(config *api.KRMInput) *KRMFuncBuilder {
3538
b.config = config
3639
return b
3740
}
3841

3942
// SetResourcesPath sets the path to the directory containing the CUE resources.
40-
func (b *CUEstomizeFuncBuilder) SetResourcesPath(resourcesPath string) *CUEstomizeFuncBuilder {
43+
func (b *KRMFuncBuilder) SetResourcesPath(resourcesPath string) *KRMFuncBuilder {
4144
b.resourcesPath = resourcesPath
4245
return b
4346
}
4447

4548
// Build returns a function that can be used to generate resources from a CUE configuration and some input resources.
46-
func (b *CUEstomizeFuncBuilder) Build(ctx context.Context) (func([]*kyaml.RNode) ([]*kyaml.RNode, error), error) {
49+
func (b *KRMFuncBuilder) Build(ctx context.Context) (func([]*kyaml.RNode) ([]*kyaml.RNode, error), error) {
4750
if b.config == nil {
4851
return nil, fmt.Errorf("config must be set before building the KRM function")
4952
}
50-
return newCuestomizeFunctionWithPath(b.config, &b.resourcesPath, ctx), nil
53+
return newCuestomizeFunctionWithPath(ctx, b.config, &b.resourcesPath), nil
5154
}

0 commit comments

Comments
 (0)