quadlet should exit non zero on failures

Fixes: #18778

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2023-06-15 19:12:17 -04:00
parent 189a74d345
commit bfe61af6d7
2 changed files with 170 additions and 134 deletions

View File

@ -77,8 +77,7 @@ func Logf(format string, a ...interface{}) {
s := fmt.Sprintf(format, a...)
line := fmt.Sprintf("quadlet-generator[%d]: %s", os.Getpid(), s)
if !logToKmsg(line) {
// If we can't log, print to stderr
if !logToKmsg(line) || dryRunFlag {
fmt.Fprintf(os.Stderr, "%s\n", line)
os.Stderr.Sync()
}
@ -133,13 +132,14 @@ func isExtSupported(filename string) bool {
return ok
}
func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) {
func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) error {
var prevError error
files, err := os.ReadDir(sourcePath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
Logf("Can't read \"%s\": %s", sourcePath, err)
return err
}
return
return nil
}
for _, file := range files {
@ -150,16 +150,20 @@ func loadUnitsFromDir(sourcePath string, units map[string]*parser.UnitFile) {
Debugf("Loading source unit file %s", path)
if f, err := parser.ParseUnitFile(path); err != nil {
Logf("Error loading '%s', ignoring: %s", path, err)
err = fmt.Errorf("error loading %q, %w", path, err)
if prevError != nil {
prevError = fmt.Errorf("%s\n%s", prevError, err)
}
} else {
units[name] = f
}
}
}
return prevError
}
func generateServiceFile(service *parser.UnitFile) error {
Debugf("writing '%s'", service.Path)
Debugf("writing %q", service.Path)
service.PrependComment("",
fmt.Sprintf("Automatically generated by %s", os.Args[0]),
@ -303,7 +307,16 @@ func warnIfAmbiguousName(container *parser.UnitFile) {
}
func main() {
exitCode := 0
if err := process(); err != nil {
Logf("%s", err.Error())
os.Exit(1)
}
os.Exit(0)
}
func process() error {
var prevError error
prgname := path.Base(os.Args[0])
isUserFlag = strings.Contains(prgname, "user")
@ -311,7 +324,7 @@ func main() {
if versionFlag {
fmt.Printf("%s\n", rawversion.RawVersion)
return
return prevError
}
if verboseFlag || dryRunFlag {
@ -322,9 +335,16 @@ func main() {
noKmsg = true
}
reportError := func(err error) {
if prevError != nil {
err = fmt.Errorf("%s\n%s", prevError, err)
}
prevError = err
}
if !dryRunFlag && flag.NArg() < 1 {
Logf("Missing output directory argument")
os.Exit(1)
reportError(errors.New("missing output directory argument"))
return prevError
}
var outputPath string
@ -339,21 +359,23 @@ func main() {
units := make(map[string]*parser.UnitFile)
for _, d := range sourcePaths {
loadUnitsFromDir(d, units)
if err := loadUnitsFromDir(d, units); err != nil {
reportError(err)
}
}
if len(units) == 0 {
// containers/podman/issues/17374: exit cleanly but log that we
// had nothing to do
Debugf("No files to parse from %s", sourcePaths)
os.Exit(0)
Debugf("No files parsed from %s", sourcePaths)
return prevError
}
if !dryRunFlag {
err := os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
Logf("Can't create dir %s: %s", outputPath, err)
os.Exit(1)
reportError(err)
return prevError
}
}
@ -372,33 +394,31 @@ func main() {
case strings.HasSuffix(name, ".network"):
service, err = quadlet.ConvertNetwork(unit, name)
default:
Logf("Unsupported file type '%s'", name)
Logf("Unsupported file type %q", name)
continue
}
if err != nil {
Logf("Error converting '%s', ignoring: %s", name, err)
} else {
service.Path = path.Join(outputPath, service.Filename)
if dryRunFlag {
data, err := service.ToString()
if err != nil {
Debugf("Error parsing %s\n---\n", service.Path)
exitCode = 1
} else {
fmt.Printf("---%s---\n%s\n", service.Path, data)
}
} else {
if err := generateServiceFile(service); err != nil {
Logf("Error writing '%s'o: %s", service.Path, err)
}
enableServiceFile(outputPath, service)
}
reportError(fmt.Errorf("converting %q: %w", name, err))
continue
}
}
service.Path = path.Join(outputPath, service.Filename)
os.Exit(exitCode)
if dryRunFlag {
data, err := service.ToString()
if err != nil {
reportError(fmt.Errorf("parsing %s: %w", service.Path, err))
continue
}
fmt.Printf("---%s---\n%s\n", service.Path, data)
continue
}
if err := generateServiceFile(service); err != nil {
reportError(fmt.Errorf("generating service file %s: %w", service.Path, err))
}
enableServiceFile(outputPath, service)
}
return prevError
}
func init() {