Skip to content

Commit

Permalink
feat: outline directive implementation example
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Aug 29, 2024
1 parent abc8880 commit bedbd35
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
53 changes: 53 additions & 0 deletions internal/directives/copy_directive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package directives

import (
"context"
"errors"
"fmt"
)

func init() {
// Register the copy directive with the builtins registry.
builtins.RegisterStep(&copyDirective{})
}

// copyDirective is a directive that copies a file or directory.
type copyDirective struct{}

// copyConfig is the configuration for the copy directive.
type copyConfig struct {
// InPath is the path to the file or directory to copy.
InPath string `json:"inPath"`
// OutPath is the path to the destination file or directory.
OutPath string `json:"outPath"`
}

// Validate validates the copy configuration, returning an error if it is invalid.
func (c *copyConfig) Validate() error {
var err []error
if c.InPath == "" {
err = append(err, errors.New("inPath is required"))
}
if c.OutPath == "" {
err = append(err, errors.New("outPath is required"))
}
return errors.Join(err...)
}

func (d *copyDirective) Name() string {
return "copy"
}

func (d *copyDirective) Run(_ context.Context, stepCtx *StepContext) (Result, error) {
cfg, err := configToStruct[copyConfig](stepCtx.Config)
if err != nil {
return ResultFailure, fmt.Errorf("could not convert config into copy config: %w", err)
}
if err = cfg.Validate(); err != nil {
return ResultFailure, fmt.Errorf("invalid copy config: %w", err)
}

// TODO: add implementation here

return ResultSuccess, nil
}
13 changes: 12 additions & 1 deletion internal/directives/registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package directives

import "fmt"
import (
"fmt"
"maps"
)

// builtins is the registry of built-in steps.
var builtins = StepRegistry{}

// BuiltinsRegistry returns a registry of built-in steps.
func BuiltinsRegistry() StepRegistry {
return maps.Clone(builtins)
}

// StepRegistry is a map of step names to steps. It is used to register and
// retrieve steps by name.
Expand Down
21 changes: 21 additions & 0 deletions internal/directives/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package directives

import (
"context"
"encoding/json"

"k8s.io/apimachinery/pkg/runtime"
)
Expand Down Expand Up @@ -72,3 +73,23 @@ type Step interface {
// Run executes the step using the provided context and configuration.
Run(ctx context.Context, stepCtx *StepContext) (Result, error)
}

// configToStruct converts a Config to a (typed) configuration struct.
func configToStruct[T any](c Config) (T, error) {
var result T

// Convert the map to JSON
jsonData, err := json.Marshal(c)
if err != nil {
return result, err
}

// Unmarshal the JSON data into the struct
err = json.Unmarshal(jsonData, &result)
if err != nil {
return result, err
}

return result, nil
}

0 comments on commit bedbd35

Please sign in to comment.