diff --git a/cli/backup/connection_permissions.go b/cli/backup/connection_permissions.go index 00c65e4b..1b08025f 100644 --- a/cli/backup/connection_permissions.go +++ b/cli/backup/connection_permissions.go @@ -64,7 +64,7 @@ func newConnectionsPermissionListCmd() simplecobra.Commander { } } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, connections) } return nil }, @@ -95,7 +95,7 @@ func newConnectionsPermissionClearCmd() simplecobra.Commander { for _, connections := range connections { rootCmd.TableObj.AppendRow(table.Row{connections}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, connections) } return nil @@ -127,7 +127,7 @@ func newConnectionsPermissionDownloadCmd() simplecobra.Commander { for _, connections := range connections { rootCmd.TableObj.AppendRow(table.Row{connections}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, connections) } return nil }, @@ -155,7 +155,7 @@ func newConnectionsPermissionUploadCmd() simplecobra.Commander { for _, connections := range connections { rootCmd.TableObj.AppendRow(table.Row{connections}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, connections) } return nil }, diff --git a/cli/backup/connections.go b/cli/backup/connections.go index 96ca1978..64398df7 100644 --- a/cli/backup/connections.go +++ b/cli/backup/connections.go @@ -55,7 +55,7 @@ func newClearConnectionsCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"datasource", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) return nil }, } @@ -79,7 +79,7 @@ func newUploadConnectionsCmd() simplecobra.Commander { for _, file := range exportedList { rootCmd.TableObj.AppendRow(table.Row{"datasource", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, exportedList) return nil }, } @@ -105,7 +105,7 @@ func newDownloadConnectionsCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"datasource", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) return nil }, } @@ -134,7 +134,7 @@ func newListConnectionsCmd() simplecobra.Commander { url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID) rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, dsListing) } return nil }, diff --git a/cli/backup/dashboard.go b/cli/backup/dashboard.go index 3bec0ceb..807a7f46 100644 --- a/cli/backup/dashboard.go +++ b/cli/backup/dashboard.go @@ -78,7 +78,7 @@ func newClearDashboardsCmd() simplecobra.Commander { slog.Info("No dashboards were found. 0 dashboards were removed") } else { slog.Info("dashboards were deleted", "count", len(deletedDashboards)) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, deletedDashboards) } return nil }, @@ -116,7 +116,7 @@ func newUploadDashboardsCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{link.Title, link.ID, link.FolderTitle, link.UID}) } if len(boards) > 0 { - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, boards) } else { slog.Info("No dashboards found") } @@ -145,7 +145,7 @@ func newDownloadDashboardsCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"dashboard", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) return nil }, } @@ -197,7 +197,7 @@ func newListDashboardsCmd() simplecobra.Commander { } if len(boards) > 0 { - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, boards) } else { slog.Info("No dashboards found") } diff --git a/cli/backup/folder_permissions.go b/cli/backup/folder_permissions.go index a12e41ad..6a57b82a 100644 --- a/cli/backup/folder_permissions.go +++ b/cli/backup/folder_permissions.go @@ -56,7 +56,7 @@ func newFolderPermissionListCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{"", "", " PERMISSION--->", entry.UserLogin, entry.Team, entry.Role, entry.PermissionName}, rowConfigAutoMerge) } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) return nil }, } @@ -83,7 +83,7 @@ func newFolderPermissionDownloadCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) return nil }, } @@ -109,7 +109,7 @@ func newFolderPermissionUploadCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) return nil }, } diff --git a/cli/backup/folders.go b/cli/backup/folders.go index e8880c64..324fd410 100644 --- a/cli/backup/folders.go +++ b/cli/backup/folders.go @@ -67,7 +67,7 @@ func newFolderClearCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) } return nil }, @@ -94,7 +94,7 @@ func newFolderListCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder.ID, folder.UID, folder.Title}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) } return nil }, @@ -119,7 +119,7 @@ func newFolderDownloadCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) } return nil }, @@ -144,7 +144,7 @@ func newFolderUploadCmd() simplecobra.Commander { for _, folder := range folders { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, folders) } return nil }, diff --git a/cli/backup/library.go b/cli/backup/library.go index 903dd0ba..ba488de9 100644 --- a/cli/backup/library.go +++ b/cli/backup/library.go @@ -56,7 +56,7 @@ func newLibraryElementsClearCmd() simplecobra.Commander { } else { slog.Info("libraries were deleted", "count", len(deletedLibrarys)) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, deletedLibrarys) } return nil }, @@ -82,7 +82,7 @@ func newLibraryElementsListCmd() simplecobra.Commander { } if len(elements) > 0 { - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, elements) } else { slog.Info("No library found") } @@ -107,7 +107,7 @@ func newLibraryElementsDownloadCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"library", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) return nil }, } @@ -130,7 +130,7 @@ func newLibraryElementsUploadCmd() simplecobra.Commander { for _, link := range elements { rootCmd.TableObj.AppendRow(table.Row{link}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, elements) } else { slog.Info("No library found") } @@ -162,7 +162,7 @@ func newLibraryElementsListConnectionsCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{dash["id"].(json.Number), dash["uid"].(string), link.Meta.Slug, dash["title"].(string), link.Meta.FolderTitle}) } if len(elements) > 0 { - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, elements) } else { slog.Info("No library found") } diff --git a/cli/backup/organizations.go b/cli/backup/organizations.go index afd6e896..f9a21f15 100644 --- a/cli/backup/organizations.go +++ b/cli/backup/organizations.go @@ -74,7 +74,7 @@ func newOrganizationsListCmd() simplecobra.Commander { org.Preferences.WeekStart, }) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, listOrganizations) } return nil }, @@ -101,7 +101,7 @@ func newOrganizationsDownloadCmd() simplecobra.Commander { for _, org := range listOrganizations { rootCmd.TableObj.AppendRow(table.Row{org}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, listOrganizations) } return nil }, @@ -121,14 +121,14 @@ func newOrganizationsUploadCmd() simplecobra.Commander { slog.Info("Uploading Folders for context: ", "context", config.Config().GetGDGConfig().GetContext()) rootCmd.TableObj.AppendHeader(table.Row{"file"}) filter := service.NewOrganizationFilter(parseOrganizationGlobalFlags(cd.CobraCommand)...) - folders := rootCmd.GrafanaSvc().UploadOrganizations(filter) - if len(folders) == 0 { - slog.Info("No Orgs were uploaded") + organizations := rootCmd.GrafanaSvc().UploadOrganizations(filter) + if len(organizations) == 0 { + slog.Info("No Organizations were uploaded") } else { - for _, folder := range folders { + for _, folder := range organizations { rootCmd.TableObj.AppendRow(table.Row{folder}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, organizations) } return nil }, diff --git a/cli/backup/team.go b/cli/backup/team.go index 25c552b0..3bab3573 100644 --- a/cli/backup/team.go +++ b/cli/backup/team.go @@ -73,7 +73,7 @@ func newTeamsListCmd() simplecobra.Commander { } } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, teams) } return nil }, @@ -102,7 +102,7 @@ func newTeamsDownloadCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{"", "", "", "", "", member.Login, getTeamPermission(member.Permission)}) } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -134,7 +134,7 @@ func newTeamsUploadCmd() simplecobra.Commander { } } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -160,7 +160,7 @@ func newTeamsClearCmd() simplecobra.Commander { for _, team := range teams { rootCmd.TableObj.AppendRow(table.Row{"team", team.ID, team.Name}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, teams) } return nil }, diff --git a/cli/backup/users.go b/cli/backup/users.go index 809f74f9..c27a5efa 100644 --- a/cli/backup/users.go +++ b/cli/backup/users.go @@ -65,7 +65,7 @@ func newUsersListCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{user.ID, user.Login, user.Name, user.Email, user.IsAdmin, user.IsDisabled, defaultPassword, labels}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, users) } return nil @@ -92,7 +92,7 @@ func newUsersDownloadCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"user", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -123,7 +123,7 @@ func newUsersUploadCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{user.ID, user.Login, user.Name, user.Email, user.IsGrafanaAdmin, user.IsDisabled, user.Password, labels}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -149,7 +149,7 @@ func newUsersClearCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"user", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, diff --git a/cli/support/root.go b/cli/support/root.go index ac1aa5cf..aa6f389d 100644 --- a/cli/support/root.go +++ b/cli/support/root.go @@ -2,11 +2,15 @@ package support import ( "context" + "encoding/json" "errors" + "fmt" "github.com/bep/simplecobra" appconfig "github.com/esnet/gdg/internal/log" "github.com/esnet/gdg/internal/service" "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "log" "os" ) @@ -32,6 +36,21 @@ type RootCommand struct { CommandEntries []simplecobra.Commander } +func (cmd *RootCommand) Render(command *cobra.Command, data interface{}) { + output, _ := command.Flags().GetString("output") + if output == "json" { + data, err := json.MarshalIndent(data, "", " ") + if err != nil { + log.Fatal("unable to render result to JSON", err) + } + fmt.Print(string(data)) + + } else { + cmd.TableObj.Render() + } + +} + // RootOption used to configure the Root Command struct type RootOption func(command *RootCommand) @@ -89,6 +108,7 @@ func (c *RootCommand) Init(cd *simplecobra.Commandeer) error { persistentFlags := cmd.PersistentFlags() persistentFlags.StringP("config", "c", "", "Configuration Override") + persistentFlags.StringP("output", "", "table", "output format: (table, json)") if c.TableObj == nil { c.TableObj = table.NewWriter() c.TableObj.SetOutputMirror(os.Stdout) diff --git a/cli/tools/auth_service_accounts.go b/cli/tools/auth_service_accounts.go index aa9aa215..aef8d0ff 100644 --- a/cli/tools/auth_service_accounts.go +++ b/cli/tools/auth_service_accounts.go @@ -71,7 +71,7 @@ func newListServiceAccountCmd() simplecobra.Commander { } } } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, apiKeys) } return nil @@ -107,7 +107,7 @@ func newDeleteServiceAccountTokensCmd() simplecobra.Commander { for _, token := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{id, "service token", token}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -131,7 +131,7 @@ func newDeleteServiceAccountCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"user", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -175,7 +175,8 @@ func newServiceAccount() simplecobra.Commander { rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "role"}) rootCmd.TableObj.AppendRow(table.Row{serviceAcct.ID, serviceAcct.Name, serviceAcct.Role}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, + map[string]interface{}{"id": serviceAcct.ID, "name": serviceAcct.Name, "role": serviceAcct.Role}) } return nil }, @@ -220,7 +221,11 @@ func newServiceAccountTokenCmd() simplecobra.Commander { rootCmd.TableObj.AppendHeader(table.Row{"serviceID", "token_id", "name", "token"}) rootCmd.TableObj.AppendRow(table.Row{serviceID, key.ID, key.Name, key.Key}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, + map[string]interface{}{"serviceID": serviceID, + "token_id": key.ID, + "name": key.Name, + "token": key.Key}) } return nil diff --git a/cli/tools/auth_tokens.go b/cli/tools/auth_tokens.go index 218e30ce..08e61b18 100644 --- a/cli/tools/auth_tokens.go +++ b/cli/tools/auth_tokens.go @@ -61,7 +61,7 @@ func newListTokensCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{apiKey.ID, apiKey.Name, apiKey.Role, formattedDate}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, apiKeys) } return nil }, @@ -86,7 +86,7 @@ func newDeleteTokenCmd() simplecobra.Commander { for _, file := range savedFiles { rootCmd.TableObj.AppendRow(table.Row{"user", file}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, savedFiles) } return nil }, @@ -130,7 +130,7 @@ func newNewTokenCmd() simplecobra.Commander { rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "token"}) rootCmd.TableObj.AppendRow(table.Row{key.ID, key.Name, key.Key}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, map[string]interface{}{"id": key.ID, "name": key.Name, "token": key.Key}) } return nil diff --git a/cli/tools/context.go b/cli/tools/context.go index 9db182f8..2c6d4c9b 100644 --- a/cli/tools/context.go +++ b/cli/tools/context.go @@ -47,8 +47,8 @@ func newContextClearCmd() simplecobra.Commander { func newListContextCmd() simplecobra.Commander { return &support.SimpleCommand{ NameP: "list", - RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { - r.TableObj.AppendHeader(table.Row{"context", "active"}) + RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { + rootCmd.TableObj.AppendHeader(table.Row{"context", "active"}) contexts := config.Config().GetGDGConfig().GetContexts() activeContext := config.Config().GetGDGConfig().GetContext() for key := range contexts { @@ -57,11 +57,11 @@ func newListContextCmd() simplecobra.Commander { key = fmt.Sprintf("*%s", activeContext) active = true } - _ = active - r.TableObj.AppendRow(table.Row{key, active}) + rootCmd.TableObj.AppendRow(table.Row{key, active}) } - r.TableObj.Render() + rootCmd.Render(cd.CobraCommand, contexts) + return nil }, Short: "List context", diff --git a/cli/tools/org_preferences.go b/cli/tools/org_preferences.go index 32236833..d9a5cc15 100644 --- a/cli/tools/org_preferences.go +++ b/cli/tools/org_preferences.go @@ -106,7 +106,7 @@ func newGetOrgPreferenceCmd() simplecobra.Commander { rootCmd.TableObj.AppendRow(table.Row{"Theme", pref.Theme}) rootCmd.TableObj.AppendRow(table.Row{"WeekStart", pref.WeekStart}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, pref) return nil diff --git a/cli/tools/organizations.go b/cli/tools/organizations.go index 9dce6fd7..e6d9885a 100644 --- a/cli/tools/organizations.go +++ b/cli/tools/organizations.go @@ -95,7 +95,7 @@ func newGetUserOrgCmd() simplecobra.Commander { slog.Info("No organizations found") } else { rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, map[string]interface{}{"id": org.ID, "name": org.Name}) } return nil @@ -119,7 +119,7 @@ func newGetTokenOrgCmd() simplecobra.Commander { slog.Info("No tokens were found") } else { rootCmd.TableObj.AppendRow(table.Row{org.ID, org.Name}) - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, map[string]interface{}{"id": org.ID, "name": org.Name}) } return nil }, @@ -150,7 +150,7 @@ func newListUsers() simplecobra.Commander { for _, user := range users { rootCmd.TableObj.AppendRow(table.Row{user.UserID, user.Login, user.OrgID, user.Name, user.Email, user.Role}) } - rootCmd.TableObj.Render() + rootCmd.Render(cd.CobraCommand, users) } return nil }, diff --git a/internal/service/dashboards.go b/internal/service/dashboards.go index 39b756b8..01d7f6e6 100644 --- a/internal/service/dashboards.go +++ b/internal/service/dashboards.go @@ -10,10 +10,8 @@ import ( "github.com/grafana/grafana-openapi-client-go/client/dashboards" "github.com/grafana/grafana-openapi-client-go/client/search" "github.com/grafana/grafana-openapi-client-go/models" - "github.com/zeitlinger/conflate" - "time" - "github.com/tidwall/pretty" + "github.com/zeitlinger/conflate" "golang.org/x/exp/maps" "log" "log/slog" @@ -109,7 +107,6 @@ func (s *DashNGoImpl) LintDashboards(req LintRequest) []string { } } else { slog.Error("AutoFix is not possible for dashboard.", slog.String("dashboard", file)) - time.Sleep(time.Second * 5) } }