All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.sitebricks.cloud.Doctor Maven / Gradle / Ivy

The newest version!
package com.google.sitebricks.cloud;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author [email protected] (Dhanji R. Prasanna)
 */
class Doctor implements Command {
  private static final Pattern DIR_REGEX = Pattern.compile("[_$\\w][_$@.\\w\\d]+");
  private volatile List messages = new ArrayList();

  @Override
  public void run(List commands, Config config) {
    try {
      // check app.yml
      File appYaml = new File("app.yml");
      check(appYaml.exists(), "Not a valid sitebricks project. Hint: run 'sitebricks init '");

      Yaml yaml = new Yaml();
      @SuppressWarnings("unchecked")
      Map appConfig = (Map) yaml.load(new FileReader(appYaml));
      check(!appConfig.isEmpty(), "No app types are defined in app.yml! Define a new process");

      for (Map.Entry entry : appConfig.entrySet()) {
        check(DIR_REGEX.matcher(entry.getKey()).matches(),
            "Invalid app name (must not have special characters or spaces): " + entry.getKey());
        @SuppressWarnings("unchecked")
        Map appValue = (Map) entry.getValue();

        check(appValue.containsKey("main"), "App definition is missing 'main' declaration: "
            + entry.getKey());
      }

    } catch (Exception e) {
      // Keep going until we can't anymore.
      if (messages.isEmpty())
        throw new RuntimeException(e);

      for (int i = 0, messagesSize = messages.size(); i < messagesSize; i++) {
        String message = messages.get(i);
        System.out.print(i + 1 + ") " + message);
      }
    }
  }

  private void check(boolean test, String message) {
    if (test)
      return;

    messages.add(message);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy