-
Notifications
You must be signed in to change notification settings - Fork 18
/
stringoutput.go
55 lines (47 loc) · 1.22 KB
/
stringoutput.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package output
import (
"strings"
. "github.com/open-component-model/ocm/pkg/out"
"github.com/open-component-model/ocm/cmds/ocm/pkg/processing"
)
type StringOutput struct {
ElementOutput
linesep string
}
var _ Output = &StringOutput{}
func NewStringOutput(opts *Options, mapper processing.MappingFunction, linesep string) *StringOutput {
return (&StringOutput{}).new(opts, mapper, linesep)
}
func (this *StringOutput) new(opts *Options, mapper processing.MappingFunction, lineseperator string) *StringOutput {
this.linesep = lineseperator
this.ElementOutput.new(opts, processing.Chain(opts.LogContext()).Parallel(20).Map(mapper))
return this
}
func (this *StringOutput) Out() error {
var err error = nil
i := this.Elems.Iterator()
for i.HasNext() {
switch cfg := i.Next().(type) {
case error:
err = cfg
if this.linesep == "" {
Error(this.Context, err.Error())
} else {
Errf(this.Context, "%s\nError: %s\n", this.linesep, err)
}
case string:
if cfg != "" {
if this.linesep != "" {
if !strings.HasPrefix(cfg, this.linesep+"\n") {
Outln(this.Context, this.linesep)
}
}
Outln(this.Context, cfg)
}
}
}
if err != nil {
return err
}
return this.ElementOutput.Out()
}