Skip to content

Commit 5baea07

Browse files
committed
Pipeline: Add 'Log' operation
This allows arbitrary messages to be logged during pipeline execution Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
1 parent 3803ae8 commit 5baea07

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

pipeline/foreach_op_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestForeachStringItem(t *testing.T) {
5151
Program: "sh",
5252
Args: &[]string{"-c", "rm /tmp/a-{{ .forEach }}.yaml"},
5353
},
54+
Log: &LogOp{
55+
Message: "Hi {{ .forEach }}",
56+
},
5457
},
5558
}
5659
d := b.Container()

pipeline/log_op.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2024 Richard Kosegi
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pipeline
18+
19+
import (
20+
"fmt"
21+
)
22+
23+
// LogOp just logs message to logger
24+
type LogOp struct {
25+
Message string `yaml:"message"`
26+
}
27+
28+
func (lo *LogOp) Do(ctx ActionContext) error {
29+
ctx.Logger().Log(ctx.TemplateEngine().RenderLenient(lo.Message, ctx.Snapshot()))
30+
return nil
31+
}
32+
33+
func (lo *LogOp) String() string {
34+
return fmt.Sprintf("Log[message=%s]", lo.Message)
35+
}
36+
37+
func (lo *LogOp) CloneWith(ctx ActionContext) Action {
38+
return &LogOp{
39+
Message: ctx.TemplateEngine().RenderLenient(lo.Message, ctx.Snapshot()),
40+
}
41+
}

pipeline/log_op_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2024 Richard Kosegi
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pipeline
18+
19+
import (
20+
"github.com/rkosegi/yaml-toolkit/dom"
21+
"github.com/stretchr/testify/assert"
22+
"testing"
23+
)
24+
25+
func TestLogOpDo(t *testing.T) {
26+
eo := &LogOp{}
27+
assert.NoError(t, eo.Do(mockEmptyActCtx()))
28+
}
29+
30+
func TestLogOpCloneWith(t *testing.T) {
31+
eo := &LogOp{
32+
Message: "Output format: {{ .Format }}",
33+
}
34+
assert.Contains(t, eo.String(), "Log[")
35+
d := b.Container()
36+
d.AddValue("Format", dom.LeafNode("toml"))
37+
eo = eo.CloneWith(mockActCtx(d)).(*LogOp)
38+
assert.Equal(t, "Output format: toml", eo.Message)
39+
}

pipeline/op_spec.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type OpSpec struct {
4646
// ForEach execute same operation in a loop for every configured item
4747
ForEach *ForEachOp `yaml:"forEach,omitempty"`
4848

49+
// Log logs arbitrary message to logger
50+
Log *LogOp `yaml:"log,omitempty"`
51+
4952
// Abort is able to signal error, so that pipeline can abort execution
5053
Abort *AbortOp `yaml:"abort,omitempty"`
5154
}
@@ -76,6 +79,9 @@ func (as OpSpec) toList() []Action {
7679
if as.ForEach != nil {
7780
actions = append(actions, as.ForEach)
7881
}
82+
if as.Log != nil {
83+
actions = append(actions, as.Log)
84+
}
7985
if as.Abort != nil {
8086
actions = append(actions, as.Abort)
8187
}
@@ -118,6 +124,9 @@ func (as OpSpec) CloneWith(ctx ActionContext) Action {
118124
if as.Exec != nil {
119125
r.Exec = as.Exec.CloneWith(ctx).(*ExecOp)
120126
}
127+
if as.Log != nil {
128+
r.Log = as.Log.CloneWith(ctx).(*LogOp)
129+
}
121130
if as.Abort != nil {
122131
r.Abort = as.Abort.CloneWith(ctx).(*AbortOp)
123132
}
@@ -146,6 +155,9 @@ func (as OpSpec) String() string {
146155
if as.Import != nil {
147156
parts = append(parts, fmt.Sprintf("Import=%v", as.Import.String()))
148157
}
158+
if as.Log != nil {
159+
parts = append(parts, fmt.Sprintf("Log=%v", as.Log.String()))
160+
}
149161
if as.Patch != nil {
150162
parts = append(parts, fmt.Sprintf("Patch=%v", as.Patch.String()))
151163
}

pipeline/op_spec_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func TestOpSpecCloneWith(t *testing.T) {
5454
Path: "{{ .Path }}",
5555
Format: OutputFormatYaml,
5656
},
57+
Log: &LogOp{
58+
Message: "Path: {{ .Path }}",
59+
},
5760
Abort: &AbortOp{
5861
Message: "abort",
5962
},

0 commit comments

Comments
 (0)