-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
988073b
commit f68ec43
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (C) 2017-present Arctic Ice Studio <development@arcticicestudio.com> | ||
// Copyright (C) 2017-present Sven Greb <development@svengreb.de> | ||
// | ||
// Project: snowsaw | ||
// Repository: https://github.com/arcticicestudio/snowsaw | ||
// License: MIT | ||
|
||
// Author: Arctic Ice Studio <development@arcticicestudio.com> | ||
// Author: Sven Greb <development@svengreb.de> | ||
// Since: 0.4.0 | ||
|
||
// Package task provides task configuration and runner implementations of the snowblock API. | ||
package task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (C) 2017-present Arctic Ice Studio <development@arcticicestudio.com> | ||
// Copyright (C) 2017-present Sven Greb <development@svengreb.de> | ||
// | ||
// Project: snowsaw | ||
// Repository: https://github.com/arcticicestudio/snowsaw | ||
// License: MIT | ||
|
||
// Author: Arctic Ice Studio <development@arcticicestudio.com> | ||
// Author: Sven Greb <development@svengreb.de> | ||
// Since: 0.4.0 | ||
|
||
package task | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fatih/color" | ||
|
||
"github.com/arcticicestudio/snowsaw/pkg/api/snowblock" | ||
) | ||
|
||
// Registry is a registry for available task runner. | ||
type Registry struct { | ||
runner map[string]snowblock.TaskRunner | ||
} | ||
|
||
// NewRegistry returns a new task runner registry instance. | ||
func NewRegistry() *Registry { | ||
return &Registry{runner: make(map[string]snowblock.TaskRunner)} | ||
} | ||
|
||
// Add validates and adds the given task runner to the registry. | ||
// If the name of the given task runner has already been registered and error is returned. | ||
func (reg *Registry) Add(r snowblock.TaskRunner) error { | ||
_, exists := reg.runner[r.GetTaskName()] | ||
if exists { | ||
return fmt.Errorf("runner for task name already exists: %s", color.CyanString(r.GetTaskName())) | ||
} | ||
reg.runner[r.GetTaskName()] = r | ||
return nil | ||
} | ||
|
||
// GetAll returns a list of all currently registered task runner. | ||
func (reg *Registry) GetAll() map[string]snowblock.TaskRunner { | ||
return reg.runner | ||
} |