@@ -9,24 +9,20 @@ public static class CommandExecutor
9
9
{
10
10
public static void Execute ( string commandType , string user , string database )
11
11
{
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)
24
21
{
25
- proc . Close ( ) ;
22
+ process . Close ( ) ;
26
23
throw new Exception ( "Error restoring database: " + error ) ;
27
24
}
28
-
29
- proc. Close ( ) ;
25
+ process. Close ( ) ;
30
26
}
31
27
32
28
public static void ExecuteRestore( UserConnectionVo connection )
@@ -43,33 +39,44 @@ public static void ExecuteRestore(UserConnectionVo connection)
43
39
break ;
44
40
}
45
41
46
- var proc = new Process ( ) ;
42
+
43
+ string fileName;
44
+ string arguments ;
45
+
47
46
if ( connection . IsForPgDump )
48
47
{
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 } """;
51
50
}
52
51
else
53
52
{
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 } """;
56
55
}
57
56
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)
67
63
{
68
- proc . Close ( ) ;
64
+ process . Close ( ) ;
69
65
throw new Exception ( "Error restoring database.Details: " + error ) ;
70
66
}
67
+ process. Close ( ) ;
68
+ }
71
69
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;
73
80
}
74
81
}
75
82
}
0 commit comments