private static P4ConnectStruct SelectCorrectInstance(MySqlConnection _conn, ArrayList alResultats) { if (alResultats.Count == 1) { return((P4ConnectStruct)alResultats[0]); } Console.Clear(); Console.WriteLine("{0} Instances match your criteria.\n", alResultats.Count); for (int i = 0; i < alResultats.Count; i++) { P4ConnectStruct p4Instance = (P4ConnectStruct)alResultats[i]; Console.WriteLine("{0}. {1} (owner = {2})\n", i, p4Instance.P4PORT, p4Instance.p4InstanceOwner); } Console.WriteLine("Which one are talking about (1-{0}, 0 to abort): "); ConsoleKeyInfo cki; do { cki = Console.ReadKey(); } while (Char.ToLower(cki.KeyChar) < '0' || Char.ToLower(cki.KeyChar) > alResultats.Count - 1); if (cki.KeyChar == '0') { Environment.Exit(0); } return((P4ConnectStruct)alResultats[cki.KeyChar - '0']); }
//sWhereClause = "SELECT name,alias,port FROM p4_instances WHERE "; private static bool GoFetchData(MySqlConnection conn, string selectClause, ref P4ConnectStruct p4c, out string errorMsg, out ArrayList alRes) { MySqlCommand cmd = new MySqlCommand(selectClause, conn); MySqlDataReader rdr = null; errorMsg = ""; alRes = new ArrayList(); bool resultats = true; try { rdr = cmd.ExecuteReader(); while (rdr.Read()) { p4c.P4NAME = rdr[0].ToString(); p4c.P4PORT = (rdr[1].ToString()) + ":" + (rdr[2].ToString()); p4c.p4InstanceOwner = rdr[3].ToString(); alRes.Add(p4c); } } catch (MySqlException mEX) { errorMsg = mEX.Message; resultats = false; } return(resultats); }
// p4passwd:q1w2e3 // length = 15; passwd length = 6; index = 8 //private static string CutLine(string ligne) //{ // int a = ligne.IndexOf(':', 0); // return ligne.Substring(a + 1, ligne.Length - a); //} private static void ReadPasswordFile(string cfgfile, ref P4ConnectStruct p4c) { string l; var sr = new StreamReader(cfgfile); if ((l = sr.ReadLine()) != null) { p4c.P4PASSWD = l; } if (sr != null) { sr.Close(); } }
private static void WriteP4Config(string p4Exec, P4ConnectStruct p4c) { string sErreur = ""; if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) { sErreur = WriteConfig(p4c); } else { Process proc = new Process(); proc.StartInfo.FileName = String.IsNullOrWhiteSpace(p4Exec) ? "p4.exe" : p4Exec; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; // go throught all P4 variables proc.StartInfo.Arguments = "set P4PORT=" + p4c.P4PORT; proc.Start(); if (String.IsNullOrWhiteSpace(p4c.P4USER) == false) { proc.StartInfo.Arguments = "set P4USER="******"set P4PASSWD=" + p4c.P4PASSWD; proc.Start(); } if (String.IsNullOrWhiteSpace(p4c.P4CLIENT) == false) { proc.StartInfo.Arguments = "set P4CLIENT=" + p4c.P4CLIENT; proc.Start(); } } if (String.IsNullOrWhiteSpace(sErreur) == false) { Console.WriteLine("Error : {0}", sErreur); } //? Environment.GetEnvironmentVariable("HOME") //: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");t }
//public struct P4ConnectStruct //{ // public String /*p4passwdfile, */P4NAME, P4CLIENT, P4PASSWD, P4PORT, p4InstanceOwner; //}; private static string WriteConfig(P4ConnectStruct p4c) { string sErreur = ""; StreamWriter sw = new StreamWriter(Environment.GetEnvironmentVariable("HOME")); try { sw.WriteLine("P4PORT={0}", p4c.P4PORT); if (String.IsNullOrWhiteSpace(p4c.P4USER) == false) { sw.WriteLine("P4USER={0}", p4c.P4USER); } if (String.IsNullOrWhiteSpace(p4c.P4PASSWD) == false) { sw.WriteLine("P4PASSWD={0}", p4c.P4PASSWD); } if (String.IsNullOrWhiteSpace(p4c.P4CLIENT) == false) { sw.WriteLine("P4CLIENT={0}", p4c.P4CLIENT); } if (String.IsNullOrWhiteSpace(p4c.P4NAME) == false) { sw.WriteLine("P4NAME={0}", p4c.P4NAME); } } catch (IOException ioex) { sErreur = ioex.Message; } finally { if (sw != null) { sw.Close(); } } return(sErreur); }
private static void Main(string[] args) { DBConnectStruct dbConnection = new DBConnectStruct(); P4ConnectStruct p4Connection = new P4ConnectStruct(); MySqlConnection _conn; string sSelect, sP4PwdFile, sConfigFile, sErreur, p4ClientExecutable; sSelect = sP4PwdFile = sConfigFile = p4ClientExecutable = ""; ArrayList alResultats; if (ParseCommandLine(args, ref sSelect, ref sConfigFile, ref sP4PwdFile) == false) { ShowHelp(); Environment.Exit(0); } dbConnection.nPort = 3306; //default value, in case the parameter is not in the config file #region MySQL connection config if (ReadConfigFile(sConfigFile, ref p4ClientExecutable, ref dbConnection, ref p4Connection) == false) { ConsoleKeyInfo cki; Console.WriteLine("{0} cannot continue as it needs a valid MySQL connection config file", Assembly.GetExecutingAssembly().GetName().Name); Console.Write("Press (1) to manually input the pertinent info, (2) to abort ? [1/2]"); do { cki = Console.ReadKey(true); } while (cki.Key != ConsoleKey.D1 && cki.Key != ConsoleKey.D2); if (cki.Key == ConsoleKey.D1) { GetConnectionInfo(ref dbConnection); } else { Environment.Exit(0); } } #endregion MySQL connection config if (String.IsNullOrWhiteSpace(sP4PwdFile) == false && File.Exists(sP4PwdFile)) { ReadPasswordFile(sP4PwdFile, ref p4Connection); } _conn = new MySqlConnection("User Id=" + dbConnection.sUser + ";Password="******";Host=" + dbConnection.sHost + ";Database=" + dbConnection.sDB + ";Port=" + dbConnection.nPort); if ((_conn = TryDBConnect(dbConnection, out sErreur)) == null) { Console.WriteLine("Connection Error : {0}", sErreur); Environment.Exit(0); } if (GoFetchData(_conn, sSelect, ref p4Connection, out sErreur, out alResultats) == false) { Console.WriteLine("Error fetching data : {0}", sErreur); Environment.Exit(0); } if (alResultats.Count == 0) { Console.WriteLine("No instance match the given criterias. Exiting."); Environment.Exit(0); } WriteP4Config(p4ClientExecutable, SelectCorrectInstance(_conn, alResultats)); Console.WriteLine("Config file created / config applied."); _conn.Close(); }
private static bool ReadConfigFile(string cfgFile, ref string p4ClientExecutable, ref DBConnectStruct dbc, ref P4ConnectStruct p4c) { string s, ligne; if (File.Exists(cfgFile) == false) { return(false); } var sr = new StreamReader(cfgFile); dbc.nPort = 3306; while ((ligne = sr.ReadLine()) != null) { switch (ligne.ToLower()) { case "[p4user]": if ((p4c.P4USER = sr.ReadLine()) == null) { return(false); } break; case "[p4clientexec]": if ((p4ClientExecutable = sr.ReadLine()) == null) { return(false); } break; case "[serveur]": case "[server]": case "[host]": if ((dbc.sHost = sr.ReadLine()) == null) { return(false); } break; case "[db]": case "[bd]": case "[database]": if ((dbc.sDB = sr.ReadLine()) == null) { return(false); } break; case "[port]": if ((s = sr.ReadLine()) == null) { return(false); } if (Int32.TryParse(s, out dbc.nPort) == false) { dbc.nPort = 3306; } break; case "[user]": if ((dbc.sUser = sr.ReadLine()) == null) { return(false); } break; case "[passwd]": case "[password]": if ((dbc.sPasswd = sr.ReadLine()) == null) { return(false); } break; } } if (sr != null) { sr.Close(); } return(!String.IsNullOrWhiteSpace(dbc.sHost) && !String.IsNullOrWhiteSpace(dbc.sDB) && !String.IsNullOrWhiteSpace(dbc.sUser) && !String.IsNullOrWhiteSpace(dbc.sPasswd)); }