Skip to content

Commit

Permalink
[TechDebt] Switching Logger away from Logrus to Core Language Logger (#…
Browse files Browse the repository at this point in the history
…222)

ChangeLog:
  - Converting to using log/slog instead logrus
  - Updating importer.yml example
  • Loading branch information
safaci2000 committed Nov 7, 2023
1 parent 6b7b922 commit 91cc966
Show file tree
Hide file tree
Showing 59 changed files with 698 additions and 815 deletions.
33 changes: 20 additions & 13 deletions cmd/backup/alertnotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"log/slog"
)

func newAlertNotificationsCommand() simplecobra.Commander {
Expand Down Expand Up @@ -44,18 +44,19 @@ func newClearAlertNotificationsCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
slog.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})

log.Infof("Clearing all alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
slog.Info("Clearing all alert notification channels for context",
"context", config.Config().AppConfig.GetContext())
deleted := rootCmd.GrafanaSvc().DeleteAllAlertNotifications()
for _, item := range deleted {
rootCmd.TableObj.AppendRow(table.Row{"alertnotification", item})
}
if len(deleted) == 0 {
log.Info("No alert notification channels were found. 0 removed")
slog.Info("No alert notification channels were found. 0 removed")
} else {
log.Infof("%d alert notification channels were deleted", len(deleted))
slog.Info("alert notification channels were deleted", "count", len(deleted))
rootCmd.TableObj.Render()
}
return nil
Expand All @@ -73,10 +74,11 @@ func newUploadAlertNotificationsCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
slog.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
rootCmd.TableObj.AppendHeader(table.Row{"name", "id", "UID"})

log.Infof("Exporting alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
slog.Info("Exporting alert notification channels for context",
"context", config.Config().AppConfig.GetContext())
rootCmd.GrafanaSvc().UploadAlertNotifications()
items := rootCmd.GrafanaSvc().ListAlertNotifications()
for _, item := range items {
Expand All @@ -85,7 +87,7 @@ func newUploadAlertNotificationsCmd() simplecobra.Commander {
if len(items) > 0 {
rootCmd.TableObj.Render()
} else {
log.Info("No alert notification channels found")
slog.Info("No alert notification channels found")
}
return nil
},
Expand All @@ -102,10 +104,12 @@ func newDownloadAlertNotificationsCmd() simplecobra.Commander {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
slog.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})

log.Infof("Downloading alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
slog.Info("Downloading alert notification channels for context",
"context", config.Config().AppConfig.GetContext())

savedFiles := rootCmd.GrafanaSvc().DownloadAlertNotifications()
for _, file := range savedFiles {
rootCmd.TableObj.AppendRow(table.Row{"alertnotification", file})
Expand All @@ -126,13 +130,16 @@ func newListAlertNotificationsCmd() simplecobra.Commander {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
slog.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")

rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "slug", "type", "default", "url"})
alertnotifications := rootCmd.GrafanaSvc().ListAlertNotifications()

log.Infof("Listing alert notifications channels for context: '%s'", config.Config().AppConfig.GetContext())
slog.Info("Listing alert notifications channels for context",
"context", config.Config().AppConfig.GetContext())

if len(alertnotifications) == 0 {
log.Info("No alert notifications found")
slog.Info("No alert notifications found")
} else {
for _, link := range alertnotifications {
url := fmt.Sprintf("%s/alerting/notification/%d/edit", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
Expand Down
22 changes: 12 additions & 10 deletions cmd/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"log/slog"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -46,13 +47,13 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
log.Infof("Listing Connection Permissions for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Listing Connection Permissions for context", "context", config.Config().GetAppConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "uid", "name", "slug", "type", "default", "url"})
connections := rootCmd.GrafanaSvc().ListConnectionPermissions(filters)
_ = connections

if len(connections) == 0 {
log.Info("No connections found")
slog.Info("No connections found")
} else {
for link, perms := range connections {
url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
Expand All @@ -79,7 +80,7 @@ func newConnectionsPermissionClearCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("Clear all connections permissions")
slog.Info("Clear all connections permissions")
tools.GetUserConfirmation(fmt.Sprintf("WARNING: this will clear all permission from all connections on: '%s' "+
"(Or all permission matching yoru --connection filter). Do you wish to continue (y/n) ", config.Config().GetAppConfig().ContextName,
), "", true)
Expand All @@ -89,7 +90,7 @@ func newConnectionsPermissionClearCmd() simplecobra.Commander {
connections := rootCmd.GrafanaSvc().DeleteAllConnectionPermissions(filters)

if len(connections) == 0 {
log.Info("No connections found")
slog.Info("No connections found")
} else {
for _, connections := range connections {
rootCmd.TableObj.AppendRow(table.Row{connections})
Expand All @@ -112,15 +113,16 @@ func newConnectionsPermissionDownloadCmd() simplecobra.Commander {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("import Connections for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("import Connections for context",
"context", config.Config().GetAppConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"filename"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
connections := rootCmd.GrafanaSvc().DownloadConnectionPermissions(filters)
log.Infof("Downloading connections permissions")
slog.Info("Downloading connections permissions")

if len(connections) == 0 {
log.Info("No connections found")
slog.Info("No connections found")
} else {
for _, connections := range connections {
rootCmd.TableObj.AppendRow(table.Row{connections})
Expand All @@ -141,14 +143,14 @@ func newConnectionsPermissionUploadCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("Uploading connections permissions")
slog.Info("Uploading connections permissions")
rootCmd.TableObj.AppendHeader(table.Row{"connection permission"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
connections := rootCmd.GrafanaSvc().UploadConnectionPermissions(filters)

if len(connections) == 0 {
log.Info("No connections found")
slog.Info("No connections found")
} else {
for _, connections := range connections {
rootCmd.TableObj.AppendRow(table.Row{connections})
Expand Down
14 changes: 8 additions & 6 deletions cmd/backup/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"log/slog"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func newClearConnectionsCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Info("Delete connections")
slog.Info("Delete connections")
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("datasource")
filters := service.NewConnectionFilter(dashboardFilter)
savedFiles := rootCmd.GrafanaSvc().DeleteAllConnections(filters)
Expand All @@ -70,7 +71,7 @@ func newUploadConnectionsCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Info("Uploading connections")
slog.Info("Uploading connections")
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
exportedList := rootCmd.GrafanaSvc().UploadConnections(filters)
Expand All @@ -94,7 +95,8 @@ func newDownloadConnectionsCmd() simplecobra.Commander {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("Importing connections for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Importing connections for context",
"context", config.Config().GetAppConfig().GetContext())
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
savedFiles := rootCmd.GrafanaSvc().DownloadConnections(filters)
Expand All @@ -121,9 +123,9 @@ func newListConnectionsCmd() simplecobra.Commander {
dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(dashboardFilter)
dsListing := rootCmd.GrafanaSvc().ListConnections(filters)
log.Infof("Listing connections for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Listing connections for context", "context", config.Config().GetAppConfig().GetContext())
if len(dsListing) == 0 {
log.Info("No connections found")
slog.Info("No connections found")
} else {
for _, link := range dsListing {
url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
Expand Down
15 changes: 8 additions & 7 deletions cmd/backup/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"log/slog"
"strings"
)

Expand Down Expand Up @@ -69,10 +69,10 @@ func newClearDashboardsCmd() simplecobra.Commander {
rootCmd.TableObj.AppendRow(table.Row{"dashboard", file})
}
if len(deletedDashboards) == 0 {
log.Info("No dashboards were found. 0 dashboards were removed")
slog.Info("No dashboards were found. 0 dashboards were removed")

} else {
log.Infof("%d dashboards were deleted", len(deletedDashboards))
slog.Info("dashboards were deleted", "count", len(deletedDashboards))
rootCmd.TableObj.Render()
}
return nil
Expand Down Expand Up @@ -112,7 +112,7 @@ func newUploadDashboardsCmd() simplecobra.Commander {
if len(boards) > 0 {
rootCmd.TableObj.Render()
} else {
log.Info("No dashboards found")
slog.Info("No dashboards found")
}
return nil
},
Expand All @@ -133,7 +133,8 @@ func newDownloadDashboardsCmd() simplecobra.Commander {
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
filter := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
savedFiles := rootCmd.GrafanaSvc().DownloadDashboards(filter)
log.Infof("Downloading dashboards for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Downloading dashboards for context",
"context", config.Config().GetAppConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
for _, file := range savedFiles {
rootCmd.TableObj.AppendRow(table.Row{"dashboard", file})
Expand All @@ -159,7 +160,7 @@ func newListDashboardsCmd() simplecobra.Commander {
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
boards := rootCmd.GrafanaSvc().ListDashboards(filters)

log.Infof("Listing dashboards for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Listing dashboards for context", "context", config.Config().GetAppConfig().GetContext())
for _, link := range boards {
url := fmt.Sprintf("%s%s", config.Config().GetDefaultGrafanaConfig().URL, link.URL)
rootCmd.TableObj.AppendRow(table.Row{link.ID, link.Title, link.Slug, link.FolderTitle,
Expand All @@ -169,7 +170,7 @@ func newListDashboardsCmd() simplecobra.Commander {
if len(boards) > 0 {
rootCmd.TableObj.Render()
} else {
log.Info("No dashboards found")
slog.Info("No dashboards found")
}
return nil
},
Expand Down
16 changes: 8 additions & 8 deletions cmd/backup/folder_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/esnet/gdg/cmd/support"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"log/slog"
)

func newFolderPermissionCommand() simplecobra.Commander {
Expand Down Expand Up @@ -42,12 +42,12 @@ func newFolderPermissionListCmd() simplecobra.Commander {
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}

log.Infof("Listing Folders for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Listing Folders for context", "context", config.Config().GetAppConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"folder ID", "folderUid", "folder Name", "UserID", "Team Name", "Role", "Permission Name"}, rowConfigAutoMerge)
folders := rootCmd.GrafanaSvc().ListFolderPermissions(getFolderFilter())

if len(folders) == 0 {
log.Info("No folders found")
slog.Info("No folders found")
return nil
}
for key, value := range folders {
Expand All @@ -71,13 +71,13 @@ func newFolderPermissionDownloadCmd() simplecobra.Commander {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("Downloading Folder Permissions for context: '%s'", config.Config().GetAppConfig().GetContext())
slog.Info("Downloading Folder Permissions for context", "context", config.Config().GetAppConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"filename"})
folders := rootCmd.GrafanaSvc().DownloadFolderPermissions(getFolderFilter())
log.Infof("Downloading folder permissions")
slog.Info("Downloading folder permissions")

if len(folders) == 0 {
log.Info("No folders found")
slog.Info("No folders found")
return nil
}
for _, folder := range folders {
Expand All @@ -98,12 +98,12 @@ func newFolderPermissionUploadCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
log.Infof("Uploading folder permissions")
slog.Info("Uploading folder permissions")
rootCmd.TableObj.AppendHeader(table.Row{"file name"})
folders := rootCmd.GrafanaSvc().UploadFolderPermissions(getFolderFilter())

if len(folders) == 0 {
log.Info("No folders found")
slog.Info("No folders found")
return nil
}
for _, folder := range folders {
Expand Down
Loading

0 comments on commit 91cc966

Please sign in to comment.