Skip to content

Commit 0d7c981

Browse files
committed
add yaml formatter
bump version
1 parent f21e52a commit 0d7c981

File tree

5 files changed

+116
-17
lines changed

5 files changed

+116
-17
lines changed

cmd/debug.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
1515
package cmd
1616

1717
import (
18+
"strings"
19+
1820
"github.com/anduintransaction/rivendell/project"
21+
"github.com/anduintransaction/rivendell/project/formatters"
1922
"github.com/anduintransaction/rivendell/utils"
2023
"github.com/spf13/cobra"
2124
)
2225

26+
var (
27+
outputFormat string
28+
filterGroups []string
29+
)
30+
2331
// debugCmd represents the debug command
2432
var debugCmd = &cobra.Command{
2533
Use: "debug [project file]",
@@ -31,10 +39,25 @@ var debugCmd = &cobra.Command{
3139
if err != nil {
3240
utils.Fatal(err)
3341
}
34-
p.Debug()
42+
43+
var formatter project.Formatter
44+
switch strings.ToLower(outputFormat) {
45+
case "console":
46+
formatter = formatters.NewConsoleFormatter()
47+
case "yaml":
48+
formatter = formatters.NewYamlFormatter()
49+
default:
50+
utils.Warn("Unknown output formatter. Fallback to console")
51+
formatter = formatters.NewConsoleFormatter()
52+
}
53+
54+
p.Debug(formatter, filterGroups)
3555
},
3656
}
3757

3858
func init() {
3959
RootCmd.AddCommand(debugCmd)
60+
61+
debugCmd.Flags().StringVarP(&outputFormat, "output", "o", "console", "print format. One of: console|yaml")
62+
debugCmd.Flags().StringSliceVar(&filterGroups, "filter-groups", []string{}, "Only print resource groups")
4063
}

project/formatters/console.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package formatters
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/anduintransaction/rivendell/project"
7+
"github.com/anduintransaction/rivendell/utils"
8+
)
9+
10+
type ConsoleFormatter struct{}
11+
12+
func NewConsoleFormatter() *ConsoleFormatter {
13+
return &ConsoleFormatter{}
14+
}
15+
16+
func (f *ConsoleFormatter) Format(p *project.Project, filterGroups []string) {
17+
m := utils.StringSliceToMap(filterGroups)
18+
p.PrintCommonInfo()
19+
p.WalkForward(func(g *project.ResourceGroup) error {
20+
if len(m) > 0 && !m[g.Name] {
21+
return nil
22+
}
23+
24+
utils.Info("Resource group %q", g.Name)
25+
for _, rf := range g.ResourceFiles {
26+
utils.Info2("Resource file %q", rf.FilePath)
27+
fmt.Println()
28+
for _, r := range rf.Resources {
29+
fmt.Println(r.RawContent)
30+
fmt.Println()
31+
}
32+
}
33+
fmt.Println()
34+
return nil
35+
})
36+
}

project/formatters/yaml.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package formatters
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/anduintransaction/rivendell/project"
8+
"github.com/anduintransaction/rivendell/utils"
9+
)
10+
11+
type YamlFormatter struct{}
12+
13+
func NewYamlFormatter() *YamlFormatter {
14+
return &YamlFormatter{}
15+
}
16+
17+
func (f *YamlFormatter) Format(p *project.Project, filterGroups []string) {
18+
m := utils.StringSliceToMap(filterGroups)
19+
out := os.Stdout
20+
sep := "---"
21+
22+
p.WalkForward(func(g *project.ResourceGroup) error {
23+
if len(m) > 0 && !m[g.Name] {
24+
return nil
25+
}
26+
27+
for _, rf := range g.ResourceFiles {
28+
for _, r := range rf.Resources {
29+
fmt.Fprintf(out, "# Resource group %q - Resource file %q\n", g.Name, rf.FilePath)
30+
fmt.Fprintln(out)
31+
fmt.Fprintln(out, r.RawContent)
32+
fmt.Fprintln(out, sep)
33+
}
34+
}
35+
return nil
36+
})
37+
}

project/project.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const (
1717
waitCount = 10
1818
)
1919

20+
type Formatter interface {
21+
Format(p *Project, filterGroups []string)
22+
}
23+
2024
// Project holds configuration for a rivendell task
2125
type Project struct {
2226
rootDir string
@@ -54,21 +58,8 @@ func ReadProject(projectFile, namespace, context, kubeConfig string, variables m
5458
}
5559

5660
// Debug .
57-
func (p *Project) Debug() {
58-
p.PrintCommonInfo()
59-
p.resourceGraph.WalkForward(func(g *ResourceGroup) error {
60-
utils.Info("Resource group %q", g.Name)
61-
for _, rf := range g.ResourceFiles {
62-
utils.Info2("Resource file %q", rf.FilePath)
63-
fmt.Println()
64-
for _, r := range rf.Resources {
65-
fmt.Println(r.RawContent)
66-
fmt.Println()
67-
}
68-
}
69-
fmt.Println()
70-
return nil
71-
})
61+
func (p *Project) Debug(f Formatter, filterGroups []string) {
62+
f.Format(p, filterGroups)
7263
}
7364

7465
// Up .
@@ -217,6 +208,10 @@ func (p *Project) PrintRestartPlan(pods []string) {
217208
}
218209
}
219210

211+
func (p *Project) WalkForward(fn func(g *ResourceGroup) error) error {
212+
return p.resourceGraph.WalkForward(fn)
213+
}
214+
220215
func (p *Project) resolveProjectRoot(projectFile, configRoot string) {
221216
projectFileDirname := filepath.Dir(projectFile)
222217
p.rootDir = filepath.Join(projectFileDirname, configRoot)

utils/common.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Version of rivendell
15-
var Version = "1.1.0"
15+
var Version = "1.1.1"
1616

1717
// MergeMaps merges multiple maps into one
1818
func MergeMaps(maps ...map[string]string) map[string]string {
@@ -80,3 +80,11 @@ func ExpandEnv(s string) string {
8080
return os.Getenv(envName)
8181
})
8282
}
83+
84+
func StringSliceToMap(s []string) map[string]bool {
85+
m := make(map[string]bool)
86+
for _, it := range s {
87+
m[it] = true
88+
}
89+
return m
90+
}

0 commit comments

Comments
 (0)