InfoCommand.java

/*
 *    Copyright 2010-2023 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    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 org.apache.ibatis.migration.commands;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Properties;

public final class InfoCommand implements Command {
  private final PrintStream out;

  public InfoCommand(PrintStream out) {
    this.out = out;
  }

  @Override
  public void execute(String... params) {
    Properties properties = new Properties();
    try (InputStream is = getClass().getResourceAsStream("/mybatis-migrations.properties")) {
      if (is != null) {
        properties.load(is);
      }
    } catch (IOException e) {
      // ignore
    }

    out.printf("%s %s (%s)%n", properties.getProperty("name"), properties.getProperty("version"),
        properties.getProperty("build"));
    out.printf("Java version: %s, vendor: %s%n", System.getProperty("java.version"), System.getProperty("java.vendor"));
    out.printf("Java home: %s%n", System.getProperty("java.home"));
    out.printf("Default locale: %s, platform encoding: %s%n", Locale.getDefault().toLanguageTag(),
        Charset.defaultCharset().name());
    out.printf("OS name: \"%s\", version: \"%s\", arch: \"%s\", family: \"%s\"%n", System.getProperty("os.name"),
        System.getProperty("os.version"), System.getProperty("os.arch"), getOsFamily());
  }

  private static String getOsFamily() {
    String osName = System.getProperty("os.name").toLowerCase();
    String pathSep = File.pathSeparator;

    if (osName.indexOf("windows") != -1) {
      return "windows";
    }

    if (osName.indexOf("os/2") != -1) {
      return "os/2";
    }

    if (osName.indexOf("z/os") != -1 || osName.indexOf("os/390") != -1) {
      return "z/os";
    }

    if (osName.indexOf("os/400") != -1) {
      return "os/400";
    }

    if (pathSep.equals(";")) {
      return "dos";
    }

    if (osName.indexOf("mac") != -1) {
      if (osName.endsWith("x")) {
        return "mac"; // MACOSX
      }
      return "unix";
    }

    if (osName.indexOf("nonstop_kernel") != -1) {
      return "tandem";
    }

    if (osName.indexOf("openvms") != -1) {
      return "openvms";
    }

    if (pathSep.equals(":")) {
      return "unix";
    }

    return "undefined";
  }

}