Skip to content

Commit 78a322f

Browse files
authored
refactor: interface naming, and functional options (#101)
* refactor(logger): rename LoggerInterface to Logger * refactor(worker): add functional options for WorkerPoolOptions * chore: cleanup go.mod * chore(lint): allow revive var-naming for internal/ packages to avoid noisy warnings * test: add benchmarks for processor and worker pool * refactor(packages): remove nolint comments from package declarations
1 parent 59351d7 commit 78a322f

32 files changed

+545
-67
lines changed

.golangci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ linters:
7373
linters:
7474
- gocritic
7575
text: "stringXbytes"
76+
# Internal packages: allow var-naming for packages matching their directory
77+
- path: internal/
78+
linters:
79+
- revive
80+
text: "var-naming: avoid"
7681
# Prealloc is often too noisy for small slices
7782
- path: \.go$
7883
linters:

cmd/tempo/synccmd/sync.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,16 @@ func resolveSyncFlags(
338338
}
339339

340340
// Worker pool options
341-
opts := worker.WorkerPoolOptions{
342-
Context: ctx,
343-
InputDir: inputDir,
344-
OutputDir: outputDir,
345-
ExcludeDir: excludeDir,
346-
MarkerName: cmdCtx.Config.Templates.GuardMarker,
347-
NumWorkers: numWorkers,
348-
IsProduction: isProd,
349-
IsForce: isForce,
350-
IsTrackExecutionTime: isTrackExecutionTime,
341+
opts, err := worker.NewWorkerPoolOptions(ctx, inputDir, outputDir,
342+
worker.WithExcludeDir(excludeDir),
343+
worker.WithMarkerName(cmdCtx.Config.Templates.GuardMarker),
344+
worker.WithNumWorkers(numWorkers),
345+
worker.WithProduction(isProd),
346+
worker.WithForce(isForce),
347+
worker.WithTrackExecutionTime(isTrackExecutionTime),
348+
)
349+
if err != nil {
350+
return worker.WorkerPoolOptions{}, nil, apperrors.Wrap("invalid worker pool options", err)
351351
}
352352

353353
// Summary options
@@ -375,7 +375,7 @@ func resolveSyncFlags(
375375
}
376376

377377
func handleSummary(
378-
logger logger.LoggerInterface,
378+
logger logger.Logger,
379379
manager *worker.WorkerPoolManager,
380380
processingErrors []worker.ProcessingError,
381381
skippedFiles []worker.ProcessingError,

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,3 @@ require (
1919
github.com/mattn/go-isatty v0.0.20 // indirect
2020
golang.org/x/sys v0.39.0 // indirect
2121
)
22-
23-
// replace (
24-
// github.com/indaco/tempo-api => ../tempo-api
25-
// )

internal/app/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
type AppContext struct {
9-
Logger logger.LoggerInterface
9+
Logger logger.Logger
1010
Config *config.Config
1111
CWD string
1212
}

internal/generator/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func GenerateActionJSONFile(filePath string, actions ActionList) error {
246246
/* ------------------------------------------------------------------------- */
247247

248248
// GenerateActionFile generates an action file for the given entity type.
249-
func GenerateActionFile(entityType string, data *TemplateData, actions []Action, logger logger.LoggerInterface) error {
249+
func GenerateActionFile(entityType string, data *TemplateData, actions []Action, logger logger.Logger) error {
250250
actionFileName := fmt.Sprintf("%s.json", entityType)
251251
actionsPath := filepath.Join(data.ActionsDir, actionFileName)
252252

@@ -260,7 +260,7 @@ func GenerateActionFile(entityType string, data *TemplateData, actions []Action,
260260
}
261261

262262
// RetrieveActionsFile retrieves actions from a JSON file.
263-
func RetrieveActionsFile(logger logger.LoggerInterface, actionFilePath string, cfg *config.Config) (JSONActionList, error) {
263+
func RetrieveActionsFile(logger logger.Logger, actionFilePath string, cfg *config.Config) (JSONActionList, error) {
264264
// Step 1: Resolve action file path
265265
resolvedPath, err := resolveActionFilePath(cfg.Paths.ActionsDir, actionFilePath)
266266
if err != nil {
@@ -283,7 +283,7 @@ func RetrieveActionsFile(logger logger.LoggerInterface, actionFilePath string, c
283283
}
284284

285285
// ProcessEntityActions retrieves and processes actions from a JSON file.
286-
func ProcessEntityActions(ctx context.Context, logger logger.LoggerInterface, pathToActionsFile string, data *TemplateData, cfg *config.Config) error {
286+
func ProcessEntityActions(ctx context.Context, logger logger.Logger, pathToActionsFile string, data *TemplateData, cfg *config.Config) error {
287287
// Validate context - use Background as fallback for non-critical operations
288288
if ctx == nil {
289289
ctx = context.Background()

internal/generator/action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ func TestProcessEntityActions(t *testing.T) {
871871

872872
// Override ProcessActionsFunc temporarily
873873
originalProcessActions := ProcessActionsFunc
874-
ProcessActionsFunc = func(ctx context.Context, logger logger.LoggerInterface, actions []Action, data *TemplateData) error {
874+
ProcessActionsFunc = func(ctx context.Context, logger logger.Logger, actions []Action, data *TemplateData) error {
875875
// Debugging output
876876
for i, action := range actions {
877877
t.Logf("DEBUG: Action[%d]: %+v", i, action)

internal/generator/generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// ActionProcessor defines the interface for processing actions.
1414
// This allows for dependency injection and easier testing.
1515
type ActionProcessor interface {
16-
ProcessActions(ctx context.Context, logger logger.LoggerInterface, actions []Action, data *TemplateData) error
16+
ProcessActions(ctx context.Context, logger logger.Logger, actions []Action, data *TemplateData) error
1717
}
1818

1919
// DefaultActionProcessor is the default implementation of ActionProcessor.
@@ -25,7 +25,7 @@ func NewActionProcessor() ActionProcessor {
2525
}
2626

2727
// ProcessActions implements ActionProcessor.ProcessActions.
28-
func (p *DefaultActionProcessor) ProcessActions(ctx context.Context, logger logger.LoggerInterface, actions []Action, data *TemplateData) error {
28+
func (p *DefaultActionProcessor) ProcessActions(ctx context.Context, logger logger.Logger, actions []Action, data *TemplateData) error {
2929
return ProcessActions(ctx, logger, actions, data)
3030
}
3131

@@ -44,7 +44,7 @@ var actionHandlers = map[string]ActionHandler{
4444
var ProcessActionsFunc = ProcessActions
4545

4646
// ProcessActions processes a list of actions using the appropriate handlers.
47-
func ProcessActions(ctx context.Context, logger logger.LoggerInterface, actions []Action, data *TemplateData) error {
47+
func ProcessActions(ctx context.Context, logger logger.Logger, actions []Action, data *TemplateData) error {
4848
// Validate context - use Background as fallback for non-critical operations
4949
if ctx == nil {
5050
ctx = context.Background()
@@ -74,7 +74,7 @@ func ProcessActions(ctx context.Context, logger logger.LoggerInterface, actions
7474
}
7575

7676
// handleDryRun handles the dry-run mode by resolving templates and logging the actions.
77-
func handleDryRun(logger logger.LoggerInterface, action Action, data *TemplateData) {
77+
func handleDryRun(logger logger.Logger, action Action, data *TemplateData) {
7878
switch action.Item {
7979
case "file":
8080
// Handle single file addition

internal/git/git.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
// GitOperations defines the interface for git operations.
1414
// This allows for dependency injection and easier testing.
1515
type GitOperations interface {
16-
CloneOrUpdate(repoURL, repoPath string, logger logger.LoggerInterface) error
17-
Update(repoPath string, logger logger.LoggerInterface) error
18-
Clone(repoURL, repoPath string, logger logger.LoggerInterface) error
19-
ForceReclone(repoURL, repoPath string, logger logger.LoggerInterface) error
16+
CloneOrUpdate(repoURL, repoPath string, logger logger.Logger) error
17+
Update(repoPath string, logger logger.Logger) error
18+
Clone(repoURL, repoPath string, logger logger.Logger) error
19+
ForceReclone(repoURL, repoPath string, logger logger.Logger) error
2020
IsValidRepo(repoPath string) bool
2121
}
2222

@@ -38,7 +38,7 @@ var (
3838
)
3939

4040
// Default implementation of CloneOrUpdate
41-
func DefaultCloneOrUpdate(repoURL, repoPath string, logger logger.LoggerInterface) error {
41+
func DefaultCloneOrUpdate(repoURL, repoPath string, logger logger.Logger) error {
4242
if IsValidGitRepo(repoPath) {
4343
logger.Info("Updating existing plugin repository").WithAttrs("repo_path", repoPath)
4444
return UpdateRepo(repoPath, logger)
@@ -48,14 +48,14 @@ func DefaultCloneOrUpdate(repoURL, repoPath string, logger logger.LoggerInterfac
4848
}
4949

5050
// Default implementation of UpdateRepo
51-
func DefaultUpdateRepo(repoPath string, logger logger.LoggerInterface) error {
51+
func DefaultUpdateRepo(repoPath string, logger logger.Logger) error {
5252
logger.Info("Updating existing repository").WithAttrs("repo_path", repoPath)
5353
return cmdrunner.RunCommand(repoPath, "git", "pull")
5454
}
5555

5656
// CloneRepo clones a Git repository into the specified path.
5757
// It validates the URL and path to prevent command injection attacks.
58-
func CloneRepo(repoURL, repoPath string, logger logger.LoggerInterface) error {
58+
func CloneRepo(repoURL, repoPath string, logger logger.Logger) error {
5959
// Validate URL to prevent command injection
6060
if err := validation.ValidateGitURL(repoURL); err != nil {
6161
return apperrors.Wrap("invalid repository URL", err)
@@ -73,7 +73,7 @@ func CloneRepo(repoURL, repoPath string, logger logger.LoggerInterface) error {
7373

7474
// ForceReclone removes an existing repository and re-clones it.
7575
// It validates the URL and path before performing any operations.
76-
func ForceReclone(repoURL, repoPath string, logger logger.LoggerInterface) error {
76+
func ForceReclone(repoURL, repoPath string, logger logger.Logger) error {
7777
// Validate URL first (CloneRepo will also validate, but fail fast)
7878
if err := validation.ValidateGitURL(repoURL); err != nil {
7979
return apperrors.Wrap("invalid repository URL", err)
@@ -103,22 +103,22 @@ func IsValidGitRepo(repoPath string) bool {
103103
// Interface implementations for DefaultGitOps
104104

105105
// CloneOrUpdate implements GitOperations.CloneOrUpdate.
106-
func (g *DefaultGitOps) CloneOrUpdate(repoURL, repoPath string, logger logger.LoggerInterface) error {
106+
func (g *DefaultGitOps) CloneOrUpdate(repoURL, repoPath string, logger logger.Logger) error {
107107
return DefaultCloneOrUpdate(repoURL, repoPath, logger)
108108
}
109109

110110
// Update implements GitOperations.Update.
111-
func (g *DefaultGitOps) Update(repoPath string, logger logger.LoggerInterface) error {
111+
func (g *DefaultGitOps) Update(repoPath string, logger logger.Logger) error {
112112
return DefaultUpdateRepo(repoPath, logger)
113113
}
114114

115115
// Clone implements GitOperations.Clone.
116-
func (g *DefaultGitOps) Clone(repoURL, repoPath string, logger logger.LoggerInterface) error {
116+
func (g *DefaultGitOps) Clone(repoURL, repoPath string, logger logger.Logger) error {
117117
return CloneRepo(repoURL, repoPath, logger)
118118
}
119119

120120
// ForceReclone implements GitOperations.ForceReclone.
121-
func (g *DefaultGitOps) ForceReclone(repoURL, repoPath string, logger logger.LoggerInterface) error {
121+
func (g *DefaultGitOps) ForceReclone(repoURL, repoPath string, logger logger.Logger) error {
122122
return ForceReclone(repoURL, repoPath, logger)
123123
}
124124

internal/git/git_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestDefaultCloneOrUpdate(t *testing.T) {
2323
originalUpdateRepo := UpdateRepo
2424
defer func() { UpdateRepo = originalUpdateRepo }()
2525

26-
UpdateRepo = func(repoPath string, logger logger.LoggerInterface) error {
26+
UpdateRepo = func(repoPath string, logger logger.Logger) error {
2727
if repoPath != tempDir {
2828
t.Errorf("UpdateRepo called with wrong path: got %s, want %s", repoPath, tempDir)
2929
}
@@ -46,7 +46,7 @@ func TestDefaultCloneOrUpdate(t *testing.T) {
4646
originalCloneRepo := CloneRepoFunc
4747
defer func() { CloneRepoFunc = originalCloneRepo }()
4848

49-
CloneRepoFunc = func(repoURL, repoPath string, logger logger.LoggerInterface) error {
49+
CloneRepoFunc = func(repoURL, repoPath string, logger logger.Logger) error {
5050
if repoPath != destRepo {
5151
t.Errorf("CloneRepoFunc called with wrong path: got %s, want %s", repoPath, destRepo)
5252
}

internal/helpers/entity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// CheckEntityForNew logs a warning or info message when creating a new entity that already exists.
1212
// It handles both component and variant entity types with appropriate path formatting.
13-
func CheckEntityForNew(entityType, entityName, outputPath string, force bool, logr logger.LoggerInterface) {
13+
func CheckEntityForNew(entityType, entityName, outputPath string, force bool, logr logger.Logger) {
1414
// Select logging function and action message based on `force` flag
1515
logFunc, action := logr.Warning, "Use '--force' to overwrite it. Any changes will be lost."
1616
if force {
@@ -32,7 +32,7 @@ func CheckEntityForNew(entityType, entityName, outputPath string, force bool, lo
3232
}
3333

3434
// CheckEntityForDefine logs a warning or info message when defining templates that already exist.
35-
func CheckEntityForDefine(entityType, outputPath string, force bool, logr logger.LoggerInterface) {
35+
func CheckEntityForDefine(entityType, outputPath string, force bool, logr logger.Logger) {
3636
// Select logging function and action message based on `force` flag
3737
logFunc, action := logr.Warning, "Use '--force' to overwrite them. Any changes will be lost."
3838
if force {

0 commit comments

Comments
 (0)