-
Notifications
You must be signed in to change notification settings - Fork 8
/
stdout.go
51 lines (41 loc) · 1.05 KB
/
stdout.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
package outputs
import (
"context"
"fmt"
"github.com/camptocamp/prometheus-puppetdb-sd/internal/config"
"github.com/camptocamp/prometheus-puppetdb-sd/internal/types"
yaml "gopkg.in/yaml.v1"
)
// StdoutOutput stores values needed to print output to stdout
type StdoutOutput struct {
format config.OutputFormat
}
func setupStdoutOutput(cfg *config.OutputConfig) (*StdoutOutput, error) {
return &StdoutOutput{
format: cfg.Format,
}, nil
}
// WriteOutput writes Prometheus configuration to stdout
func (o *StdoutOutput) WriteOutput(ctx context.Context, scrapeConfigs []*types.ScrapeConfig) (err error) {
var c []byte
switch o.format {
case config.ScrapeConfigs:
c, err = yaml.Marshal(scrapeConfigs)
if err != nil {
return
}
fmt.Printf("%s", string(c))
case config.MergedStaticConfigs:
for _, scrapeConfig := range scrapeConfigs {
c, err = yaml.Marshal(scrapeConfig.StaticConfigs)
if err != nil {
return
}
fmt.Printf("%s", string(c))
}
default:
err = fmt.Errorf("unexpected output format '%s'", o.format)
return
}
return
}