Skip to content

Commit eca017a

Browse files
committed
refactor: allow more than one LimaUser warning
Signed-off-by: Anders F Björklund <[email protected]>
1 parent e416977 commit eca017a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pkg/osutil/user.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,31 @@ const (
7575

7676
var cache struct {
7777
sync.Once
78-
u *user.User
79-
err error
80-
warning string
78+
u *user.User
79+
err error
80+
warnings []string
8181
}
8282

8383
func LimaUser(warn bool) (*user.User, error) {
84+
cache.warnings = []string{}
8485
cache.Do(func() {
8586
cache.u, cache.err = user.Current()
8687
if cache.err == nil {
8788
// `useradd` only allows user and group names matching the following pattern:
8889
// (it allows a trailing '$', but it feels prudent to map those to the fallback user as well)
8990
validName := "^[a-z_][a-z0-9_-]*$"
9091
if !regexp.MustCompile(validName).Match([]byte(cache.u.Username)) {
91-
cache.warning = fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
92+
warning := fmt.Sprintf("local user %q is not a valid Linux username (must match %q); using %q username instead",
9293
cache.u.Username, validName, fallbackUser)
94+
cache.warnings = append(cache.warnings, warning)
9395
cache.u.Username = fallbackUser
9496
}
9597
}
9698
})
97-
if warn && cache.warning != "" {
98-
logrus.Warn(cache.warning)
99+
if warn && len(cache.warnings) > 0 {
100+
for _, warning := range cache.warnings {
101+
logrus.Warn(warning)
102+
}
99103
}
100104
return cache.u, cache.err
101105
}

0 commit comments

Comments
 (0)