Skip to content

Commit

Permalink
Pipeline: Preliminary implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed May 28, 2024
1 parent cdaaada commit aa571fa
Show file tree
Hide file tree
Showing 13 changed files with 1,115 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module github.com/rkosegi/yaml-toolkit
go 1.21

require (
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/google/go-cmp v0.6.0
github.com/magiconair/properties v1.8.7
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
Expand Down
166 changes: 166 additions & 0 deletions pipeline/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
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 (
"bytes"
"encoding/base64"
"fmt"
sprig "github.com/go-task/slim-sprig/v3"
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/rkosegi/yaml-toolkit/patch"
"github.com/rkosegi/yaml-toolkit/props"
"path/filepath"
"text/template"
)

func (pfm ParseFileMode) toValue(content []byte) (dom.Node, error) {
switch pfm {
case ParseFileModeBinary:
return dom.LeafNode(base64.StdEncoding.EncodeToString(content)), nil
case ParseFileModeText:
return dom.LeafNode(string(content)), nil
case ParseFileModeYaml:
return b.FromReader(bytes.NewReader(content), dom.DefaultYamlDecoder)
case ParseFileModeJson:
return b.FromReader(bytes.NewReader(content), dom.DefaultJsonDecoder)
case ParseFileModeProperties:
return b.FromReader(bytes.NewReader(content), props.DecoderFn)
default:
return nil, fmt.Errorf("invalid ParseFileMode: %v", pfm)
}
}

func (ia *ImportAction) String() string {
return fmt.Sprintf("Import[file=%s,path=%s,mode=%s]", ia.File, ia.Path, ia.Mode)
}

func (ia *ImportAction) Perform(ctx ActionContext) error {
if len(ia.Path) == 0 {
return ErrPathMissing
}
val, err := parseFile(ia.File, ia.Mode)
if err != nil {
return err
}
ctx.Data().AddValueAt(ia.Path, val)
return nil
}

func (iga *ImportGlobAction) String() string {
return fmt.Sprintf("ImportGlob[glob=%s,mode=%s]", iga.Glob, iga.Mode)
}

func (iga *ImportGlobAction) Perform(ctx ActionContext) error {
if len(iga.Template) == 0 {
return ErrTemplateEmpty
}
tmpl := template.New("tmpl").Funcs(sprig.TxtFuncMap())
_, err := tmpl.Parse(iga.Template)
if err != nil {
return err
}
snapshot := dom.DefaultNodeMappingFn(ctx.Data())
matches, err := filepath.Glob(iga.Glob)
if err != nil {
return err
}
for _, match := range matches {
var out bytes.Buffer
err = tmpl.Execute(&out, map[string]interface{}{
"Data": snapshot,
"File": match,
})
if err != nil {
return err
}
val, err := parseFile(match, iga.Mode)
if err != nil {
return err
}
ctx.Data().AddValueAt(out.String(), val)
}
return nil
}

func (ps *PatchAction) String() string {
return fmt.Sprintf("Patch[Op=%s,Path=%s]", ps.Op, ps.Path)
}

func (ps *PatchAction) Perform(ctx ActionContext) error {
oo := &patch.OpObj{
Op: ps.Op,
}
path, err := patch.ParsePath(ps.Path)
if err != nil {
return err
}
oo.Path = path
oo.Value = b.FromMap(ps.Value)
if len(ps.From) > 0 {
from, err := patch.ParsePath(ps.From)
if err != nil {
return err
}
oo.From = &from
}
return patch.Do(oo, ctx.Data())
}

func (sa *SetAction) String() string {
return fmt.Sprintf("Set[Path=%s]", sa.Path)
}

func (sa *SetAction) Perform(ctx ActionContext) error {
gd := ctx.Data()
if sa.Data == nil {
return ErrNoDataToSet
}
data := ctx.Factory().FromMap(sa.Data)
if len(sa.Path) > 0 {
gd.AddValueAt(sa.Path, data)
} else {
for k, v := range data.Children() {
gd.AddValueAt(k, v)
}
}
return nil
}

func (ts *TemplateAction) String() string {
return fmt.Sprintf("Template[WriteTo=%s]", ts.WriteTo)
}

func (ts *TemplateAction) Perform(ctx ActionContext) error {
if len(ts.Template) == 0 {
return ErrTemplateEmpty
}
if len(ts.WriteTo) == 0 {
return ErrWriteToEmpty
}
tmpl := template.New("tmpl").Funcs(sprig.TxtFuncMap())
_, err := tmpl.Parse(ts.Template)
if err != nil {
return err
}
var out bytes.Buffer
err = tmpl.Execute(&out, map[string]interface{}{
"Data": dom.DefaultNodeMappingFn(ctx.Data()),
})
ctx.Data().AddValueAt(ts.WriteTo, dom.LeafNode(out.String()))
return err
}
Loading

0 comments on commit aa571fa

Please sign in to comment.