Skip to content

Commit

Permalink
Merge pull request containers#18828 from rhatdan/quadlet
Browse files Browse the repository at this point in the history
quadlet should exit non zero on failures
  • Loading branch information
openshift-merge-robot authored Jun 18, 2023
2 parents 030213c + bfe61af commit feea666
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 136 deletions.
92 changes: 56 additions & 36 deletions cmd/quadlet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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]),
Expand Down Expand Up @@ -303,15 +307,24 @@ 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")

flag.Parse()

if versionFlag {
fmt.Printf("%s\n", rawversion.RawVersion)
return
return prevError
}

if verboseFlag || dryRunFlag {
Expand All @@ -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
Expand All @@ -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
}
}

Expand All @@ -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)

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)
}

os.Exit(exitCode)
return prevError
}

func init() {
Expand Down
Loading

0 comments on commit feea666

Please sign in to comment.