Skip to content

Commit e5b4e3a

Browse files
committed
fix logging non-existent path
Signed-off-by: Songpon Srisawai <songpon.ssw@gmail.com>
1 parent dd8a2e1 commit e5b4e3a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pkg/limayaml/validate.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,42 +144,48 @@ func Validate(y *LimaYAML, warn bool) error {
144144
}
145145
}
146146

147+
var mountErrs []error
147148
for i, f := range y.Mounts {
148149
if !filepath.IsAbs(f.Location) && !strings.HasPrefix(f.Location, "~") {
149-
return fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q",
150-
i, f.Location)
150+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q", i, f.Location))
151151
}
152152
// f.Location has already been expanded in FillDefaults(), but that function cannot return errors.
153153
loc, err := localpathutil.Expand(f.Location)
154154
if err != nil {
155-
return fmt.Errorf("field `mounts[%d].location` refers to an unexpandable path: %q: %w", i, f.Location, err)
155+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].location` refers to an unexpandable path: %q: %w", i, f.Location, err))
156156
}
157157
st, err := os.Stat(loc)
158158
if err != nil {
159-
if !errors.Is(err, os.ErrNotExist) {
160-
return fmt.Errorf("field `mounts[%d].location` refers to an inaccessible path: %q: %w", i, f.Location, err)
159+
if errors.Is(err, os.ErrNotExist) {
160+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].location` refers to non-existent path: %q: %w", i, f.Location, err))
161+
continue
161162
}
163+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].location` refers to an inaccessible path: %q: %w", i, f.Location, err))
162164
} else if !st.IsDir() {
163-
return fmt.Errorf("field `mounts[%d].location` refers to a non-directory path: %q: %w", i, f.Location, err)
165+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].location` refers to a non-directory path: %q: %w", i, f.Location, err))
164166
}
165167

166168
switch *f.MountPoint {
167169
case "/", "/bin", "/dev", "/etc", "/home", "/opt", "/sbin", "/tmp", "/usr", "/var":
168-
return fmt.Errorf("field `mounts[%d].mountPoint` must not be a system path such as /etc or /usr", i)
170+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].mountPoint` must not be a system path such as /etc or /usr", i))
169171
// home directory defined in "cidata.iso:/user-data"
170172
case *y.User.Home:
171-
return fmt.Errorf("field `mounts[%d].mountPoint` is the reserved internal home directory %q", i, *y.User.Home)
173+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].mountPoint` is the reserved internal home directory %q", i, *y.User.Home))
172174
}
173175
// There is no tilde-expansion for guest filenames
174176
if strings.HasPrefix(*f.MountPoint, "~") {
175-
return fmt.Errorf("field `mounts[%d].mountPoint` must not start with \"~\"", i)
177+
mountErrs = append(mountErrs, fmt.Errorf("field `mounts[%d].mountPoint` must not start with \"~\"", i))
176178
}
177179

178180
if _, err := units.RAMInBytes(*f.NineP.Msize); err != nil {
179-
return fmt.Errorf("field `msize` has an invalid value: %w", err)
181+
mountErrs = append(mountErrs, fmt.Errorf("field `msize` has an invalid value: %w", err))
180182
}
181183
}
182184

185+
if len(mountErrs) > 0 {
186+
return fmt.Errorf("mounts validation failed:\n%w", errors.Join(mountErrs...))
187+
}
188+
183189
if *y.SSH.LocalPort != 0 {
184190
if err := validatePort("ssh.localPort", *y.SSH.LocalPort); err != nil {
185191
return err

0 commit comments

Comments
 (0)