Skip to content

Commit

Permalink
fix: symbolic bug in GatherFacts function (#2411)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dbbDylan authored Sep 20, 2024
1 parent e4957a6 commit 4261580
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/connector/local_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/connector/ssh_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/converter/internal/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/converter/tmpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4261580

Please sign in to comment.