Skip to content

Commit 1d35342

Browse files
committed
refactor: remove unnecessary abstraction
1 parent d61e8ed commit 1d35342

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

Diff for: go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/gosimple/slug v1.14.0
1111
github.com/grafana/grafana-api-golang-client v0.27.0
1212
github.com/grafana/grafana-operator/v5 v5.9.2
13-
github.com/spf13/cobra v1.8.0
13+
github.com/spf13/cobra v1.8.1
1414
github.com/spf13/viper v1.19.0
1515
github.com/stretchr/testify v1.9.0
1616
gopkg.in/yaml.v3 v3.0.1
@@ -19,6 +19,7 @@ require (
1919
require (
2020
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
2121
github.com/blang/semver v3.5.1+incompatible // indirect
22+
github.com/clambin/go-common/testutils v0.1.0 // indirect
2223
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2324
github.com/fsnotify/fsnotify v1.7.0 // indirect
2425
github.com/go-logr/logr v1.4.2 // indirect

Diff for: go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ github.com/clambin/go-common/set v0.4.3/go.mod h1:Q5GpBoM7B7abNV2Wzys+wQMInBHMoH
99
github.com/clambin/go-common/testutils v0.1.0 h1:/nGWaOCIhW+Ew1c2NU7GLY/YPb8dp9SV8+MTgWksAgk=
1010
github.com/clambin/go-common/testutils v0.1.0/go.mod h1:bV0j8D4zhNkleCeluFKLBeLQ0L/dqkxbaR/joLn8kzg=
1111
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
12+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1213
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1314
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1415
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -125,6 +126,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
125126
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
126127
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
127128
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
129+
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
130+
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
128131
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
129132
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
130133
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=

Diff for: internal/exporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func makeExporter(v *viper.Viper, l *slog.Logger) (exporter, error) {
3333
return exporter{
3434
logger: l,
3535
client: c,
36-
formatter: OperatorFormatter{
36+
formatter: Formatter{
3737
Namespace: stringOrDefault(v.GetString("namespace"), "default"),
3838
GrafanaLabelName: stringOrDefault(v.GetString("grafana.operator.label.name"), "dashboards"),
3939
GrafanaLabelValue: stringOrDefault(v.GetString("grafana.operator.label.value"), "grafana"),
File renamed without changes.

Diff for: internal/fetcher.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,23 @@ type Dashboard struct {
2323
Model map[string]any
2424
}
2525

26-
func FetchDashboards(c DashboardClient, logger *slog.Logger, shouldExport func(dashboard gapi.FolderDashboardSearchResponse) bool) (Dashboards, error) {
26+
func FetchDashboards(c DashboardClient, logger *slog.Logger, shouldExport func(gapi.FolderDashboardSearchResponse) bool) (Dashboards, error) {
2727
foundBoards, err := c.Dashboards()
2828
if err != nil {
2929
return nil, fmt.Errorf("grafana search: %w", err)
3030
}
3131

3232
dashboards := make(Dashboards, 0, len(foundBoards))
3333
for _, board := range foundBoards {
34-
logger.Debug("dashboard found",
35-
slog.String("title", board.Title),
36-
slog.String("type", board.Type),
37-
slog.String("folder", board.FolderTitle),
38-
)
34+
logger.Debug("dashboard found", "data", folderDashboard(board))
3935

4036
// Only process dashboards, not folders
4137
// Only export if the dashboard meets the criteria
4238
if board.Type == "dash-db" && shouldExport(board) {
43-
// Get the dashboard model
4439
rawBoard, err := c.DashboardByUID(board.UID)
4540
if err != nil {
4641
return nil, fmt.Errorf("grafana get board: %w", err)
4742
}
48-
4943
dashboards = append(dashboards, Dashboard{
5044
Folder: board.FolderTitle,
5145
Title: board.Title,
@@ -56,3 +50,14 @@ func FetchDashboards(c DashboardClient, logger *slog.Logger, shouldExport func(d
5650

5751
return dashboards, nil
5852
}
53+
54+
var _ slog.LogValuer = folderDashboard{}
55+
56+
type folderDashboard gapi.FolderDashboardSearchResponse
57+
58+
func (d folderDashboard) LogValue() slog.Value {
59+
return slog.GroupValue(slog.String("title", d.Title),
60+
slog.String("type", d.Type),
61+
slog.String("folder", d.FolderTitle),
62+
)
63+
}

Diff for: internal/fetcher_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package internal
2+
3+
import (
4+
"bytes"
5+
"github.com/clambin/go-common/testutils"
6+
gapi "github.com/grafana/grafana-api-golang-client"
7+
"github.com/stretchr/testify/assert"
8+
"log/slog"
9+
"testing"
10+
)
11+
12+
func Test_folderDashboard_LogValue(t *testing.T) {
13+
dashboard := gapi.FolderDashboardSearchResponse{FolderTitle: "folder", Title: "dashboard", Type: "db"}
14+
15+
var output bytes.Buffer
16+
l := testutils.NewTextLogger(&output, slog.LevelInfo)
17+
l.Info("dashboard found", "dashboard", folderDashboard(dashboard))
18+
assert.Equal(t, `level=INFO msg="dashboard found" dashboard.title=dashboard dashboard.type=db dashboard.folder=folder
19+
`, output.String())
20+
}

Diff for: internal/formatter.go

+7-14
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ import (
1111
"io"
1212
)
1313

14-
type Formatter interface {
15-
FormatDashboard(w io.Writer, dashboard Dashboard) error
16-
FormatDataSources(w io.Writer, sources []*gapi.DataSource) error
17-
}
18-
19-
var _ Formatter = OperatorFormatter{}
20-
21-
type OperatorFormatter struct {
14+
type Formatter struct {
2215
Namespace string
2316
GrafanaLabelName string
2417
GrafanaLabelValue string
@@ -49,7 +42,7 @@ type instanceSelector struct {
4942
MatchLabels map[string]string `yaml:"matchLabels"`
5043
}
5144

52-
func (o OperatorFormatter) FormatDashboard(w io.Writer, dashboard Dashboard) error {
45+
func (f Formatter) FormatDashboard(w io.Writer, dashboard Dashboard) error {
5346
var encodedDashboard bytes.Buffer
5447
jEnc := json.NewEncoder(&encodedDashboard)
5548
jEnc.SetIndent("", " ")
@@ -62,13 +55,13 @@ func (o OperatorFormatter) FormatDashboard(w io.Writer, dashboard Dashboard) err
6255
Kind: "GrafanaDashboard",
6356
Metadata: metadata{
6457
Name: slug.Make(dashboard.Title),
65-
Namespace: o.Namespace,
58+
Namespace: f.Namespace,
6659
},
6760
Spec: grafanaOperatorCustomResourceSpec{
6861
AllowCrossNamespaceImport: true,
6962
InstanceSelector: instanceSelector{
7063
MatchLabels: map[string]string{
71-
o.GrafanaLabelName: o.GrafanaLabelValue,
64+
f.GrafanaLabelName: f.GrafanaLabelValue,
7265
},
7366
},
7467
Folder: dashboard.Folder,
@@ -81,19 +74,19 @@ func (o OperatorFormatter) FormatDashboard(w io.Writer, dashboard Dashboard) err
8174
return yEnc.Encode(dashboardCR)
8275
}
8376

84-
func (o OperatorFormatter) FormatDataSources(w io.Writer, dataSources []*gapi.DataSource) error {
77+
func (f Formatter) FormatDataSources(w io.Writer, dataSources []*gapi.DataSource) error {
8578
for _, dataSource := range dataSources {
8679
cr := grafanaOperatorCustomResource{
8780
APIVersion: grafanav1beta1.GroupVersion.String(),
8881
Kind: "GrafanaDataSource",
8982
Metadata: metadata{
9083
Name: "datasource-" + slug.Make(dataSource.Name),
91-
Namespace: o.Namespace,
84+
Namespace: f.Namespace,
9285
},
9386
Spec: grafanaOperatorCustomResourceSpec{
9487
InstanceSelector: instanceSelector{
9588
MatchLabels: map[string]string{
96-
o.GrafanaLabelName: o.GrafanaLabelValue,
89+
f.GrafanaLabelName: f.GrafanaLabelValue,
9790
},
9891
},
9992
DataSource: dataSource,

0 commit comments

Comments
 (0)