Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tag filtering #239

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/code_scanners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Source
uses: actions/checkout@v3
- uses: actions/setup-go@v4
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.21.5"
cache: false
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go.version }}
- name: Verify go version
run: go version
- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hugo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
deployHugoPages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Log in to Docker Hub
Expand Down
45 changes: 29 additions & 16 deletions cli/backup/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@ package backup

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/url"
"strings"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"log/slog"
"net/url"
"strings"
)

var skipConfirmAction bool

func parseDashboardGlobalFlags(command *cobra.Command) []string {
folderFilter, _ := command.Flags().GetString("folder")
dashboardFilter, _ := command.Flags().GetString("dashboard")
tagsFilter, _ := command.Flags().GetStringSlice("tags")
tagsFilter, _ := command.Flags().GetStringArray("tags")
rawTags, err := json.Marshal(tagsFilter)
jsonTags := ""
if err == nil {
jsonTags = string(rawTags)
}

return []string{folderFilter, dashboardFilter, strings.Join(tagsFilter, ",")}
return []string{folderFilter, dashboardFilter, jsonTags}
}

func newDashboardCommand() simplecobra.Commander {
Expand All @@ -37,7 +44,7 @@ func newDashboardCommand() simplecobra.Commander {
dashboard.PersistentFlags().BoolVarP(&skipConfirmAction, "skip-confirmation", "", false, "when set to true, bypass confirmation prompts")
dashboard.PersistentFlags().StringP("dashboard", "d", "", "filter by dashboard slug")
dashboard.PersistentFlags().StringP("folder", "f", "", "Filter by Folder Name (Quotes in names not supported)")
dashboard.PersistentFlags().StringSliceP("tags", "t", []string{}, "Filter by Tags (does not apply on upload)")
dashboard.PersistentFlags().StringArrayP("tags", "t", []string{}, "Filter by list of comma delimited tags")
},
CommandsList: []simplecobra.Commander{
newListDashboardsCmd(),
Expand All @@ -49,7 +56,6 @@ func newDashboardCommand() simplecobra.Commander {
return cd.CobraCommand.Help()
},
}

}

func newClearDashboardsCmd() simplecobra.Commander {
Expand All @@ -71,15 +77,13 @@ func newClearDashboardsCmd() simplecobra.Commander {
}
if len(deletedDashboards) == 0 {
slog.Info("No dashboards were found. 0 dashboards were removed")

} else {
slog.Info("dashboards were deleted", "count", len(deletedDashboards))
rootCmd.TableObj.Render()
}
return nil
},
}

}

func newUploadDashboardsCmd() simplecobra.Commander {
Expand All @@ -90,7 +94,7 @@ func newUploadDashboardsCmd() simplecobra.Commander {
Long: description,
CommandsList: []simplecobra.Commander{},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"u"}
cmd.Aliases = []string{"u", "up"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
filter := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
Expand All @@ -106,9 +110,9 @@ func newUploadDashboardsCmd() simplecobra.Commander {
rootCmd.TableObj.AppendHeader(table.Row{"Title", "id", "folder", "UID"})
boards := rootCmd.GrafanaSvc().ListDashboards(filter)

slog.Info(fmt.Sprintf("%d dashboards have been uploaded", len(boards)))
for _, link := range boards {
rootCmd.TableObj.AppendRow(table.Row{link.Title, link.ID, link.FolderTitle, link.UID})

}
if len(boards) > 0 {
rootCmd.TableObj.Render()
Expand All @@ -118,7 +122,6 @@ func newUploadDashboardsCmd() simplecobra.Commander {
return nil
},
}

}

func newDownloadDashboardsCmd() simplecobra.Commander {
Expand All @@ -145,6 +148,7 @@ func newDownloadDashboardsCmd() simplecobra.Commander {
},
}
}

func newListDashboardsCmd() simplecobra.Commander {
description := "List all dashboards from grafana"
return &support.SimpleCommand{
Expand All @@ -161,7 +165,7 @@ func newListDashboardsCmd() simplecobra.Commander {
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
boards := rootCmd.GrafanaSvc().ListDashboards(filters)

slog.Info("Listing dashboards for context", "context", config.Config().GetGDGConfig().GetContext())
slog.Info("Listing dashboards for context", slog.String("context", config.Config().GetGDGConfig().GetContext()), slog.Any("count", len(boards)))
for _, link := range boards {
base, err := url.Parse(config.Config().GetDefaultGrafanaConfig().URL)
var baseHost string
Expand All @@ -173,8 +177,18 @@ func newListDashboardsCmd() simplecobra.Commander {
baseHost = base.String()
}
urlValue := fmt.Sprintf("%s%s", baseHost, link.URL)
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.Title, link.Slug, link.FolderTitle,
link.UID, strings.Join(link.Tags, ","), urlValue})
var tagVal string
if len(link.Tags) > 0 {
tagValByte, err := json.Marshal(link.Tags)
if err == nil {
tagVal = string(tagValByte)
}
}

rootCmd.TableObj.AppendRow(table.Row{
link.ID, link.Title, link.Slug, link.FolderTitle,
link.UID, tagVal, urlValue,
})

}
if len(boards) > 0 {
Expand All @@ -185,5 +199,4 @@ func newListDashboardsCmd() simplecobra.Commander {
return nil
},
}

}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/aws/aws-sdk-go v1.49.13
github.com/bep/simplecobra v0.4.0
github.com/carlmjohnson/requests v0.23.5
github.com/go-openapi/runtime v0.26.2
github.com/go-openapi/strfmt v0.22.0
github.com/google/uuid v1.5.0
github.com/gosimple/slug v1.13.1
Expand Down Expand Up @@ -89,6 +88,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/loads v0.21.5 // indirect
github.com/go-openapi/runtime v0.26.2 // indirect
github.com/go-openapi/spec v0.20.13 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/validate v0.22.6 // indirect
Expand Down Expand Up @@ -126,7 +126,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.10 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
Expand Down Expand Up @@ -176,5 +176,4 @@ require (
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading