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 25c7882
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
29 changes: 17 additions & 12 deletions internal/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ 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
errors := make([]error, 0, len(args.arguments))

for _, argument := range args.arguments {
if argument.Dependency == nil {
Expand All @@ -59,8 +59,12 @@ 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))
}

fmt.Print("\nInteractive wizard finished. Starting...\n\n")

return errors
}

func (args *Handler) NewPromptStringVar(callback *string, name, defaultValue, usage string, required bool, dependency func() bool) {
Expand Down Expand Up @@ -113,26 +117,27 @@ func (args *Handler) NewPromptBoolVar(callback *bool, name string, defaultValue
func (args *Handler) newStringPrompt(callback *string, defaultValue, usage string, required bool) {
for {
fmt.Printf("%s - (Preselection: '%s'): ", usage, defaultValue)

if args.scanner.Scan() {
input := args.scanner.Text()
if input != "" {

switch {
case input != "":
*callback = input
break
} else if input == "" && defaultValue != "" {
return
case input == "" && defaultValue != "":
*callback = defaultValue
break
} else if input == "" && !required {
break
return
case input == "" && !required:
return
}
} else {
if err := args.scanner.Err(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "reading standard input:", err)
break
return
}
}
}

return
}

func (args *Handler) newBoolPrompt(callback *bool, defaultValue bool, usage string) {
Expand Down
20 changes: 14 additions & 6 deletions 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 All @@ -130,14 +131,14 @@ func init() {

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

// Icinga 2 specific arguments
args.NewPromptStringVar(&icinga2.APICred.Username, "icinga2-api-user", "", "Username of global Icinga 2 API user to collect data about Icinga 2 Infrastructure", false, icinga2Enabled)
args.NewPromptStringVar(&icinga2.APICred.Password, "icinga2-api-pass", "", "Password for global Icinga 2 API user to collect data about Icinga 2 Infrastructure", false, icinga2Enabled)
args.NewPromptStringSliceVar(&icinga2.APIEndpoints, "icinga2-api-endpoints", []string{}, "Comma separated list of Icinga 2 API Endpoints (including port) to collect data from. FQDN or IP address must be reachable. (Example: i2-master01.local:5665)", false, icinga2Enabled)
args.NewPromptStringVar(&icinga2.APICred.Username, "icinga2-api-user", "", "Icinga 2: Username of global API user to collect data about Icinga 2 Infrastructure", false, icinga2Enabled)
args.NewPromptStringVar(&icinga2.APICred.Password, "icinga2-api-pass", "", "Icinga 2: Password for global API user to collect data about Icinga 2 Infrastructure", false, icinga2Enabled)
args.NewPromptStringSliceVar(&icinga2.APIEndpoints, "icinga2-api-endpoints", []string{}, "Icinga 2: Comma separated list of API Endpoints (including port) to collect data from. FQDN or IP address must be reachable. (Example: i2-master01.local:5665)", false, icinga2Enabled)

flag.CommandLine.SortFlags = false

Expand All @@ -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 25c7882

Please sign in to comment.