Skip to content

Commit 682d02b

Browse files
committed
add error handler for init()
1 parent cd4db20 commit 682d02b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

internal/arguments/arguments.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func New() Handler {
4343
}
4444
}
4545

46-
func (args *Handler) CollectArgsFromStdin(availableModules string) {
46+
func (args *Handler) CollectArgsFromStdin(availableModules string) []error {
4747
fmt.Printf(interactiveHelpText+"\n\n", availableModules)
4848

4949
var errors []error
@@ -59,8 +59,10 @@ func (args *Handler) CollectArgsFromStdin(availableModules string) {
5959
continue
6060
}
6161

62-
errors = append(errors, fmt.Errorf("%s is not matching the needed depenency", argument.Name))
62+
errors = append(errors, fmt.Errorf("argument '%s' is not matching the needed depenency. Skipping... ", argument.Name))
6363
}
64+
65+
return errors
6466
}
6567

6668
func (args *Handler) NewPromptStringVar(callback *string, name, defaultValue, usage string, required bool, dependency func() bool) {
@@ -131,8 +133,6 @@ func (args *Handler) newStringPrompt(callback *string, defaultValue, usage strin
131133
}
132134
}
133135
}
134-
135-
return
136136
}
137137

138138
func (args *Handler) newBoolPrompt(callback *bool, defaultValue bool, usage string) {

main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var (
114114
commandTimeout = 60 * time.Second
115115
startTime = time.Now()
116116
metric *metrics.Metrics
117+
initErrors []error
117118
)
118119

119120
func init() {
@@ -130,8 +131,8 @@ func init() {
130131

131132
// Run specific arguments
132133
args.NewPromptStringVar(&outputFile, "output", buildFileName(), "Filename for resulting zip", true, nil)
133-
args.NewPromptStringSliceVar(&enabledModules, "enable", moduleOrder, "Enabled modules for collection (comma seperated)", false, nil)
134-
args.NewPromptStringSliceVar(&disabledModules, "disable", []string{}, "Explicit disabled modules for collection (comma seperated)", false, nil)
134+
args.NewPromptStringSliceVar(&enabledModules, "enable", moduleOrder, "Enabled modules for collection (comma separated)", false, nil)
135+
args.NewPromptStringSliceVar(&disabledModules, "disable", []string{}, "Explicit disabled modules for collection (comma separated)", false, nil)
135136
args.NewPromptBoolVar(&noDetailedCollection, "no-details", false, "Disable detailed collection including logs and more", nil)
136137

137138
// Icinga 2 specific arguments
@@ -160,7 +161,7 @@ func init() {
160161

161162
// Start interactive wizard if interactive is enabled
162163
if !arguments.NonInteractive {
163-
args.CollectArgsFromStdin(strings.Join(moduleOrder, ","))
164+
initErrors = args.CollectArgsFromStdin(strings.Join(moduleOrder, ","))
164165
}
165166

166167
// Verify enabled modules
@@ -193,6 +194,13 @@ func main() {
193194
// Close collection
194195
defer closeCollection()
195196

197+
// Check for errors in init()
198+
if len(initErrors) > 0 {
199+
for _, err := range initErrors {
200+
c.Log.Info(err)
201+
}
202+
}
203+
196204
// Initialize new metrics and defer function to save it to json
197205
metric = metrics.New(getVersion())
198206
defer func() {

0 commit comments

Comments
 (0)