Skip to content

Commit bfe61af

Browse files
committed
quadlet should exit non zero on failures
Fixes: containers#18778 Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 189a74d commit bfe61af

File tree

2 files changed

+172
-136
lines changed

2 files changed

+172
-136
lines changed

cmd/quadlet/main.go

+56-36
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ func Logf(format string, a ...interface{}) {
7777
s := fmt.Sprintf(format, a...)
7878
line := fmt.Sprintf("quadlet-generator[%d]: %s", os.Getpid(), s)
7979

80-
if !logToKmsg(line) {
81-
// If we can't log, print to stderr
80+
if !logToKmsg(line) || dryRunFlag {
8281
fmt.Fprintf(os.Stderr, "%s\n", line)
8382
os.Stderr.Sync()
8483
}
@@ -133,13 +132,14 @@ func isExtSupported(filename string) bool {
133132
return ok
134133
}
135134

136-
func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) {
135+
func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) error {
136+
var prevError error
137137
files, err := os.ReadDir(sourcePath)
138138
if err != nil {
139139
if !errors.Is(err, os.ErrNotExist) {
140-
Logf("Can't read \"%s\": %s", sourcePath, err)
140+
return err
141141
}
142-
return
142+
return nil
143143
}
144144

145145
for _, file := range files {
@@ -150,16 +150,20 @@ func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) {
150150
Debugf("Loading source unit file %s", path)
151151

152152
if f, err := parser.ParseUnitFile(path); err != nil {
153-
Logf("Error loading '%s', ignoring: %s", path, err)
153+
err = fmt.Errorf("error loading %q, %w", path, err)
154+
if prevError != nil {
155+
prevError = fmt.Errorf("%s\n%s", prevError, err)
156+
}
154157
} else {
155158
units[name] = f
156159
}
157160
}
158161
}
162+
return prevError
159163
}
160164

161165
func generateServiceFile(service *parser.UnitFile) error {
162-
Debugf("writing '%s'", service.Path)
166+
Debugf("writing %q", service.Path)
163167

164168
service.PrependComment("",
165169
fmt.Sprintf("Automatically generated by %s", os.Args[0]),
@@ -303,15 +307,24 @@ func warnIfAmbiguousName(container *parser.UnitFile) {
303307
}
304308

305309
func main() {
306-
exitCode := 0
310+
if err := process(); err != nil {
311+
Logf("%s", err.Error())
312+
os.Exit(1)
313+
}
314+
os.Exit(0)
315+
}
316+
317+
func process() error {
318+
var prevError error
319+
307320
prgname := path.Base(os.Args[0])
308321
isUserFlag = strings.Contains(prgname, "user")
309322

310323
flag.Parse()
311324

312325
if versionFlag {
313326
fmt.Printf("%s\n", rawversion.RawVersion)
314-
return
327+
return prevError
315328
}
316329

317330
if verboseFlag || dryRunFlag {
@@ -322,9 +335,16 @@ func main() {
322335
noKmsg = true
323336
}
324337

338+
reportError := func(err error) {
339+
if prevError != nil {
340+
err = fmt.Errorf("%s\n%s", prevError, err)
341+
}
342+
prevError = err
343+
}
344+
325345
if !dryRunFlag && flag.NArg() < 1 {
326-
Logf("Missing output directory argument")
327-
os.Exit(1)
346+
reportError(errors.New("missing output directory argument"))
347+
return prevError
328348
}
329349

330350
var outputPath string
@@ -339,21 +359,23 @@ func main() {
339359

340360
units := make(map[string]*parser.UnitFile)
341361
for _, d := range sourcePaths {
342-
loadUnitsFromDir(d, units)
362+
if err := loadUnitsFromDir(d, units); err != nil {
363+
reportError(err)
364+
}
343365
}
344366

345367
if len(units) == 0 {
346368
// containers/podman/issues/17374: exit cleanly but log that we
347369
// had nothing to do
348-
Debugf("No files to parse from %s", sourcePaths)
349-
os.Exit(0)
370+
Debugf("No files parsed from %s", sourcePaths)
371+
return prevError
350372
}
351373

352374
if !dryRunFlag {
353375
err := os.MkdirAll(outputPath, os.ModePerm)
354376
if err != nil {
355-
Logf("Can't create dir %s: %s", outputPath, err)
356-
os.Exit(1)
377+
reportError(err)
378+
return prevError
357379
}
358380
}
359381

@@ -372,33 +394,31 @@ func main() {
372394
case strings.HasSuffix(name, ".network"):
373395
service, err = quadlet.ConvertNetwork(unit, name)
374396
default:
375-
Logf("Unsupported file type '%s'", name)
397+
Logf("Unsupported file type %q", name)
376398
continue
377399
}
378400

379401
if err != nil {
380-
Logf("Error converting '%s', ignoring: %s", name, err)
381-
} else {
382-
service.Path = path.Join(outputPath, service.Filename)
383-
384-
if dryRunFlag {
385-
data, err := service.ToString()
386-
if err != nil {
387-
Debugf("Error parsing %s\n---\n", service.Path)
388-
exitCode = 1
389-
} else {
390-
fmt.Printf("---%s---\n%s\n", service.Path, data)
391-
}
392-
} else {
393-
if err := generateServiceFile(service); err != nil {
394-
Logf("Error writing '%s'o: %s", service.Path, err)
395-
}
396-
enableServiceFile(outputPath, service)
402+
reportError(fmt.Errorf("converting %q: %w", name, err))
403+
continue
404+
}
405+
service.Path = path.Join(outputPath, service.Filename)
406+
407+
if dryRunFlag {
408+
data, err := service.ToString()
409+
if err != nil {
410+
reportError(fmt.Errorf("parsing %s: %w", service.Path, err))
411+
continue
397412
}
413+
fmt.Printf("---%s---\n%s\n", service.Path, data)
414+
continue
398415
}
416+
if err := generateServiceFile(service); err != nil {
417+
reportError(fmt.Errorf("generating service file %s: %w", service.Path, err))
418+
}
419+
enableServiceFile(outputPath, service)
399420
}
400-
401-
os.Exit(exitCode)
421+
return prevError
402422
}
403423

404424
func init() {

0 commit comments

Comments
 (0)