Skip to content

Commit

Permalink
Adding a few more items
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Sep 25, 2023
1 parent f4ee4c3 commit cd0b11e
Show file tree
Hide file tree
Showing 24 changed files with 2,935 additions and 43 deletions.
20 changes: 18 additions & 2 deletions cmd/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package cmd

import (
"context"
"github.com/bep/simplecobra"
"github.com/esnet/gdg/cmd/backup"
"github.com/esnet/gdg/cmd/support"
"github.com/esnet/gdg/cmd/tools"
)

// Execute executes a command.
func Execute(defaultCfg string, args []string) error {
func Execute(defaultCfg string, args []string, options ...support.RootOption) error {
support.DefaultConfig = defaultCfg
x, err := NewExec()
rootCmd := support.NewRootCmd(getNewRootCmd(), options...)
x, err := simplecobra.New(rootCmd)
if err != nil {
return err
}
Expand All @@ -21,3 +25,15 @@ func Execute(defaultCfg string, args []string) error {

return nil
}

func getNewRootCmd() *support.RootCommand {
return &support.RootCommand{
NameP: "gdg",
CommandEntries: []simplecobra.Commander{
newVersionCmd(),
newContextCmd(),
tools.NewToolsCommand(),
backup.NewBackupCommand(),
},
}
}
24 changes: 0 additions & 24 deletions cmd/commands.go

This file was deleted.

28 changes: 18 additions & 10 deletions cmd/support/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"os"
"sync"
)

var (
DefaultConfig string
svcLock sync.Mutex
)

type RootCommand struct {
NameP string
isInit bool

grafanaSvc service.GrafanaService
GrafanaSvc func() service.GrafanaService

localFlagName string

Expand All @@ -39,8 +37,17 @@ type RootCommand struct {
CommandEntries []simplecobra.Commander
}

func (c *RootCommand) GrafanaSvc() service.GrafanaService {
return c.grafanaSvc
type RootOption func(command *RootCommand)

// NewRootCmd Allows to construct a root command passing any number of arguments to set RootCommand Options
func NewRootCmd(root *RootCommand, options ...RootOption) *RootCommand {
if root == nil {
root = &RootCommand{}
}
for _, o := range options {
o(root)
}
return root
}

func (c *RootCommand) Commands() []simplecobra.Commander {
Expand All @@ -52,8 +59,6 @@ func (c *RootCommand) PreRun(this, runner *simplecobra.Commandeer) error {
c.initThis = this
c.initRunner = runner
c.initConfiguration()

c.grafanaSvc = service.NewApiService()
return nil
}

Expand Down Expand Up @@ -96,11 +101,14 @@ func (c *RootCommand) Init(cd *simplecobra.Commandeer) error {
return errors.New("failWithCobraCommand")
}
cmd := cd.CobraCommand

persistentFlags := cmd.PersistentFlags()
persistentFlags.StringP("config", "c", "", "Configuration Override")
c.TableObj = table.NewWriter()
c.TableObj.SetOutputMirror(os.Stdout)
c.TableObj.SetStyle(table.StyleLight)
if c.TableObj == nil {
c.TableObj = table.NewWriter()
c.TableObj.SetOutputMirror(os.Stdout)
c.TableObj.SetStyle(table.StyleLight)
}

return nil
}
12 changes: 7 additions & 5 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"github.com/esnet/gdg/cmd/support"
"github.com/esnet/gdg/internal/version"
"github.com/spf13/cobra"
"os"
)

func newVersionCmd() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "version",
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error {
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Git Commit:", version.GitCommit)
fmt.Println("Version:", version.Version)
fmt.Println("Go Version:", version.GoVersion)
fmt.Println("OS / Arch:", version.OsArch)
stdout := os.Stdout
fmt.Fprintf(stdout, "Build Date: %s\n", version.BuildDate)
fmt.Fprintf(stdout, "Git Commit: %s\n", version.GitCommit)
fmt.Fprintf(stdout, "Version: %s\n", version.Version)
fmt.Fprintf(stdout, "Go Version: %s\n", version.GoVersion)
fmt.Fprintf(stdout, "OS / Arch: %s\n", version.OsArch)
return nil
},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
Expand Down
62 changes: 62 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"github.com/esnet/gdg/cmd/support"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/internal/version"
"github.com/stretchr/testify/assert"
"io"
"os"
"strings"
"testing"
)

func interceptStdout() (*os.File, *os.File, func()) {
backupStd := os.Stdout
backupErr := os.Stderr
r, w, _ := os.Pipe()
//Restore streams
cleanup := func() {
os.Stdout = backupStd
os.Stderr = backupErr
}
os.Stdout = w
os.Stderr = w

return r, w, cleanup

}

func TestVersionCommand(t *testing.T) {
testSvc := new(mocks.GrafanaService)
getMockSvc := func() service.GrafanaService {
return testSvc
}

optionMockSvc := func() support.RootOption {
return func(response *support.RootCommand) {
response.GrafanaSvc = getMockSvc
}
}
path, _ := os.Getwd()
fmt.Println(path)
r, w, cleanup := interceptStdout()
data, err := os.ReadFile("../config/testing.yml")
assert.Nil(t, err)

err = Execute(string(data), []string{"version"}, optionMockSvc())
assert.Nil(t, err)
defer cleanup()
w.Close()
out, _ := io.ReadAll(r)
outStr := string(out)
assert.True(t, strings.Contains(outStr, "Build Date:"))
assert.True(t, strings.Contains(outStr, "Git Commit:"))
assert.True(t, strings.Contains(outStr, "Version:"))
assert.True(t, strings.Contains(outStr, version.Version))
assert.True(t, strings.Contains(outStr, "Date:"))
assert.True(t, strings.Contains(outStr, "Go Version:"))
assert.True(t, strings.Contains(outStr, "OS / Arch:"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
go.opencensus.io v0.24.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1Fof
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
1 change: 0 additions & 1 deletion internal/service/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type DashboardsApi interface {
DownloadDashboards(filter filters.Filter) []string
UploadDashboards(filter filters.Filter)
DeleteAllDashboards(filter filters.Filter) []string
getDashboardByUid(uid string) (*models.DashboardFullWithMeta, error)
}

// getDashboardByUid retrieve a dashboard given a particular uid.
Expand Down
91 changes: 91 additions & 0 deletions internal/service/mocks/AlertNotificationsApi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd0b11e

Please sign in to comment.