From 45f411c69ded180331f5c7b2fd8910fa83516679 Mon Sep 17 00:00:00 2001 From: Christophe Lambin Date: Fri, 16 Aug 2024 22:25:18 +0200 Subject: [PATCH] build: go 1.23 --- .github/workflows/release.yml | 4 +-- .github/workflows/test.yml | 2 +- .github/workflows/vulnerabilities.yaml | 2 +- go.mod | 4 +-- internal/fetcher.go | 38 ++++++++++++++-------- internal/formatter.go | 45 ++++++++++++++++---------- 6 files changed, 57 insertions(+), 38 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4a672db..e0c1cf3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,10 +10,10 @@ permissions: jobs: test: - uses: clambin/workflows/.github/workflows/test.yaml@main + uses: clambin/workflows/.github/workflows/test.yaml@go1.23 secrets: codecov-token: ${{ secrets.CODECOV_TOKEN }} release: needs: - test - uses: clambin/workflows/.github/workflows/release.yaml@docker + uses: clambin/workflows/.github/workflows/release.yaml@go1.23 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 10cf454..a1ec82f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ on: jobs: test: - uses: clambin/workflows/.github/workflows/test.yaml@main + uses: clambin/workflows/.github/workflows/test.yaml@go1.23 secrets: codecov-token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/vulnerabilities.yaml b/.github/workflows/vulnerabilities.yaml index faf8dc7..b516699 100644 --- a/.github/workflows/vulnerabilities.yaml +++ b/.github/workflows/vulnerabilities.yaml @@ -7,4 +7,4 @@ on: jobs: vulnerabilities: - uses: clambin/workflows/.github/workflows/vulnerabilities.yaml@main + uses: clambin/workflows/.github/workflows/vulnerabilities.yaml@go1.23 diff --git a/go.mod b/go.mod index dc521d4..771062d 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/clambin/grope -go 1.22.1 - -toolchain go1.22.5 +go 1.23 require ( github.com/clambin/go-common/charmer v0.2.0 diff --git a/internal/fetcher.go b/internal/fetcher.go index 58bd463..d2ace8f 100644 --- a/internal/fetcher.go +++ b/internal/fetcher.go @@ -3,6 +3,7 @@ package internal import ( "fmt" gapi "github.com/grafana/grafana-api-golang-client" + "iter" "log/slog" ) @@ -30,27 +31,36 @@ func FetchDashboards(c DashboardClient, logger *slog.Logger, shouldExport func(g } dashboards := make(Dashboards, 0, len(foundBoards)) - for _, board := range foundBoards { + for board := range dashboardsToExport(foundBoards, shouldExport) { logger.Debug("dashboard found", "data", folderDashboard(board)) - - // Only process dashboards, not folders - // Only export if the dashboard meets the criteria - if board.Type == "dash-db" && shouldExport(board) { - rawBoard, err := c.DashboardByUID(board.UID) - if err != nil { - return nil, fmt.Errorf("grafana get board: %w", err) - } - dashboards = append(dashboards, Dashboard{ - Folder: board.FolderTitle, - Title: board.Title, - Model: rawBoard.Model, - }) + rawBoard, err := c.DashboardByUID(board.UID) + if err != nil { + return nil, fmt.Errorf("grafana get board: %w", err) } + dashboards = append(dashboards, Dashboard{ + Folder: board.FolderTitle, + Title: board.Title, + Model: rawBoard.Model, + }) } return dashboards, nil } +func dashboardsToExport(dashboards []gapi.FolderDashboardSearchResponse, shouldExport func(gapi.FolderDashboardSearchResponse) bool) iter.Seq[gapi.FolderDashboardSearchResponse] { + return func(yield func(gapi.FolderDashboardSearchResponse) bool) { + for _, board := range dashboards { + // Only process dashboards, not folders + // Only export if the dashboard meets the criteria + if board.Type == "dash-db" && shouldExport(board) { + if !yield(board) { + return + } + } + } + } +} + var _ slog.LogValuer = folderDashboard{} type folderDashboard gapi.FolderDashboardSearchResponse diff --git a/internal/formatter.go b/internal/formatter.go index 2dec448..454fc3d 100644 --- a/internal/formatter.go +++ b/internal/formatter.go @@ -9,6 +9,7 @@ import ( grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1" "gopkg.in/yaml.v3" "io" + "iter" ) type Formatter struct { @@ -75,23 +76,7 @@ func (f Formatter) FormatDashboard(w io.Writer, dashboard Dashboard) error { } func (f Formatter) FormatDataSources(w io.Writer, dataSources []*gapi.DataSource) error { - for _, dataSource := range dataSources { - cr := grafanaOperatorCustomResource{ - APIVersion: grafanav1beta1.GroupVersion.String(), - Kind: "GrafanaDataSource", - Metadata: metadata{ - Name: "datasource-" + slug.Make(dataSource.Name), - Namespace: f.Namespace, - }, - Spec: grafanaOperatorCustomResourceSpec{ - InstanceSelector: instanceSelector{ - MatchLabels: map[string]string{ - f.GrafanaLabelName: f.GrafanaLabelValue, - }, - }, - DataSource: dataSource, - }, - } + for cr := range f.grafanaOperatorCustomResources(dataSources) { _, _ = w.Write([]byte("---\n")) yEnc := yaml.NewEncoder(w) yEnc.SetIndent(2) @@ -101,3 +86,29 @@ func (f Formatter) FormatDataSources(w io.Writer, dataSources []*gapi.DataSource } return nil } + +func (f Formatter) grafanaOperatorCustomResources(dataSources []*gapi.DataSource) iter.Seq[grafanaOperatorCustomResource] { + return func(yield func(grafanaOperatorCustomResource) bool) { + for _, dataSource := range dataSources { + cr := grafanaOperatorCustomResource{ + APIVersion: grafanav1beta1.GroupVersion.String(), + Kind: "GrafanaDataSource", + Metadata: metadata{ + Name: "datasource-" + slug.Make(dataSource.Name), + Namespace: f.Namespace, + }, + Spec: grafanaOperatorCustomResourceSpec{ + InstanceSelector: instanceSelector{ + MatchLabels: map[string]string{ + f.GrafanaLabelName: f.GrafanaLabelValue, + }, + }, + DataSource: dataSource, + }, + } + if !yield(cr) { + return + } + } + } +}