Skip to content

Commit

Permalink
[BUG] Fixing config loading with missing extension (#318)
Browse files Browse the repository at this point in the history
* [BUG] Fixing config loading with missing extension

Fixes #317

* Removing default configuration.  GDG will now require a valid config for backup/tools functionality.

* Adding a small test for default-config
  • Loading branch information
safaci2000 authored Nov 12, 2024
1 parent 5118bfc commit 44cef1b
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 97 deletions.
9 changes: 2 additions & 7 deletions cli/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ package cli

import (
"context"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/backup"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/cli/tools"
assets "github.com/esnet/gdg/config"
)

// Execute executes a command.
func Execute(defaultCfg string, args []string, options ...support.RootOption) error {
func Execute(args []string, options ...support.RootOption) error {
var err error
support.DefaultConfig, err = assets.GetFile(defaultCfg)
if err != nil {
slog.Warn("unable to find load default configuration", "err", err)
}
rootCmd := support.NewRootCmd(getNewRootCmd(), options...)
x, err := simplecobra.New(rootCmd)
if err != nil {
Expand All @@ -41,6 +35,7 @@ func getNewRootCmd() *support.RootCommand {
NameP: "gdg",
CommandEntries: []simplecobra.Commander{
newVersionCmd(),
newDefaultConfig(),
tools.NewToolsCommand(),
backup.NewBackupCommand(),
},
Expand Down
13 changes: 3 additions & 10 deletions cli/support/init_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ import (
// InitConfiguration Loads configuration, and setups fail over case
func InitConfiguration(cmd *cobra.Command) {
configOverride, _ := cmd.Flags().GetString("config")
if DefaultConfig == "" {
raw, err := os.ReadFile("config/importer-example.yml")
if err == nil {
DefaultConfig = string(raw)
} else {
DefaultConfig = ""
}
}

// Registers sub CommandsList
config.InitGdgConfig(configOverride, DefaultConfig)
if config.Config() == nil {
config.InitGdgConfig(configOverride)
}
appconfig.InitializeAppLogger(os.Stdout, os.Stderr, config.Config().IsDebug())

// Validate current configuration
Expand Down
9 changes: 4 additions & 5 deletions cli/test/backup/alerting_contactpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/esnet/gdg/cli"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/pkg/test_tooling"
"github.com/esnet/gdg/pkg/test_tooling/common"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -66,7 +65,7 @@ func TestUploadContactPoints(t *testing.T) {
r, w, cleanup := test_tooling.InterceptStdout()
defer cleanup()

err := cli.Execute(common.DefaultTestConfig, listCmd, optionMockSvc())
err := cli.Execute(listCmd, optionMockSvc())
if tc.expectErr {
assert.NotNil(t, err)
} else {
Expand Down Expand Up @@ -129,7 +128,7 @@ func TestDownloadContactPoints(t *testing.T) {
r, w, cleanup := test_tooling.InterceptStdout()
defer cleanup()

err := cli.Execute(common.DefaultTestConfig, listCmd, optionMockSvc())
err := cli.Execute(listCmd, optionMockSvc())
if tc.expectErr {
assert.NotNil(t, err)
} else {
Expand Down Expand Up @@ -210,7 +209,7 @@ func TestListContactPoints(t *testing.T) {
r, w, cleanup := test_tooling.InterceptStdout()
defer cleanup()

err := cli.Execute(common.DefaultTestConfig, listCmd, optionMockSvc())
err := cli.Execute(listCmd, optionMockSvc())
if tc.expectErr {
assert.NotNil(t, err)
} else {
Expand Down Expand Up @@ -285,7 +284,7 @@ func TestClearContactPoints(t *testing.T) {
r, w, cleanup := test_tooling.InterceptStdout()
defer cleanup()

err := cli.Execute(common.DefaultTestConfig, clearCmd, optionMockSvc())
err := cli.Execute(clearCmd, optionMockSvc())
assert.Nil(t, err)
defer cleanup()
assert.NoError(t, w.Close())
Expand Down
3 changes: 1 addition & 2 deletions cli/test/backup/conections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/pkg/test_tooling/common"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -42,7 +41,7 @@ func TestConnectionCommand(t *testing.T) {
}
r, w, cleanup := test_tooling.InterceptStdout()

err := cli.Execute(common.DefaultTestConfig, []string{"backup", "connections", "list"}, optionMockSvc())
err := cli.Execute([]string{"backup", "connections", "list"}, optionMockSvc())
assert.Nil(t, err)
defer cleanup()
assert.NoError(t, w.Close())
Expand Down
10 changes: 5 additions & 5 deletions cli/test/tools/devel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

func TestDevelSrvInfo(t *testing.T) {
execMe := func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error {
execMe := func(mock *mocks.GrafanaService, optionMockSvc func() support.RootOption) error {
expected := make(map[string]interface{})
expected["Database"] = "db"
expected["Commit"] = "commit"
expected["Version"] = "version"

mock.EXPECT().GetServerInfo().Return(expected)
err := cli.Execute(string(data), []string{"tools", "devel", "srvinfo"}, optionMockSvc())
err := cli.Execute([]string{"tools", "devel", "srvinfo"}, optionMockSvc())
return err
}
outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe)
Expand All @@ -32,9 +32,9 @@ func TestDevelSrvInfo(t *testing.T) {
}

func TestDevelSrvCompletion(t *testing.T) {
fn := func(args []string) func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error {
return func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error {
err := cli.Execute(string(data), args, optionMockSvc())
fn := func(args []string) func(mock *mocks.GrafanaService, optionMockSvc func() support.RootOption) error {
return func(mock *mocks.GrafanaService, optionMockSvc func() support.RootOption) error {
err := cli.Execute(args, optionMockSvc())
return err
}
}
Expand Down
22 changes: 18 additions & 4 deletions cli/test/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ import (
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/service/mocks"
"github.com/esnet/gdg/internal/version"
"github.com/esnet/gdg/pkg/test_tooling/common"
"github.com/stretchr/testify/assert"
)

func TestDefaultConfigCommand(t *testing.T) {
assert.NoError(t, path.FixTestDir("test", "../.."))
execMe := func(mock *mocks.GrafanaService, optionMockSvc func() support.RootOption) error {
err := cli.Execute([]string{"default-config"}, optionMockSvc())
return err
}
outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe)
defer closeReader()

assert.True(t, strings.Contains(outStr, "storage_engine"))
assert.True(t, strings.Contains(outStr, "global"))
assert.True(t, strings.Contains(outStr, "context_name:"))
assert.True(t, strings.Contains(outStr, "contexts:"))
}

func TestVersionCommand(t *testing.T) {
assert.NoError(t, path.FixTestDir("test", "../.."))
execMe := func(mock *mocks.GrafanaService, data []byte, optionMockSvc func() support.RootOption) error {
err := cli.Execute(string(data), []string{"version"}, optionMockSvc())
execMe := func(mock *mocks.GrafanaService, optionMockSvc func() support.RootOption) error {
err := cli.Execute([]string{"version"}, optionMockSvc())
return err
}
outStr, closeReader := test_tooling.SetupAndExecuteMockingServices(t, execMe)
Expand Down Expand Up @@ -49,7 +63,7 @@ func TestVersionErrCommand(t *testing.T) {
}
r, w, cleanup := test_tooling.InterceptStdout()
defer cleanup()
err := cli.Execute(common.DefaultTestConfig, []string{"dumb", "dumb"}, optionMockSvc())
err := cli.Execute([]string{"dumb", "dumb"}, optionMockSvc())
assert.NotNil(t, err)
assert.NoError(t, w.Close())

Expand Down
20 changes: 19 additions & 1 deletion cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cli

import (
"context"
"fmt"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/config"
"github.com/esnet/gdg/internal/version"
"github.com/spf13/cobra"
)
Expand All @@ -20,6 +22,22 @@ func newVersionCmd() simplecobra.Commander {
cmd.Aliases = []string{"v"}
},
Short: "Print the version number of generated code example",
Long: "All software has versions. This is generated code example",
Long: "Print the version number of generated code example",
}
}

func newDefaultConfig() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "default-config",
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error {
o := config.Configuration{}
fmt.Print(o.DefaultConfig())
return nil
},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"v"}
},
Short: "Prints an example configuration",
Long: "Prints an example configuration",
}
}
11 changes: 4 additions & 7 deletions cmd/gdg-generate/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"log/slog"
"os"

assets "github.com/esnet/gdg/config"
"github.com/esnet/gdg/internal/version"

"github.com/esnet/gdg/internal/config"
appconfig "github.com/esnet/gdg/internal/log"
"github.com/esnet/gdg/internal/templating"
Expand Down Expand Up @@ -33,6 +34,7 @@ func init() {

func initConfig() {
var err error
slog.Info("Running gdg-generate", slog.Any("version", version.Version))
cfgFile, err = rootCmd.Flags().GetString("config")
if err != nil {
log.Fatal("unable to get config file")
Expand All @@ -42,12 +44,7 @@ func initConfig() {
log.Fatal("unable to get template config file")
}

defaultConfiguration, err := assets.GetFile("importer-example.yml")
if err != nil {
slog.Warn("unable to load default configuration, no fallback")
}

config.InitGdgConfig(cfgFile, defaultConfiguration)
config.InitGdgConfig(cfgFile)
config.InitTemplateConfig(tplCfgFile)
cfg := config.Config()
appconfig.InitializeAppLogger(os.Stdout, os.Stderr, cfg.IsDebug())
Expand Down
2 changes: 1 addition & 1 deletion cmd/gdg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
}
}

err := cli.Execute("importer-example.yml", os.Args[1:], setGrafanaSvc())
err := cli.Execute(os.Args[1:], setGrafanaSvc())
if err != nil {
log.Fatalf("Error: %s", err)
}
Expand Down
Loading

0 comments on commit 44cef1b

Please sign in to comment.