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

Code before:
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.cli;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import javax.annotation.Nullable;

/** A class to determine cache locations for any cli commands. */
public class CacheDirectories {

  private static final String APPLICATION_LAYER_CACHE_DIR = "application-layers";
  private static final String EXPLODED_JAR_CACHE_DIR = "exploded-jar";

  @Nullable private final Path baseImageCache;
  private final Path projectCache;

  /**
   * Create a caches helper for cli cache locations.
   *
   * @param commonCliOptions cli options for user configured cache directories
   * @param contextRoot the context root, use the parent directory of single files, this context
   *     root must exist
   * @return an instance of CacheDirectories with cli specific cache locations
   */
  public static CacheDirectories from(CommonCliOptions commonCliOptions, Path contextRoot) {
    Preconditions.checkArgument(
        Files.isDirectory(contextRoot),
        "contextRoot must be a directory, but " + contextRoot.toString() + " is not.");
    return new CacheDirectories(
        commonCliOptions.getBaseImageCache().orElse(null),
        commonCliOptions
            .getProjectCache()
            .orElse(
                Paths.get(System.getProperty("java.io.tmpdir"))
                    .resolve("jib-cli-cache")
                    .resolve("projects")
                    .resolve(getProjectCacheDirectoryFromProject(contextRoot))));

Review comment (performance):
I think `CACHE` is misleading. The primary purpose is to have an intermediate storage for further processing, not like keeping the contents for caching.

Code after:
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.cli;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import javax.annotation.Nullable;

/** A class to determine cache locations for any cli commands. */
public class CacheDirectories {

  private static final String APPLICATION_LAYER_CACHE_DIR = "application-layers";
  private static final String EXPLODED_JAR_DIR = "exploded-jar";

  @Nullable private final Path baseImageCache;
  private final Path projectCache;

  /**
   * Create a caches helper for cli cache locations.
   *
   * @param commonCliOptions cli options for user configured cache directories
   * @param contextRoot the context root, use the parent directory of single files, this context
   *     root must exist
   * @return an instance of CacheDirectories with cli specific cache locations
   */
  public static CacheDirectories from(CommonCliOptions commonCliOptions, Path contextRoot) {
    Preconditions.checkArgument(
        Files.isDirectory(contextRoot),
        "contextRoot must be a directory, but " + contextRoot.toString() + " is not.");
    return new CacheDirectories(
        commonCliOptions.getBaseImageCache().orElse(null),
        commonCliOptions
            .getProjectCache()
            .orElse(
                Paths.get(System.getProperty("java.io.tmpdir"))
                    .resolve("jib-cli-cache")
                    .resolve("projects")
                    .resolve(getProjectCacheDirectoryFromProject(contextRoot))));