github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/backup/library.go (about) 1 package backup 2 3 import ( 4 "context" 5 "encoding/json" 6 "github.com/bep/simplecobra" 7 "github.com/esnet/gdg/cli/support" 8 "github.com/esnet/gdg/internal/config" 9 "github.com/esnet/gdg/internal/service/filters" 10 "github.com/jedib0t/go-pretty/v6/table" 11 "github.com/spf13/cobra" 12 "log" 13 "log/slog" 14 ) 15 16 func newLibraryElementsCommand() simplecobra.Commander { 17 description := "Manage Library Elements" 18 return &support.SimpleCommand{ 19 NameP: "libraryelements", 20 Short: description, 21 Long: description, 22 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 23 cmd.Aliases = []string{"lib", "library"} 24 }, 25 CommandsList: []simplecobra.Commander{ 26 newLibraryElementsListCmd(), 27 newLibraryElementsClearCmd(), 28 newLibraryElementsDownloadCmd(), 29 newLibraryElementsUploadCmd(), 30 newLibraryElementsListConnectionsCmd(), 31 }, 32 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 33 return cd.CobraCommand.Help() 34 }, 35 } 36 } 37 38 func newLibraryElementsClearCmd() simplecobra.Commander { 39 description := "delete all Library elements from grafana" 40 return &support.SimpleCommand{ 41 NameP: "clear", 42 Short: description, 43 Long: description, 44 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 45 cmd.Aliases = []string{"c"} 46 }, 47 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 48 //filter := getLibraryGlobalFlags(cli) 49 deletedLibrarys := rootCmd.GrafanaSvc().DeleteAllLibraryElements(nil) 50 rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"}) 51 for _, file := range deletedLibrarys { 52 rootCmd.TableObj.AppendRow(table.Row{"library", file}) 53 } 54 if len(deletedLibrarys) == 0 { 55 slog.Info("No library were found. 0 libraries removed") 56 57 } else { 58 slog.Info("libraries were deleted", "count", len(deletedLibrarys)) 59 rootCmd.Render(cd.CobraCommand, deletedLibrarys) 60 } 61 return nil 62 }, 63 } 64 } 65 func newLibraryElementsListCmd() simplecobra.Commander { 66 description := "List all library Elements" 67 return &support.SimpleCommand{ 68 NameP: "list", 69 Short: description, 70 Long: description, 71 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 72 cmd.Aliases = []string{"l"} 73 }, 74 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 75 rootCmd.TableObj.AppendHeader(table.Row{"id", "UID", "Folder", "Name", "Type"}) 76 77 elements := rootCmd.GrafanaSvc().ListLibraryElements(nil) 78 79 slog.Info("Listing library for context", "context", config.Config().GetGDGConfig().GetContext()) 80 for _, link := range elements { 81 rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Meta.FolderName, link.Name, link.Type}) 82 83 } 84 if len(elements) > 0 { 85 rootCmd.Render(cd.CobraCommand, elements) 86 } else { 87 slog.Info("No library found") 88 } 89 90 return nil 91 }, 92 } 93 } 94 func newLibraryElementsDownloadCmd() simplecobra.Commander { 95 description := "Download all library from grafana to local file system" 96 return &support.SimpleCommand{ 97 NameP: "download", 98 Short: description, 99 Long: description, 100 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 101 cmd.Aliases = []string{"d"} 102 }, 103 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 104 slog.Info("Downloading library for context", "context", config.Config().GetGDGConfig().GetContext()) 105 savedFiles := rootCmd.GrafanaSvc().DownloadLibraryElements(nil) 106 rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"}) 107 for _, file := range savedFiles { 108 rootCmd.TableObj.AppendRow(table.Row{"library", file}) 109 } 110 rootCmd.Render(cd.CobraCommand, savedFiles) 111 return nil 112 }, 113 } 114 } 115 func newLibraryElementsUploadCmd() simplecobra.Commander { 116 description := "upload all library to grafana" 117 return &support.SimpleCommand{ 118 NameP: "upload", 119 Short: description, 120 Long: description, 121 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 122 cmd.Aliases = []string{"u"} 123 }, 124 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 125 slog.Info("exporting lib elements") 126 libraryFilter := filters.NewBaseFilter() 127 elements := rootCmd.GrafanaSvc().UploadLibraryElements(libraryFilter) 128 rootCmd.TableObj.AppendHeader(table.Row{"Name"}) 129 if len(elements) > 0 { 130 for _, link := range elements { 131 rootCmd.TableObj.AppendRow(table.Row{link}) 132 } 133 rootCmd.Render(cd.CobraCommand, elements) 134 } else { 135 slog.Info("No library found") 136 } 137 return nil 138 }, 139 } 140 } 141 142 func newLibraryElementsListConnectionsCmd() simplecobra.Commander { 143 description := "List all library Connection given a valid library Connection UID" 144 return &support.SimpleCommand{ 145 NameP: "list-connections", 146 Short: description, 147 Long: description, 148 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 149 cmd.Aliases = []string{"c"} 150 }, 151 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 152 if len(args) != 1 { 153 log.Fatal("Wrong number of arguments, requires library element UUID") 154 } 155 rootCmd.TableObj.AppendHeader(table.Row{"id", "UID", "Slug", "Title", "Folder"}) 156 157 libElmentUid := args[0] 158 elements := rootCmd.GrafanaSvc().ListLibraryElementsConnections(nil, libElmentUid) 159 slog.Info("Listing library connections for context", "context", config.Config().GetGDGConfig().GetContext()) 160 for _, link := range elements { 161 dash := link.Dashboard.(map[string]interface{}) 162 rootCmd.TableObj.AppendRow(table.Row{dash["id"].(json.Number), dash["uid"].(string), link.Meta.Slug, dash["title"].(string), link.Meta.FolderTitle}) 163 } 164 if len(elements) > 0 { 165 rootCmd.Render(cd.CobraCommand, elements) 166 } else { 167 slog.Info("No library found") 168 } 169 return nil 170 }, 171 } 172 }