Skip to content

Commit e831f25

Browse files
authored
Merge pull request #1319 from devstream-io/logging-improvement
chore: logging improvement for invalid configuration
2 parents 593ccc2 + c82b5a9 commit e831f25

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

cmd/devstream/apply.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"os"
5+
"strings"
56

67
"github.com/spf13/cobra"
78

@@ -23,6 +24,9 @@ func applyCMDFunc(cmd *cobra.Command, args []string) {
2324
log.Info("Apply started.")
2425
if err := pluginengine.Apply(configFilePath, continueDirectly); err != nil {
2526
log.Errorf("Apply failed => %s.", err)
27+
if strings.Contains(err.Error(), "config not valid") {
28+
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.")
29+
}
2630
os.Exit(1)
2731
}
2832
log.Success("Apply finished.")

internal/pkg/configmanager/configmanager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package configmanager
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
)
78

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

65+
missingVariableErrorMsg := "map has no entry for key "
66+
6467
// 2. tools with global variables rendered
6568
tools, err := r.getToolsWithVars(vars)
6669
if err != nil {
70+
keyNotFoundIndex := strings.Index(err.Error(), missingVariableErrorMsg)
71+
if keyNotFoundIndex != -1 {
72+
return nil, fmt.Errorf("failed to process variables in the tools section. Missing variable definition: %s", err.Error()[keyNotFoundIndex+len(missingVariableErrorMsg):])
73+
}
6774
return nil, fmt.Errorf("failed to get tools from config file. Error: %w", err)
75+
6876
}
6977

7078
// 3. apps tools with global variables rendered
7179
appTools, err := r.getAppToolsWithVars(vars)
7280
if err != nil {
81+
keyNotFoundIndex := strings.Index(err.Error(), "map has no entry for key ")
82+
if keyNotFoundIndex != -1 {
83+
return nil, fmt.Errorf("failed to process variables in the apps section. Missing variable definition: %s", err.Error()[keyNotFoundIndex+len(missingVariableErrorMsg):])
84+
}
7385
return nil, fmt.Errorf("failed to get apps from config file. Error: %w", err)
7486
}
7587
// all tools from apps should depend on the original tools,

internal/pkg/configmanager/rawconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func newRawConfigFromConfigBytes(fileText []byte) (*rawConfig, error) {
5959

6060
// validate will check config data is valid
6161
func (c *rawConfig) validate() error {
62-
errorFmt := "configmanager can't found valid [%s], please check your config file"
62+
errorFmt := "config not valid; check the [%s] section of your config file"
6363
if (len(c.config)) == 0 {
6464
return fmt.Errorf(errorFmt, "config")
6565
}

internal/pkg/configmanager/rawconfig_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("rawConfig struct", func() {
6969
It("should return error", func() {
7070
e := r.validate()
7171
Expect(e).Error().Should(HaveOccurred())
72-
Expect(e.Error()).Should(Equal("configmanager can't found valid [config], please check your config file"))
72+
Expect(e.Error()).Should(Equal("config not valid; check the [config] section of your config file"))
7373
})
7474
})
7575
When("apps and tools is not exist", func() {
@@ -81,7 +81,7 @@ var _ = Describe("rawConfig struct", func() {
8181
It("should return error", func() {
8282
e := r.validate()
8383
Expect(e).Error().Should(HaveOccurred())
84-
Expect(e.Error()).Should(Equal("configmanager can't found valid [tools and apps], please check your config file"))
84+
Expect(e.Error()).Should(Equal("config not valid; check the [tools and apps] section of your config file"))
8585
})
8686
})
8787
})

0 commit comments

Comments
 (0)