Skip to content

Commit f68ec43

Browse files
TaskRegistry implementation
Implemented the `pkg/api/snowblock.TaskRegistry` interface to register future implementations of the `pkg/api/snowblock.TaskRunner` interface. The validation logic of the `Add` function filters and denies task runners that might try to register a duplicate task name. Epic GH-33 Resolves GH-71
1 parent 988073b commit f68ec43

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

pkg/config/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arcticicestudio/snowsaw/pkg/config/encoder"
2424
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
25+
"github.com/arcticicestudio/snowsaw/pkg/prt"
2526
)
2627

2728
// Config represents the application-wide configurations.
@@ -43,6 +44,10 @@ type Snowblocks struct {
4344

4445
func init() {
4546
AppConfigPaths = genConfigPaths()
47+
if err := registerSnowblockTaskRunner(); err != nil {
48+
prt.Fatalf("Failed to register snowblock task runner: %v", err)
49+
panic(err)
50+
}
4651
}
4752

4853
func genConfigPaths() []*file.File {
@@ -62,3 +67,13 @@ func genConfigPaths() []*file.File {
6267

6368
return files
6469
}
70+
71+
func registerSnowblockTaskRunner() error {
72+
for _, runner := range availableTaskRunner {
73+
if err := SnowblockTaskRunnerRegistry.Add(runner); err != nil {
74+
return err
75+
}
76+
}
77+
78+
return nil
79+
}

pkg/config/constants.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
package config
1313

1414
import (
15+
"github.com/arcticicestudio/snowsaw/pkg/api/snowblock"
1516
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
17+
"github.com/arcticicestudio/snowsaw/pkg/snowblock/task"
1618
)
1719

1820
const (
@@ -36,9 +38,14 @@ var (
3638
// AppConfigPaths is the default paths the application will search for configuration files.
3739
AppConfigPaths []*file.File
3840

41+
availableTaskRunner []snowblock.TaskRunner
42+
3943
// BuildDateTime is the date and time this application was build.
4044
BuildDateTime string
4145

46+
// SnowblockTaskRunnerRegistry is the application-wide registry for snowblock task runner.
47+
SnowblockTaskRunnerRegistry = task.NewRegistry()
48+
4249
// Version is the application version.
4350
Version = "0.0.0"
4451
)

pkg/snowblock/task/doc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (C) 2017-present Arctic Ice Studio <[email protected]>
2+
// Copyright (C) 2017-present Sven Greb <[email protected]>
3+
//
4+
// Project: snowsaw
5+
// Repository: https://github.com/arcticicestudio/snowsaw
6+
// License: MIT
7+
8+
// Author: Arctic Ice Studio <[email protected]>
9+
// Author: Sven Greb <[email protected]>
10+
// Since: 0.4.0
11+
12+
// Package task provides task configuration and runner implementations of the snowblock API.
13+
package task

pkg/snowblock/task/registry.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (C) 2017-present Arctic Ice Studio <[email protected]>
2+
// Copyright (C) 2017-present Sven Greb <[email protected]>
3+
//
4+
// Project: snowsaw
5+
// Repository: https://github.com/arcticicestudio/snowsaw
6+
// License: MIT
7+
8+
// Author: Arctic Ice Studio <[email protected]>
9+
// Author: Sven Greb <[email protected]>
10+
// Since: 0.4.0
11+
12+
package task
13+
14+
import (
15+
"fmt"
16+
17+
"github.com/fatih/color"
18+
19+
"github.com/arcticicestudio/snowsaw/pkg/api/snowblock"
20+
)
21+
22+
// Registry is a registry for available task runner.
23+
type Registry struct {
24+
runner map[string]snowblock.TaskRunner
25+
}
26+
27+
// NewRegistry returns a new task runner registry instance.
28+
func NewRegistry() *Registry {
29+
return &Registry{runner: make(map[string]snowblock.TaskRunner)}
30+
}
31+
32+
// Add validates and adds the given task runner to the registry.
33+
// If the name of the given task runner has already been registered and error is returned.
34+
func (reg *Registry) Add(r snowblock.TaskRunner) error {
35+
_, exists := reg.runner[r.GetTaskName()]
36+
if exists {
37+
return fmt.Errorf("runner for task name already exists: %s", color.CyanString(r.GetTaskName()))
38+
}
39+
reg.runner[r.GetTaskName()] = r
40+
return nil
41+
}
42+
43+
// GetAll returns a list of all currently registered task runner.
44+
func (reg *Registry) GetAll() map[string]snowblock.TaskRunner {
45+
return reg.runner
46+
}

0 commit comments

Comments
 (0)