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

chore: logging improvement for invalid configuration #1319

Merged
merged 1 commit into from
Dec 8, 2022
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
4 changes: 4 additions & 0 deletions cmd/devstream/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -23,6 +24,9 @@ func applyCMDFunc(cmd *cobra.Command, args []string) {
log.Info("Apply started.")
if err := pluginengine.Apply(configFilePath, continueDirectly); err != nil {
log.Errorf("Apply failed => %s.", err)
if strings.Contains(err.Error(), "config not valid") {
log.Info("It seems your config file is not valid. Please check the official documentation https://docs.devstream.io, or use the \"dtm show config\" command to get an example.")
}
os.Exit(1)
}
log.Success("Apply finished.")
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/configmanager/configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configmanager
import (
"fmt"
"os"
"strings"
)

// Manager is used to load the config file from the ConfigFilePath and finally get the Config object.
Expand Down Expand Up @@ -61,15 +62,26 @@ func (m *Manager) getConfigFromFileWithGlobalVars() (*Config, error) {
return nil, fmt.Errorf("failed to get variables from config file. Error: %w", err)
}

missingVariableErrorMsg := "map has no entry for key "

// 2. tools with global variables rendered
tools, err := r.getToolsWithVars(vars)
if err != nil {
keyNotFoundIndex := strings.Index(err.Error(), missingVariableErrorMsg)
if keyNotFoundIndex != -1 {
return nil, fmt.Errorf("failed to process variables in the tools section. Missing variable definition: %s", err.Error()[keyNotFoundIndex+len(missingVariableErrorMsg):])
}
return nil, fmt.Errorf("failed to get tools from config file. Error: %w", err)

}

// 3. apps tools with global variables rendered
appTools, err := r.getAppToolsWithVars(vars)
if err != nil {
keyNotFoundIndex := strings.Index(err.Error(), "map has no entry for key ")
if keyNotFoundIndex != -1 {
return nil, fmt.Errorf("failed to process variables in the apps section. Missing variable definition: %s", err.Error()[keyNotFoundIndex+len(missingVariableErrorMsg):])
}
return nil, fmt.Errorf("failed to get apps from config file. Error: %w", err)
}
// all tools from apps should depend on the original tools,
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/configmanager/rawconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func newRawConfigFromConfigBytes(fileText []byte) (*rawConfig, error) {

// validate will check config data is valid
func (c *rawConfig) validate() error {
errorFmt := "configmanager can't found valid [%s], please check your config file"
errorFmt := "config not valid; check the [%s] section of your config file"
if (len(c.config)) == 0 {
return fmt.Errorf(errorFmt, "config")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/configmanager/rawconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var _ = Describe("rawConfig struct", func() {
It("should return error", func() {
e := r.validate()
Expect(e).Error().Should(HaveOccurred())
Expect(e.Error()).Should(Equal("configmanager can't found valid [config], please check your config file"))
Expect(e.Error()).Should(Equal("config not valid; check the [config] section of your config file"))
})
})
When("apps and tools is not exist", func() {
Expand All @@ -81,7 +81,7 @@ var _ = Describe("rawConfig struct", func() {
It("should return error", func() {
e := r.validate()
Expect(e).Error().Should(HaveOccurred())
Expect(e.Error()).Should(Equal("configmanager can't found valid [tools and apps], please check your config file"))
Expect(e.Error()).Should(Equal("config not valid; check the [tools and apps] section of your config file"))
})
})
})
Expand Down