Repository: apple/pkl
File: pkl-cli/src/main/kotlin/org/pkl/cli/CliFormatterCommand.kt (Kotlin)

Code before:

    handleSources(status)

    when (status.status) {
      FORMATTING_VIOLATION -> {
        // using CliTestException instead of CliException because we want full control on how to
        // print errors
        throw CliTestException("", status.status)
      }
      ERROR -> {
        if (!silent) {
          writeErr("An error occurred during formatting.")
        }
        throw CliTestException("", status.status)
      }
    }
  }

  private fun handleSources(status: Status) {
    for (source in allSources()) {
      val path = if (source.uri == VmUtils.REPL_TEXT_URI) Path.of("-") else Path.of(source.uri)
      try {
        val contents =
          if (source.contents != null) {
            if (overwrite) {
              writeErr("Cannot write formatted output for stdin input.")
              throw CliTestException("", ERROR)
            }
            source.contents!!
          } else {
            File(source.uri).readText()
          }

        val formatted = format(contents)
        if (contents != formatted) {
          status.update(FORMATTING_VIOLATION)
          if (names || overwrite) {
            // if `--names` or `-w` is specified, only write file names
            write(path.toAbsolutePath().toString())
          }

          if (overwrite) {
            path.writeText(formatted, Charsets.UTF_8)
          }
        }

        if (!names && !overwrite) {
          write(formatted)
        }
      } catch (pe: GenericParserError) {
        writeErr("Could not format `$path`: $pe")

Review comment (suggestion):
```suggestion
              writeErr("Cannot write to stdin.")
```

Code after:

    handleSources(status)

    when (status.status) {
      FORMATTING_VIOLATION -> {
        // using CliTestException instead of CliException because we want full control on how to
        // print errors
        throw CliTestException("", status.status)
      }
      ERROR -> {
        if (!silent) {
          writeErr("An error occurred during formatting.")
        }
        throw CliTestException("", status.status)
      }
    }
  }

  private fun handleSources(status: Status) {
    for (source in allSources()) {
      val path = if (source.uri == VmUtils.REPL_TEXT_URI) Path.of("-") else Path.of(source.uri)
      try {
        val contents =
          if (source.contents != null) {
            if (overwrite) {
              writeErr("Cannot write to stdin.")
              throw CliTestException("", ERROR)
            }
            source.contents!!
          } else {
            File(source.uri).readText()
          }

        val formatted = format(contents)
        if (contents != formatted) {
          status.update(FORMATTING_VIOLATION)
          if (diffNameOnly || overwrite) {
            // if `--diff-name-only` or `-w` is specified, only write file names
            write(path.toAbsolutePath().toString())
          }

          if (overwrite) {
            path.writeText(formatted, Charsets.UTF_8)
          }
        }

        if (!diffNameOnly && !overwrite) {
          write(formatted)
        }
      } catch (pe: GenericParserError) {
        writeErr("Could not format `$path`: $pe")