From 4261580a60f0b683c944dba1e208ee5abb0cef01 Mon Sep 17 00:00:00 2001 From: dbb_DingYongliang <62107013+dbbDylan@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:18:10 +0800 Subject: [PATCH] fix: symbolic bug in GatherFacts function (#2411) * fix: replace `bytes.TrimSuffix` with `bytes.TrimSpace` in HostInfo function, to avoid unexpected `\r` symbolic with different os system. * fix: replace trim function with trimSpace * fix bug --- pkg/connector/local_connector.go | 6 +++--- pkg/connector/ssh_connector.go | 10 +++++----- pkg/converter/internal/functions.go | 2 +- pkg/converter/tmpl/template.go | 2 +- pkg/modules/command.go | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/connector/local_connector.go b/pkg/connector/local_connector.go index 4ebd68d26..b6c245908 100644 --- a/pkg/connector/local_connector.go +++ b/pkg/connector/local_connector.go @@ -120,17 +120,17 @@ func (c *localConnector) HostInfo(ctx context.Context) (map[string]any, error) { if err != nil { return nil, fmt.Errorf("get kernel version error: %w", err) } - osVars[_const.VariableOSKernelVersion] = string(bytes.TrimSuffix(kernel, []byte("\n"))) + osVars[_const.VariableOSKernelVersion] = string(bytes.TrimSpace(kernel)) hn, err := c.ExecuteCommand(ctx, "hostname") if err != nil { return nil, fmt.Errorf("get hostname error: %w", err) } - osVars[_const.VariableOSHostName] = string(bytes.TrimSuffix(hn, []byte("\n"))) + osVars[_const.VariableOSHostName] = string(bytes.TrimSpace(hn)) arch, err := c.ExecuteCommand(ctx, "arch") if err != nil { return nil, fmt.Errorf("get arch error: %w", err) } - osVars[_const.VariableOSArchitecture] = string(bytes.TrimSuffix(arch, []byte("\n"))) + osVars[_const.VariableOSArchitecture] = string(bytes.TrimSpace(arch)) // process information procVars := make(map[string]any) diff --git a/pkg/connector/ssh_connector.go b/pkg/connector/ssh_connector.go index 51ffe184b..5f0b5cdb5 100644 --- a/pkg/connector/ssh_connector.go +++ b/pkg/connector/ssh_connector.go @@ -158,8 +158,8 @@ func (c *sshConnector) Init(context.Context) error { return fmt.Errorf("env command error: %w", err) } - if strings.TrimSuffix(string(output), "\n") != "" { - c.shell = strings.TrimSuffix(string(output), "\n") + if strings.TrimSpace(string(output)) != "" { + c.shell = strings.TrimSpace(string(output)) } return nil @@ -293,17 +293,17 @@ func (c *sshConnector) HostInfo(ctx context.Context) (map[string]any, error) { if err != nil { return nil, fmt.Errorf("get kernel version error: %w", err) } - osVars[_const.VariableOSKernelVersion] = string(bytes.TrimSuffix(kernel, []byte("\n"))) + osVars[_const.VariableOSKernelVersion] = string(bytes.TrimSpace(kernel)) hn, err := c.ExecuteCommand(ctx, "hostname") if err != nil { return nil, fmt.Errorf("get hostname error: %w", err) } - osVars[_const.VariableOSHostName] = string(bytes.TrimSuffix(hn, []byte("\n"))) + osVars[_const.VariableOSHostName] = string(bytes.TrimSpace(hn)) arch, err := c.ExecuteCommand(ctx, "arch") if err != nil { return nil, fmt.Errorf("get arch error: %w", err) } - osVars[_const.VariableOSArchitecture] = string(bytes.TrimSuffix(arch, []byte("\n"))) + osVars[_const.VariableOSArchitecture] = string(bytes.TrimSpace(arch)) // process information procVars := make(map[string]any) diff --git a/pkg/converter/internal/functions.go b/pkg/converter/internal/functions.go index 499b87d3b..56f040db3 100644 --- a/pkg/converter/internal/functions.go +++ b/pkg/converter/internal/functions.go @@ -35,7 +35,7 @@ func toYAML(v any) string { return "" } - return strings.TrimSuffix(string(data), "\n") + return strings.TrimSpace(string(data)) } // ipInCIDR get the IP of a specific location within the cidr range diff --git a/pkg/converter/tmpl/template.go b/pkg/converter/tmpl/template.go index d50cc9af5..a1241776b 100644 --- a/pkg/converter/tmpl/template.go +++ b/pkg/converter/tmpl/template.go @@ -68,7 +68,7 @@ func ParseString(ctx map[string]any, input string) (string, error) { } klog.V(6).InfoS(" parse template succeed", "result", result.String()) - return strings.TrimPrefix(strings.TrimSuffix(result.String(), "\n"), "\n"), nil + return strings.Trim(result.String(), "\r\n"), nil } // IsTmplSyntax Check if the string conforms to the template syntax. diff --git a/pkg/modules/command.go b/pkg/modules/command.go index 1161054e8..f54ffd965 100644 --- a/pkg/modules/command.go +++ b/pkg/modules/command.go @@ -48,7 +48,7 @@ func ModuleCommand(ctx context.Context, options ExecOptions) (string, string) { stderr = err.Error() } if data != nil { - stdout = strings.TrimSuffix(string(data), "\n") + stdout = strings.TrimSpace(string(data)) } return stdout, stderr