@@ -9,24 +9,20 @@ public static class CommandExecutor
99 {
1010 public static void Execute ( string commandType , string user , string database )
1111 {
12- var proc = new Process ( ) ;
13- proc . StartInfo . FileName = "psql" ;
14- proc . StartInfo . Arguments = $@ "-U { user } -c ""{ commandType } database """"{ database } """"";
15- proc.StartInfo.RedirectStandardOutput = true;
16- proc.StartInfo.RedirectStandardError = true;
17- proc.StartInfo.UseShellExecute = false;
18- proc.StartInfo.CreateNoWindow = true;
19- proc.Start();
20- var output = proc.StandardOutput.ReadToEnd();
21- var error = proc.StandardError.ReadToEnd();
22- proc.WaitForExit();
23- if (proc.ExitCode != 0)
12+ var fileName = "psql" ;
13+ var arguments = $@ "-U { user } -c ""{ commandType } database """"{ database } """"";
14+
15+ var process = ExecuteCommand(fileName, arguments);
16+ process.Start();
17+ var output = process.StandardOutput.ReadToEnd();
18+ var error = process.StandardError.ReadToEnd();
19+ process.WaitForExit();
20+ if (process.ExitCode != 0)
2421 {
25- proc . Close ( ) ;
22+ process . Close ( ) ;
2623 throw new Exception ( "Error restoring database: " + error ) ;
2724 }
28-
29- proc. Close ( ) ;
25+ process. Close ( ) ;
3026 }
3127
3228 public static void ExecuteRestore( UserConnectionVo connection )
@@ -43,33 +39,44 @@ public static void ExecuteRestore(UserConnectionVo connection)
4339 break ;
4440 }
4541
46- var proc = new Process ( ) ;
42+
43+ string fileName;
44+ string arguments ;
45+
4746 if ( connection . IsForPgDump )
4847 {
49- proc . StartInfo . FileName = "psql" ;
50- proc . StartInfo . Arguments = $@ "-U { connection . UserName } ""{ connection . DatabaseName } "" < ""{ connection . RestoreFileLocation } """;
48+ fileName = "psql" ;
49+ arguments = $@ "-U { connection . UserName } ""{ connection . DatabaseName } "" < ""{ connection . RestoreFileLocation } """;
5150 }
5251 else
5352 {
54- proc . StartInfo . FileName = "pg_dump" ;
55- proc . StartInfo . Arguments = $@ "-U { connection . UserName } -d ""{ connection . DatabaseName } "" ""{ connection . RestoreFileLocation } """;
53+ fileName = "pg_dump" ;
54+ arguments = $@ "-U { connection . UserName } -d ""{ connection . DatabaseName } "" ""{ connection . RestoreFileLocation } """;
5655 }
5756
58- proc.StartInfo.RedirectStandardOutput = true;
59- proc.StartInfo.RedirectStandardError = true;
60- proc.StartInfo.UseShellExecute = false;
61- proc.StartInfo.CreateNoWindow = true;
62- proc.Start();
63- var output = proc.StandardOutput.ReadToEnd();
64- var error = proc.StandardError.ReadToEnd();
65- proc.WaitForExit();
66- if (proc.ExitCode != 0)
57+ var process = ExecuteCommand(fileName, arguments);
58+ process.Start();
59+ var output = process.StandardOutput.ReadToEnd();
60+ var error = process.StandardError.ReadToEnd();
61+ process.WaitForExit();
62+ if (process.ExitCode != 0)
6763 {
68- proc . Close ( ) ;
64+ process . Close ( ) ;
6965 throw new Exception ( "Error restoring database.Details: " + error ) ;
7066 }
67+ process. Close ( ) ;
68+ }
7169
72- proc. Close ( ) ;
70+ private static Process ExecuteCommand( string fileName , string arguments )
71+ {
72+ var proc = new Process( ) ;
73+ proc. StartInfo . FileName = fileName ;
74+ proc. StartInfo . Arguments = arguments ;
75+ proc. StartInfo . RedirectStandardOutput = true ;
76+ proc. StartInfo . RedirectStandardError = true ;
77+ proc. StartInfo . UseShellExecute = false ;
78+ proc. StartInfo . CreateNoWindow = true ;
79+ return proc;
7380 }
7481 }
7582}
0 commit comments