From 6cbd8cb0072d72084b0e300b9d0d43c27bf61905 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 14 May 2020 14:21:34 +0200 Subject: [PATCH] [Elastic Agent] Cleaner output of inspect command (#18405) * no stack on inspect warnings --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + .../pkg/agent/application/introspect_config_cmd.go | 2 +- .../pkg/agent/application/introspect_output_cmd.go | 11 +++++------ x-pack/elastic-agent/pkg/agent/errors/error.go | 5 +++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 1c0f62cff7f..5e909ac9ed0 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -55,4 +55,5 @@ - Enable introspecting configuration {pull}18124[18124] - Follow home path for all config files {pull}18161[18161] - Use nested objects so fleet can handle metadata correctly {pull}18234[18234] +- More clear output of inspect command {pull}18405[18405] - Pick up version from libbeat {pull}18350[18350] diff --git a/x-pack/elastic-agent/pkg/agent/application/introspect_config_cmd.go b/x-pack/elastic-agent/pkg/agent/application/introspect_config_cmd.go index f5ab634b84d..bb5d25c2c35 100644 --- a/x-pack/elastic-agent/pkg/agent/application/introspect_config_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/introspect_config_cmd.go @@ -53,7 +53,7 @@ func (c *IntrospectConfigCmd) introspectConfig() error { if err != nil { return err } else if fleetConfig == nil { - return errors.New("no fleet config retrieved yet") + return fmt.Errorf("no fleet config retrieved yet") } return printMapStringConfig(fleetConfig) diff --git a/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go b/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go index 26eb424fe97..cb7c185fbd5 100644 --- a/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go @@ -13,7 +13,6 @@ import ( "github.com/urso/ecslog/backend/layout" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/filters" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" @@ -69,7 +68,7 @@ func (c *IntrospectOutputCmd) introspectOutputs() error { if err != nil { return err } else if fleetConfig == nil { - return errors.New("no fleet config retrieved yet") + return fmt.Errorf("no fleet config retrieved yet") } return listOutputsFromMap(l, fleetConfig) @@ -122,7 +121,7 @@ func (c *IntrospectOutputCmd) introspectOutput() error { if err != nil { return err } else if fleetConfig == nil { - return errors.New("no fleet config retrieved yet") + return fmt.Errorf("no fleet config retrieved yet") } return printOutputFromMap(l, c.output, c.program, fleetConfig) @@ -153,16 +152,16 @@ func printOutputFromConfig(log *logger.Logger, output, programName string, cfg * } if !programFound { - fmt.Printf("program '%s' is not recognized within output '%s', try running `elastic-agent introspect output` to find available outputs.\n", + return fmt.Errorf("program '%s' is not recognized within output '%s', try running `elastic-agent introspect output` to find available outputs", programName, output) } + return nil } - fmt.Printf("output '%s' is not recognized, try running `elastic-agent introspect output` to find available outputs.\n", output) + return fmt.Errorf("output '%s' is not recognized, try running `elastic-agent introspect output` to find available outputs", output) - return nil } func printOutputFromMap(log *logger.Logger, output, programName string, cfg map[string]interface{}) error { diff --git a/x-pack/elastic-agent/pkg/agent/errors/error.go b/x-pack/elastic-agent/pkg/agent/errors/error.go index c972ed1f032..c3c1d6a5ddb 100644 --- a/x-pack/elastic-agent/pkg/agent/errors/error.go +++ b/x-pack/elastic-agent/pkg/agent/errors/error.go @@ -27,6 +27,11 @@ type agentError struct { meta map[string]interface{} } +// Unwrap returns nested error. +func (e agentError) Unwrap() error { + return e.err +} + // Error returns a string consisting of a message and originating error. func (e agentError) Error() string { if e.msg != "" {