Skip to content

Commit

Permalink
TaskRegistry implementation
Browse files Browse the repository at this point in the history
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
arcticicestudio committed Jul 13, 2019
1 parent 988073b commit f68ec43
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/arcticicestudio/snowsaw/pkg/config/encoder"
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
"github.com/arcticicestudio/snowsaw/pkg/prt"
)

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

func init() {
AppConfigPaths = genConfigPaths()
if err := registerSnowblockTaskRunner(); err != nil {
prt.Fatalf("Failed to register snowblock task runner: %v", err)
panic(err)
}
}

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

return files
}

func registerSnowblockTaskRunner() error {
for _, runner := range availableTaskRunner {
if err := SnowblockTaskRunnerRegistry.Add(runner); err != nil {
return err
}
}

return nil
}
7 changes: 7 additions & 0 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
package config

import (
"github.com/arcticicestudio/snowsaw/pkg/api/snowblock"
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
"github.com/arcticicestudio/snowsaw/pkg/snowblock/task"
)

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

availableTaskRunner []snowblock.TaskRunner

// BuildDateTime is the date and time this application was build.
BuildDateTime string

// SnowblockTaskRunnerRegistry is the application-wide registry for snowblock task runner.
SnowblockTaskRunnerRegistry = task.NewRegistry()

// Version is the application version.
Version = "0.0.0"
)
13 changes: 13 additions & 0 deletions pkg/snowblock/task/doc.go
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
46 changes: 46 additions & 0 deletions pkg/snowblock/task/registry.go
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
}

0 comments on commit f68ec43

Please sign in to comment.