Changeset 34897


Ignore:
Timestamp:
2021-02-18T16:01:53+13:00 (3 years ago)
Author:
su41
Message:

Changed runnable into callable so that we could handle exit codes better

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tipple-bridge/trunk/src/src/main/java/sqlCli.java

    r34896 r34897  
    99import com.google.gson.Gson;
    1010import java.io.FileWriter;
     11import java.util.concurrent.Callable;
    1112
    1213/*
     
    2526 */
    2627@Command (name="sqlCli", description = "Arguments for database connection", mixinStandardHelpOptions = true)
    27 public class sqlCli implements Runnable {
     28public class sqlCli implements Callable<Integer> {
    2829    //our cli arguments
    2930
     
    5455    String projectID;
    5556
    56 
    57     /*
    58     this parameter can be read in at any point on the options line.
    59     but that's only because it's the only parameter at the moment.
    60     If we add more parameters, will either have to put them next to each other
    61     OR add in indexes
    62     https://picocli.info/quick-guide.html#_options_and_parameters
    63      */
    64     @Parameters(description = "Name of the output JSON file. E.g. yourFileName.json")
    65     String jsonOutputFile;
    66 
    6757    @Option(names = {"--host"}, description = "Type of host e.g. localhost ",
    6858            required = true, defaultValue = "localhost")
    6959    String host;
    7060
    71     public void run(){
     61    /*
     62        this parameter can be read in at any point on the options line.
     63        but that's only because it's the only parameter at the moment.
     64        If we add more parameters, will either have to put them next to each other
     65        OR add in indexes
     66        https://picocli.info/quick-guide.html#_options_and_parameters
     67         */
     68    @Parameters(description = "Name of the output JSON file. E.g. yourFileName.json")
     69    String jsonOutputFile;
     70
     71    public Integer call(){
    7272        //if it doesn't end with ...
    7373        if(!jsonOutputFile.endsWith(".json")){
     
    7575            System.out.println("The file to save the output to must be a .json file.");
    7676            //exit code other than 0 means it's not fine.
    77             System.exit(1);
    78         }
    79         //else we're good to go.
    80         databaseConnect(userName, password, port, dbName, projectID, jsonOutputFile, host);
     77            return 1;
     78        }
     79        //else we're good to go.
     80       // databaseConnect(userName, password, port, dbName, projectID, jsonOutputFile, host);
     81        int status = databaseConnect(userName, password, port, dbName, projectID, jsonOutputFile, host);
     82        return status;
    8183    }
    8284
     
    8789
    8890
    89     private static void databaseConnect(String user, String pass, int port, String dbName,
     91    private static int databaseConnect(String user, String pass, int port, String dbName,
    9092                                        String projectID, String fileName, String host){
    9193        Connection conn;
    9294        Statement stmt;
    93 
     95        int returnStatus = 0;
    9496       // String dbUrl = "jdbc:mysql://localhost:" + port +"/"+ dbName;
    9597
     
    215217               System.out.println("    -'Database name' ");
    216218            }
     219           returnStatus = 1;
    217220
    218221        }
     
    222225            //print out wrong username or password instead of the stack trace?
    223226            System.out.println("encountered error");
    224 
    225         }
     227            returnStatus = 2;
     228        }
     229        return returnStatus;
    226230    }
    227231}
Note: See TracChangeset for help on using the changeset viewer.