Skip to content

Commit

Permalink
add error handler for init()
Browse files Browse the repository at this point in the history
  • Loading branch information
tbauriedel committed Aug 28, 2024
1 parent cd4db20 commit 804c69c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New() Handler {
}
}

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

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

errors = append(errors, fmt.Errorf("%s is not matching the needed depenency", argument.Name))
errors = append(errors, fmt.Errorf("Argument '%s' is not matching the needed depenency. Skipping...", argument.Name))
}

return errors
}

func (args *Handler) NewPromptStringVar(callback *string, name, defaultValue, usage string, required bool, dependency func() bool) {
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var (
commandTimeout = 60 * time.Second
startTime = time.Now()
metric *metrics.Metrics
initErrors []error
)

func init() {
Expand Down Expand Up @@ -160,7 +161,7 @@ func init() {

// Start interactive wizard if interactive is enabled
if !arguments.NonInteractive {
args.CollectArgsFromStdin(strings.Join(moduleOrder, ","))
initErrors = args.CollectArgsFromStdin(strings.Join(moduleOrder, ","))
}

// Verify enabled modules
Expand Down Expand Up @@ -193,6 +194,13 @@ func main() {
// Close collection
defer closeCollection()

// Check for errors in init()
if len(initErrors) > 0 {
for _, err := range initErrors {
c.Log.Info(err)
}
}

// Initialize new metrics and defer function to save it to json
metric = metrics.New(getVersion())
defer func() {
Expand Down

0 comments on commit 804c69c

Please sign in to comment.