Skip to content

Commit

Permalink
Refacotring configuration struct to better reflect the yaml files.
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Jul 7, 2023
1 parent 590cf40 commit 9a1b47c
Show file tree
Hide file tree
Showing 36 changed files with 525 additions and 409 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Starting SFTP Server
run: docker run -p 2222:22 -d atmoz/sftp "foo:pass:::testing"
- name: Wait for Minio to come up.
run: nc -z -v -w15 127.0.0.1 9000
- name: Wait for SSH to come up.
run: nc -z -v -w15 127.0.0.1 2222
- name: Calc coverage
if: "${{ matrix.go == '1.20.2' && matrix.grafana == '9.4.3' }}"
run: |
Expand Down
12 changes: 6 additions & 6 deletions cmd/alertnotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
Expand All @@ -27,7 +27,7 @@ var clearAlertNotifications = &cobra.Command{
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
tableObj.AppendHeader(table.Row{"type", "filename"})

log.Infof("Clearing all alert notification channels for context: '%s'", apphelpers.GetContext())
log.Infof("Clearing all alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
deleted := grafanaSvc.DeleteAllAlertNotifications()
for _, item := range deleted {
tableObj.AppendRow(table.Row{"alertnotification", item})
Expand All @@ -50,7 +50,7 @@ var uploadAlertNotifications = &cobra.Command{
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
tableObj.AppendHeader(table.Row{"name", "id", "UID"})

log.Infof("Exporting alert notification channels for context: '%s'", apphelpers.GetContext())
log.Infof("Exporting alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
grafanaSvc.ExportAlertNotifications()
items := grafanaSvc.ListAlertNotifications()
for _, item := range items {
Expand All @@ -73,7 +73,7 @@ var downloadAlertNotifications = &cobra.Command{
log.Warn("Alert Notifications will be deprecated as of Grafana 9.0, this API may no longer work soon")
tableObj.AppendHeader(table.Row{"type", "filename"})

log.Infof("Importing alert notification channels for context: '%s'", apphelpers.GetContext())
log.Infof("Importing alert notification channels for context: '%s'", config.Config().AppConfig.GetContext())
savedFiles := grafanaSvc.ImportAlertNotifications()
for _, file := range savedFiles {
tableObj.AppendRow(table.Row{"alertnotification", file})
Expand All @@ -93,12 +93,12 @@ var listAlertNotifications = &cobra.Command{
tableObj.AppendHeader(table.Row{"id", "name", "slug", "type", "default", "url"})
alertnotifications := grafanaSvc.ListAlertNotifications()

log.Infof("Listing alert notifications channels for context: '%s'", apphelpers.GetContext())
log.Infof("Listing alert notifications channels for context: '%s'", config.Config().AppConfig.GetContext())
if len(alertnotifications) == 0 {
log.Info("No alert notifications found")
} else {
for _, link := range alertnotifications {
url := fmt.Sprintf("%s/alerting/notification/%d/edit", apphelpers.GetCtxDefaultGrafanaConfig().URL, link.ID)
url := fmt.Sprintf("%s/alerting/notification/%d/edit", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
tableObj.AppendRow(table.Row{link.ID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url})
}
tableObj.Render()
Expand Down
6 changes: 3 additions & 3 deletions cmd/auth_serviceAccts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package cmd

import (
"errors"
"github.com/esnet/gdg/internal/config"
"sort"
"strconv"

"github.com/esnet/gdg/internal/apphelpers"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -74,7 +74,7 @@ var deleteServiceAcctsTokensCmd = &cobra.Command{
log.Fatalf("unable to parse %s as a valid numeric value", idStr)
}

log.Infof("Deleting Service Accounts Tokens for serviceID %d for context: '%s'", id, apphelpers.GetContext())
log.Infof("Deleting Service Accounts Tokens for serviceID %d for context: '%s'", id, config.Config().AppConfig.GetContext())
savedFiles := grafanaSvc.DeleteServiceAccountTokens(id)
tableObj.AppendHeader(table.Row{"serviceID", "type", "token_name"})
if len(savedFiles) == 0 {
Expand All @@ -95,7 +95,7 @@ var deleteServiceAcctsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

savedFiles := grafanaSvc.DeleteAllServiceAccounts()
log.Infof("Delete Service Accounts for context: '%s'", apphelpers.GetContext())
log.Infof("Delete Service Accounts for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"type", "filename"})
if len(savedFiles) == 0 {
log.Info("No Service Accounts found")
Expand Down
4 changes: 2 additions & 2 deletions cmd/auth_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"errors"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -54,7 +54,7 @@ var deleteTokensCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

savedFiles := grafanaSvc.DeleteAllTokens()
log.Infof("Delete Tokens for context: '%s'", apphelpers.GetContext())
log.Infof("Delete Tokens for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"type", "filename"})
if len(savedFiles) == 0 {
log.Info("No Tokens found")
Expand Down
32 changes: 15 additions & 17 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"errors"
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -22,7 +22,7 @@ var contextClear = &cobra.Command{
Short: "clear all context",
Long: `clear all contexts`,
Run: func(cmd *cobra.Command, args []string) {
apphelpers.ClearContexts()
config.Config().ClearContexts()
log.Info("Successfully deleted all configured contexts")
},
}
Expand All @@ -42,8 +42,7 @@ var contextCopy = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
src := args[0]
dest := args[1]
apphelpers.CopyContext(src, dest)

config.Config().CopyContext(src, dest)
},
}

Expand All @@ -60,7 +59,7 @@ var contextDelete = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
ctx := args[0]
apphelpers.DeleteContext(ctx)
config.Config().DeleteContext(ctx)
log.Infof("Successfully deleted context %s", ctx)
},
}
Expand All @@ -71,15 +70,15 @@ var contextList = &cobra.Command{
Long: `List contexts.`,
Run: func(cmd *cobra.Command, args []string) {
tableObj.AppendHeader(table.Row{"context", "active"})
contexts := apphelpers.GetContexts()
activeContext := apphelpers.GetContext()
for _, item := range contexts {
contexts := config.Config().GetAppConfig().GetContexts()
activeContext := config.Config().GetAppConfig().GetContext()
for key, _ := range contexts {
active := false
if item == strings.ToLower(activeContext) {
item = fmt.Sprintf("*%s", activeContext)
if key == strings.ToLower(activeContext) {
key = fmt.Sprintf("*%s", activeContext)
active = true
}
tableObj.AppendRow(table.Row{item, active})
tableObj.AppendRow(table.Row{key, active})
}

tableObj.Render()
Expand All @@ -99,8 +98,7 @@ var contextNew = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
ctx := args[0]
apphelpers.NewContext(ctx)

config.Config().NewContext(ctx)
},
}

Expand All @@ -117,7 +115,7 @@ var contextSet = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
context := args[0]
apphelpers.SetContext(context)
config.Config().ChangeContext(context)

},
}
Expand All @@ -127,11 +125,11 @@ var contextShow = &cobra.Command{
Short: "show optional[context]",
Long: `show contexts optional[context]`,
Run: func(cmd *cobra.Command, args []string) {
context := apphelpers.GetContext()
if len(args) > 1 && len(args[0]) > 0 {
context := config.Config().GetAppConfig().GetContext()
if len(args) > 0 && len(args[0]) > 0 {
context = args[0]
}
apphelpers.ShowContext(context)
config.Config().PrintContext(context)

},
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"bufio"
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -75,7 +75,7 @@ var uploadDashboard = &cobra.Command{
if confirm {
askForConfirmation(fmt.Sprintf("WARNING: this will delete all dashboards from the monitored folders: '%s' "+
"(or all folders if ignore_dashboard_filters is set to true) and upload your local copy. Do you wish to "+
"continue (y/n)", strings.Join(apphelpers.GetCtxDefaultGrafanaConfig().GetMonitoredFolders(), ", ")))
"continue (y/n)", strings.Join(config.Config().GetDefaultGrafanaConfig().GetMonitoredFolders(), ", ")))
}
grafanaSvc.ExportDashboards(filter)

Expand Down Expand Up @@ -103,7 +103,7 @@ var downloadDashboard = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
filter := service.NewDashboardFilter(parseDashboardGlobalFlags(cmd)...)
savedFiles := grafanaSvc.ImportDashboards(filter)
log.Infof("Importing dashboards for context: '%s'", apphelpers.GetContext())
log.Infof("Importing dashboards for context: '%s'", config.Config().GetAppConfig().GetContext())
tableObj.AppendHeader(table.Row{"type", "filename"})
for _, file := range savedFiles {
tableObj.AppendRow(table.Row{"dashboard", file})
Expand All @@ -123,9 +123,9 @@ var listDashboards = &cobra.Command{
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cmd)...)
boards := grafanaSvc.ListDashboards(filters)

log.Infof("Listing dashboards for context: '%s'", apphelpers.GetContext())
log.Infof("Listing dashboards for context: '%s'", config.Config().GetAppConfig().GetContext())
for _, link := range boards {
url := fmt.Sprintf("%s%s", apphelpers.GetCtxDefaultGrafanaConfig().URL, link.URL)
url := fmt.Sprintf("%s%s", config.Config().GetDefaultGrafanaConfig().URL, link.URL)
tableObj.AppendRow(table.Row{link.ID, link.Title, link.Slug, link.FolderTitle,
link.UID, strings.Join(link.Tags, ","), url})

Expand Down
8 changes: 4 additions & 4 deletions cmd/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -60,7 +60,7 @@ var downloadDataSources = &cobra.Command{
Long: `download all datasources from grafana to local filesystem`,
Aliases: []string{"d", "import"},
Run: func(cmd *cobra.Command, args []string) {
log.Infof("Importing datasources for context: '%s'", apphelpers.GetContext())
log.Infof("Importing datasources for context: '%s'", config.Config().GetAppConfig().GetContext())
dashboardFilter, _ := cmd.Flags().GetString("datasource")
filters := service.NewDataSourceFilter(dashboardFilter)
savedFiles := grafanaSvc.ImportDataSources(filters)
Expand All @@ -83,12 +83,12 @@ var listDataSources = &cobra.Command{
dashboardFilter, _ := cmd.Flags().GetString("datasource")
filters := service.NewDataSourceFilter(dashboardFilter)
dsListing := grafanaSvc.ListDataSources(filters)
log.Infof("Listing datasources for context: '%s'", apphelpers.GetContext())
log.Infof("Listing datasources for context: '%s'", config.Config().GetAppConfig().GetContext())
if len(dsListing) == 0 {
log.Info("No datasources found")
} else {
for _, link := range dsListing {
url := fmt.Sprintf("%s/datasource/edit/%d", apphelpers.GetCtxDefaultGrafanaConfig().URL, link.ID)
url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
tableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url})
}
tableObj.Render()
Expand Down
10 changes: 5 additions & 5 deletions cmd/folders.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/filters"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -33,7 +33,7 @@ var deleteFoldersCmd = &cobra.Command{
Long: `delete Folders from grafana`,
Run: func(cmd *cobra.Command, args []string) {

log.Infof("Deleting all Folders for context: '%s'", apphelpers.GetContext())
log.Infof("Deleting all Folders for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"title"})

folders := grafanaSvc.DeleteAllFolder(getFolderFilter())
Expand All @@ -56,7 +56,7 @@ var uploadFoldersCmd = &cobra.Command{
Aliases: []string{"export", "u"},
Run: func(cmd *cobra.Command, args []string) {

log.Infof("Listing Folders for context: '%s'", apphelpers.GetContext())
log.Infof("Listing Folders for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"file"})
folders := grafanaSvc.ExportFolder(getFolderFilter())
if len(folders) == 0 {
Expand All @@ -78,7 +78,7 @@ var downloadFoldersCmd = &cobra.Command{
Aliases: []string{"import", "d"},
Run: func(cmd *cobra.Command, args []string) {

log.Infof("Listing Folders for context: '%s'", apphelpers.GetContext())
log.Infof("Listing Folders for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"file"})
folders := grafanaSvc.ImportFolder(getFolderFilter())
if len(folders) == 0 {
Expand All @@ -100,7 +100,7 @@ var listFoldersCmd = &cobra.Command{
Aliases: []string{"l"},
Run: func(cmd *cobra.Command, args []string) {

log.Infof("Listing Folders for context: '%s'", apphelpers.GetContext())
log.Infof("Listing Folders for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"id", "uid", "title"})
folders := grafanaSvc.ListFolder(getFolderFilter())

Expand Down
8 changes: 4 additions & 4 deletions cmd/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"encoding/json"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/service/filters"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -69,7 +69,7 @@ var downloadLibary = &cobra.Command{
log.Info("exporting lib elements")
//filter := parseDashboardGlobalFlags(cmd)
savedFiles := grafanaSvc.ImportLibraryElements(nil)
log.Infof("Importing library for context: '%s'", apphelpers.GetContext())
log.Infof("Importing library for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"type", "filename"})
for _, file := range savedFiles {
tableObj.AppendRow(table.Row{"library", file})
Expand All @@ -89,7 +89,7 @@ var listLibraries = &cobra.Command{
elements := grafanaSvc.ListLibraryElements(nil)
log.Infof("Number of elements is: %d", len(elements))

log.Infof("Listing library for context: '%s'", apphelpers.GetContext())
log.Infof("Listing library for context: '%s'", config.Config().AppConfig.GetContext())
for _, link := range elements {
tableObj.AppendRow(table.Row{link.ID, link.UID, link.Meta.FolderName, link.Name, link.Type})

Expand All @@ -113,7 +113,7 @@ var listLibraryConnections = &cobra.Command{

libElmentUid := args[0]
elements := grafanaSvc.ListLibraryElementsConnections(nil, libElmentUid)
log.Infof("Listing library for context: '%s'", apphelpers.GetContext())
log.Infof("Listing library connections for context: '%s'", config.Config().AppConfig.GetContext())
for _, link := range elements {
dash := link.Dashboard.(map[string]interface{})
tableObj.AppendRow(table.Row{dash["id"].(json.Number), dash["uid"].(string), link.Meta.Slug, dash["title"].(string), link.Meta.FolderTitle})
Expand Down
4 changes: 2 additions & 2 deletions cmd/organizations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/config"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -21,7 +21,7 @@ var listOrgCmd = &cobra.Command{
Aliases: []string{"l"},
Run: func(cmd *cobra.Command, args []string) {

log.Infof("Listing organizations for context: '%s'", apphelpers.GetContext())
log.Infof("Listing organizations for context: '%s'", config.Config().AppConfig.GetContext())
tableObj.AppendHeader(table.Row{"id", "org"})
listOrganizations := grafanaSvc.ListOrganizations()
if len(listOrganizations) == 0 {
Expand Down
Loading

0 comments on commit 9a1b47c

Please sign in to comment.