Skip to content

Commit

Permalink
Pipeline: reorganize code
Browse files Browse the repository at this point in the history
Put every operation into dedicated source file.
Use common naming convention of files.

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 3, 2024
1 parent 8509454 commit 58aabe5
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 434 deletions.
39 changes: 39 additions & 0 deletions pipeline/action_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"fmt"
)

// ActionMeta holds action's metadata used by Executor
type ActionMeta struct {
// Name of this step, should be unique within current scope
Name string `yaml:"name,omitempty"`

// Optional ordinal number that controls order of execution within parent step
Order int `yaml:"order,omitempty"`

// Optional expression to make execution of this action conditional.
// Execution of this step is skipped when this expression is evaluated to false.
// If value of this field is omitted, then this action is executed.
When *string `yaml:"when,omitempty"`
}

func (am ActionMeta) String() string {
return fmt.Sprintf("[name=%s,order=%d,when=%s]", am.Name, am.Order, safeStrDeref(am.When))
}
60 changes: 60 additions & 0 deletions pipeline/action_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"fmt"
)

type ActionSpec struct {
ActionMeta `yaml:",inline"`
// Operations to perform
Operations OpSpec `yaml:",inline"`
// Children element is an optional map of child actions that will be executed
// as a part of this action (after any of OpSpec in Operations are performed).
// Exact order of execution is given by Order field value (lower the value, sooner the execution will take place).
Children ChildActions `yaml:"steps,omitempty"`
}

func (s ActionSpec) CloneWith(ctx ActionContext) Action {
return ActionSpec{
ActionMeta: s.ActionMeta,
Operations: s.Operations.CloneWith(ctx).(OpSpec),
Children: s.Children.CloneWith(ctx).(ChildActions),
}
}

func (s ActionSpec) String() string {
return fmt.Sprintf("ActionSpec[meta=%v]", s.ActionMeta)
}

func (s ActionSpec) Do(ctx ActionContext) error {
for _, a := range []Action{s.Operations, s.Children} {
if s.When != nil {
if ok, err := ctx.TemplateEngine().EvalBool(*s.When, ctx.Snapshot()); err != nil {
return err
} else if !ok {
return nil
}
}
err := ctx.Executor().Execute(a)
if err != nil {
return err
}
}
return nil
}
165 changes: 0 additions & 165 deletions pipeline/actions.go

This file was deleted.

17 changes: 17 additions & 0 deletions pipeline/env_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ var (
// TODO: merge with same thing from analytics package and move it to common place
type StringPredicateFn func(string) bool

// EnvOp is used to import OS environment variables into data
type EnvOp struct {
// Optional regexp which defines what to include. Only item names matching this regexp are added into data document.
Include *regexp.Regexp `yaml:"include,omitempty"`

// Optional regexp which defines what to exclude. Only item names NOT matching this regexp are added into data document.
// Exclusion is considered after inclusion regexp is processed.
Exclude *regexp.Regexp `yaml:"exclude,omitempty"`

// Optional path within data tree under which "Env" container will be put.
// When omitted, then "Env" goes to root of data.
Path string `yaml:"path,omitempty"`

// for mock purposes only. this could be used to override os.Environ() to arbitrary func
envGetter func() []string
}

func (eo *EnvOp) Do(ctx ActionContext) error {
var (
inclFn StringPredicateFn
Expand Down
9 changes: 9 additions & 0 deletions pipeline/exec_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import (
"slices"
)

type ExecOp struct {
// Program to execute
Program string `yaml:"program,omitempty"`
// Optional arguments for program
Args *[]string `yaml:"args,omitempty"`
// List of exit codes that are assumed to be valid
ValidExitCodes *[]int `yaml:"validExitCodes,omitempty"`
}

func (e *ExecOp) String() string {
return fmt.Sprintf("Exec[Program=%s,Args=%d]", e.Program, safeStrListSize(e.Args))
}
Expand Down
20 changes: 20 additions & 0 deletions pipeline/export_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ import (
"os"
)

type OutputFormat string

const (
OutputFormatYaml = OutputFormat("yaml")
OutputFormatJson = OutputFormat("json")
OutputFormatProperties = OutputFormat("properties")
)

// ExportOp allows to export data into file
type ExportOp struct {
// File to export data onto
File string
// Path within data tree pointing to dom.Container to export. Empty path denotes whole document.
// If path does not resolve or resolves to dom.Node that is not dom.Container,
// then empty document will be exported.
Path string
// Format of output file.
Format OutputFormat
}

func (e *ExportOp) String() string {
return fmt.Sprintf("Export[file=%s,format=%s,path=%s]", e.File, e.Format, e.Path)
}
Expand Down
7 changes: 7 additions & 0 deletions pipeline/foreach.go → pipeline/foreach_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import (
"path/filepath"
)

type ForEachOp struct {
Glob *string `yaml:"glob,omitempty"`
Item *[]string `yaml:"item,omitempty"`
// Action to perform for every item
Action OpSpec `yaml:"action"`
}

func (fea *ForEachOp) Do(ctx ActionContext) error {
if nonEmpty(fea.Glob) {
if matches, err := filepath.Glob(*fea.Glob); err != nil {
Expand Down
File renamed without changes.
Loading

0 comments on commit 58aabe5

Please sign in to comment.