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 support for JSON output #262

Merged
merged 1 commit into from
Mar 12, 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
8 changes: 4 additions & 4 deletions cli/backup/connection_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newConnectionsPermissionListCmd() simplecobra.Commander {
}
}
}
rootCmd.TableObj.Render()
rootCmd.Render(cd.CobraCommand, connections)
}
return nil
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions cli/backup/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand All @@ -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
},
}
Expand All @@ -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
},
}
Expand Down Expand Up @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions cli/backup/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
},
}
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions cli/backup/folder_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand All @@ -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
},
}
Expand All @@ -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
},
}
Expand Down
8 changes: 4 additions & 4 deletions cli/backup/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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
},
Expand All @@ -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
},
Expand All @@ -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
},
Expand Down
10 changes: 5 additions & 5 deletions cli/backup/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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")
}
Expand All @@ -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
},
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
14 changes: 7 additions & 7 deletions cli/backup/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func newOrganizationsListCmd() simplecobra.Commander {
org.Preferences.WeekStart,
})
}
rootCmd.TableObj.Render()
rootCmd.Render(cd.CobraCommand, listOrganizations)
}
return nil
},
Expand All @@ -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
},
Expand All @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions cli/backup/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func newTeamsListCmd() simplecobra.Commander {
}
}
}
rootCmd.TableObj.Render()
rootCmd.Render(cd.CobraCommand, teams)
}
return nil
},
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -134,7 +134,7 @@ func newTeamsUploadCmd() simplecobra.Commander {
}
}
}
rootCmd.TableObj.Render()
rootCmd.Render(cd.CobraCommand, savedFiles)
}
return nil
},
Expand All @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions cli/backup/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
Expand Down Expand Up @@ -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
},
Expand All @@ -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
},
Expand Down
20 changes: 20 additions & 0 deletions cli/support/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
Loading
Loading