001/* 002 * geordi 003 * 004 * Copyright (C) 2018 Richard "Shred" Körber 005 * https://github.com/shred/geordi 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 015 */ 016package org.shredzone.geordi; 017 018import org.apache.commons.cli.CommandLine; 019import org.apache.commons.cli.CommandLineParser; 020import org.apache.commons.cli.DefaultParser; 021import org.apache.commons.cli.HelpFormatter; 022import org.apache.commons.cli.Options; 023import org.apache.commons.cli.ParseException; 024 025import com.google.inject.Guice; 026import com.google.inject.Injector; 027 028/** 029 * Geordi's main class. 030 */ 031public class Geordi { 032 033 /** 034 * Runs Geordi. 035 * 036 * @param args 037 * Command line parameters 038 */ 039 public static void main(String[] args) { 040 Options options = new Options(); 041 options.addOption("d", "database", true, "database URL"); 042 options.addOption("u", "user", true, "database user"); 043 options.addOption("p", "password", true, "database password"); 044 045 try { 046 CommandLineParser parser = new DefaultParser(); 047 CommandLine cmd = parser.parse(options, args); 048 049 GeordiModule module = new GeordiModule(); 050 module.setDatabaseHost(getDatabaseHost(cmd)); 051 module.setDatabaseUser(getDatabaseUser(cmd)); 052 module.setDatabasePassword(getDatabasePassword(cmd)); 053 054 Injector injector = Guice.createInjector(module); 055 GeordiRunner runner = injector.getInstance(GeordiRunner.class); 056 runner.start(); 057 } catch (ParseException ex) { 058 HelpFormatter help = new HelpFormatter(); 059 help.printHelp("geordi", options, true); 060 System.exit(1); 061 } 062 } 063 064 private static String getDatabaseHost(CommandLine cmd) { 065 String database = "jdbc:postgresql://localhost/geordi"; 066 067 if (cmd.hasOption("database")) { 068 database = cmd.getOptionValue("database"); 069 } else { 070 String env = System.getenv("GEORDI_DATABASE"); 071 if (env != null) { 072 database = env.trim(); 073 } 074 } 075 076 if (!database.contains("://")) { 077 database = "jdbc:postgresql://" + database; 078 } 079 080 if (!database.startsWith("jdbc:")) { 081 database = "jdbc:" + database; 082 } 083 084 return database; 085 } 086 087 private static String getDatabaseUser(CommandLine cmd) { 088 if (cmd.hasOption("user")) { 089 return cmd.getOptionValue("user"); 090 } else { 091 String env = System.getenv("GEORDI_USER"); 092 if (env != null) { 093 return env.trim(); 094 } 095 } 096 097 return null; 098 } 099 100 private static String getDatabasePassword(CommandLine cmd) { 101 if (cmd.hasOption("password")) { 102 return cmd.getOptionValue("password"); 103 } else { 104 String env = System.getenv("GEORDI_PASSWORD"); 105 if (env != null) { 106 return env; 107 } 108 } 109 110 return null; 111 } 112 113}