Repository: GoogleContainerTools/jib
File: jib-cli/src/main/java/com/google/cloud/tools/jib/cli/cli2/Containerizers.java (Java)

Code before:
      throws InvalidImageReferenceException {
    String imageSpec = buildOptions.getTargetImage();
    if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
      // TODO: allow setting docker env and docker executable (along with path/env)
      return Containerizer.to(
          DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
    }
    if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
      return Containerizer.to(
          TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, "")))
              .named(buildOptions.getName()));
    }
    ImageReference imageReference =
        ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    applyCredentialConfig(
        registryImage,
        CredentialRetrieverFactory.forImage(
            imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())),
        buildOptions);
    return Containerizer.to(registryImage);
  }

  static void applyCredentialConfig(
      RegistryImage registryImage, CredentialRetrieverFactory factory, JibCli buildOptions) {
    if (buildOptions.getUsernamePassword().isPresent()) {
      registryImage.addCredentialRetriever(buildOptions::getUsernamePassword);
    } else if (buildOptions.getToUsernamePassword().isPresent()) {
      registryImage.addCredentialRetriever(buildOptions::getToUsernamePassword);
    }
    for (String credentialHelper : buildOptions.getCredentialHelpers()) {
      Path path = Paths.get(credentialHelper);
      if (Files.exists(path)) {
        registryImage.addCredentialRetriever(factory.dockerCredentialHelper(path));
      } else {
        registryImage.addCredentialRetriever(factory.dockerCredentialHelper(credentialHelper));
      }
    }
    // then add any other known helpers
    registryImage.addCredentialRetriever(factory.dockerConfig());
    registryImage.addCredentialRetriever(factory.wellKnownCredentialHelpers());
    registryImage.addCredentialRetriever(factory.googleApplicationDefaultCredentials());
  }

  static void applyConfiguration(Containerizer containerizer, JibCli buildOptions) {
    containerizer.setToolName(VersionInfo.TOOL_NAME);
    containerizer.setToolVersion(VersionInfo.getVersionSimple());

    // TODO: it's strange that we use system properties to set these
    // TODO: perhaps we should expose these as configuration options on the containerizer
    if (buildOptions.isSendCredentialsOverHttp()) {

Review comment (suggestion):
It's not possible to have both `--username` and `--to-username` given, right? Is it worth checking that here?
```
Preconditions.checkArgument(!(buildOptions.getUsernamePassword().isPresent() && buildOptinos.getToUsernamePassword.isPresent()));
```

Code after:

  private static Containerizer create(JibCli buildOptions, ConsoleLogger logger)
      throws InvalidImageReferenceException, FileNotFoundException {
    String imageSpec = buildOptions.getTargetImage();
    if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
      // TODO: allow setting docker env and docker executable (along with path/env)
      return Containerizer.to(
          DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
    }
    if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
      return Containerizer.to(
          TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, "")))
              .named(buildOptions.getName()));
    }
    ImageReference imageReference =
        ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    DefaultCredentialRetrievers defaultCredentialRetrievers =
        DefaultCredentialRetrievers.init(
            CredentialRetrieverFactory.forImage(
                imageReference,
                logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
    Credentials.getToCredentialRetrievers(buildOptions, defaultCredentialRetrievers)
        .forEach(registryImage::addCredentialRetriever);
    return Containerizer.to(registryImage);
  }

  private static void applyConfiguration(Containerizer containerizer, JibCli buildOptions) {
    containerizer.setToolName(VersionInfo.TOOL_NAME);
    containerizer.setToolVersion(VersionInfo.getVersionSimple());

    // TODO: it's strange that we use system properties to set these
    // TODO: perhaps we should expose these as configuration options on the containerizer
    if (buildOptions.isSendCredentialsOverHttp()) {
      System.setProperty(JibSystemProperties.SEND_CREDENTIALS_OVER_HTTP, Boolean.TRUE.toString());
    }
    if (buildOptions.isSerialize()) {
      System.setProperty(JibSystemProperties.SERIALIZE, Boolean.TRUE.toString());
    }

    containerizer.setAllowInsecureRegistries(buildOptions.isAllowInsecureRegistries());
    buildOptions.getBaseImageCache().ifPresent(containerizer::setBaseImageLayersCache);
    buildOptions.getApplicationCache().ifPresent(containerizer::setApplicationLayersCache);

    buildOptions.getAdditionalTags().forEach(containerizer::withAdditionalTag);
  }

  private static void applyHandlers(Containerizer containerizer, ConsoleLogger consoleLogger) {
    containerizer
        .addEventHandler(
            LogEvent.class,