Skip to content

Commit 1c18f60

Browse files
committed
Trim new lines from strings.
Refactor all trimming into single trim func.
1 parent b10ec61 commit 1c18f60

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

helper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"os/exec"
11+
"strings"
1112
)
1213

1314
// run wraps `exec.Command` with easy access to stdout and stderr.
@@ -29,3 +30,7 @@ func protect(appID, id string) string {
2930
func readFile(filename string) ([]byte, error) {
3031
return ioutil.ReadFile(filename)
3132
}
33+
34+
func trim(s string) string {
35+
return strings.TrimSpace(strings.Trim(s, "\n"))
36+
}

helper_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,42 @@ func Test_run_unknown(t *testing.T) {
6767
t.Error("unexpected error, expected exec not found")
6868
}
6969
}
70+
71+
func Test_trim(t *testing.T) {
72+
type args struct {
73+
s string
74+
}
75+
tests := []struct {
76+
name string
77+
args args
78+
want string
79+
}{
80+
{
81+
name: "nil",
82+
args: args{s: ""},
83+
want: "",
84+
},
85+
{
86+
name: "space",
87+
args: args{s: " space "},
88+
want: "space",
89+
},
90+
{
91+
name: "nl",
92+
args: args{s: "data\n"},
93+
want: "data",
94+
},
95+
{
96+
name: "combined",
97+
args: args{s: " some data \n"},
98+
want: "some data",
99+
},
100+
}
101+
for _, tt := range tests {
102+
t.Run(tt.name, func(t *testing.T) {
103+
if got := trim(tt.args.s); got != tt.want {
104+
t.Errorf("trim() = %v, want %v", got, tt.want)
105+
}
106+
})
107+
}
108+
}

id_bsd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package machineid
55
import (
66
"bytes"
77
"os"
8-
"strings"
98
)
109

1110
const hostidPath = "/etc/hostid"
@@ -30,7 +29,7 @@ func readHostid() (string, error) {
3029
if err != nil {
3130
return "", err
3231
}
33-
return strings.TrimSpace(string(buf)), nil
32+
return trim(string(buf)), nil
3433
}
3534

3635
func readKenv() (string, error) {
@@ -39,5 +38,5 @@ func readKenv() (string, error) {
3938
if err != nil {
4039
return "", err
4140
}
42-
return strings.TrimSpace(buf.String()), nil
41+
return trim(buf.String()), nil
4342
}

id_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func machineID() (string, error) {
2121
if err != nil {
2222
return "", err
2323
}
24-
return strings.TrimSpace(id), nil
24+
return trim(id), nil
2525
}
2626

2727
func extractID(lines string) (string, error) {

id_linux.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
package machineid
44

5-
import (
6-
"strings"
7-
)
8-
95
const (
106
// dbusPath is the default path for dbus machine id.
117
dbusPath = "/var/lib/dbus/machine-id"
@@ -27,5 +23,5 @@ func machineID() (string, error) {
2723
if err != nil {
2824
return "", err
2925
}
30-
return strings.TrimSpace(string(id)), nil
26+
return trim(string(id)), nil
3127
}

0 commit comments

Comments
 (0)