@@ -10,7 +10,6 @@ public class CoreAdminHandler
10
10
public static CoreAdminHandler Instance => _instance . Value ;
11
11
private Config _config ;
12
12
private Action < bool , string > ? _updateFunc ;
13
- private const string _tag = "CoreAdminHandler" ;
14
13
private int _linuxSudoPid = - 1 ;
15
14
16
15
public async Task Init ( Config config , Action < bool , string > updateFunc )
@@ -30,69 +29,60 @@ private void UpdateFunc(bool notify, string msg)
30
29
31
30
public async Task < Process ? > RunProcessAsLinuxSudo ( string fileName , CoreInfo coreInfo , string configPath )
32
31
{
33
- try
34
- {
35
- var cmdLine = $ "{ fileName . AppendQuotes ( ) } { string . Format ( coreInfo . Arguments , Utils . GetBinConfigPath ( configPath ) . AppendQuotes ( ) ) } ";
36
- var shFilePath = await CreateLinuxShellFile ( cmdLine , "run_as_sudo.sh" ) ;
32
+ var cmdLine = $ "{ fileName . AppendQuotes ( ) } { string . Format ( coreInfo . Arguments , Utils . GetBinConfigPath ( configPath ) . AppendQuotes ( ) ) } ";
33
+ var shFilePath = await CreateLinuxShellFile ( cmdLine , "run_as_sudo.sh" ) ;
37
34
38
- Process proc = new ( )
39
- {
40
- StartInfo = new ( )
41
- {
42
- FileName = shFilePath ,
43
- Arguments = "" ,
44
- WorkingDirectory = Utils . GetBinConfigPath ( ) ,
45
- UseShellExecute = false ,
46
- RedirectStandardInput = true ,
47
- RedirectStandardOutput = true ,
48
- RedirectStandardError = true ,
49
- CreateNoWindow = true ,
50
- StandardInputEncoding = Encoding . UTF8 ,
51
- StandardOutputEncoding = Encoding . UTF8 ,
52
- StandardErrorEncoding = Encoding . UTF8 ,
53
- }
54
- } ;
55
-
56
- proc . OutputDataReceived += ( sender , e ) =>
35
+ Process proc = new ( )
36
+ {
37
+ StartInfo = new ( )
57
38
{
58
- if ( e . Data . IsNotEmpty ( ) )
59
- {
60
- UpdateFunc ( false , e . Data + Environment . NewLine ) ;
61
- }
62
- } ;
63
- proc . ErrorDataReceived += ( sender , e ) =>
39
+ FileName = shFilePath ,
40
+ Arguments = "" ,
41
+ WorkingDirectory = Utils . GetBinConfigPath ( ) ,
42
+ UseShellExecute = false ,
43
+ RedirectStandardInput = true ,
44
+ RedirectStandardOutput = true ,
45
+ RedirectStandardError = true ,
46
+ CreateNoWindow = true ,
47
+ StandardInputEncoding = Encoding . UTF8 ,
48
+ StandardOutputEncoding = Encoding . UTF8 ,
49
+ StandardErrorEncoding = Encoding . UTF8 ,
50
+ }
51
+ } ;
52
+
53
+ proc . OutputDataReceived += ( sender , e ) =>
54
+ {
55
+ if ( e . Data . IsNotEmpty ( ) )
64
56
{
65
- if ( e . Data . IsNotEmpty ( ) )
66
- {
67
- UpdateFunc ( false , e . Data + Environment . NewLine ) ;
68
- }
69
- } ;
70
-
71
- proc . Start ( ) ;
72
- proc . BeginOutputReadLine ( ) ;
73
- proc . BeginErrorReadLine ( ) ;
74
-
75
- await Task . Delay ( 10 ) ;
76
- await proc . StandardInput . WriteLineAsync ( ) ;
77
- await Task . Delay ( 10 ) ;
78
- await proc . StandardInput . WriteLineAsync ( AppHandler . Instance . LinuxSudoPwd ) ;
79
-
80
- await Task . Delay ( 100 ) ;
81
- if ( proc is null or { HasExited : true } )
57
+ UpdateFunc ( false , e . Data + Environment . NewLine ) ;
58
+ }
59
+ } ;
60
+ proc . ErrorDataReceived += ( sender , e ) =>
61
+ {
62
+ if ( e . Data . IsNotEmpty ( ) )
82
63
{
83
- throw new Exception ( ResUI . FailedToRunCore ) ;
64
+ UpdateFunc ( false , e . Data + Environment . NewLine ) ;
84
65
}
66
+ } ;
85
67
86
- _linuxSudoPid = proc . Id ;
68
+ proc . Start ( ) ;
69
+ proc . BeginOutputReadLine ( ) ;
70
+ proc . BeginErrorReadLine ( ) ;
87
71
88
- return proc ;
89
- }
90
- catch ( Exception ex )
72
+ await Task . Delay ( 10 ) ;
73
+ await proc . StandardInput . WriteLineAsync ( ) ;
74
+ await Task . Delay ( 10 ) ;
75
+ await proc . StandardInput . WriteLineAsync ( AppHandler . Instance . LinuxSudoPwd ) ;
76
+
77
+ await Task . Delay ( 100 ) ;
78
+ if ( proc is null or { HasExited : true } )
91
79
{
92
- Logging . SaveLog ( _tag , ex ) ;
93
- UpdateFunc ( false , ex . Message ) ;
94
- return null ;
80
+ throw new Exception ( ResUI . FailedToRunCore ) ;
95
81
}
82
+
83
+ _linuxSudoPid = proc . Id ;
84
+
85
+ return proc ;
96
86
}
97
87
98
88
public async Task KillProcessAsLinuxSudo ( )
@@ -102,22 +92,14 @@ public async Task KillProcessAsLinuxSudo()
102
92
return ;
103
93
}
104
94
105
- try
106
- {
107
- var cmdLine = $ "pkill -P { _linuxSudoPid } ; kill { _linuxSudoPid } ";
108
- var shFilePath = await CreateLinuxShellFile ( cmdLine , "kill_as_sudo.sh" ) ;
95
+ var cmdLine = $ "pkill -P { _linuxSudoPid } ; kill { _linuxSudoPid } ";
96
+ var shFilePath = await CreateLinuxShellFile ( cmdLine , "kill_as_sudo.sh" ) ;
109
97
110
- var result = await Cli . Wrap ( shFilePath )
111
- . WithStandardInputPipe ( PipeSource . FromString ( AppHandler . Instance . LinuxSudoPwd ) )
112
- . ExecuteAsync ( ) ;
98
+ await Cli . Wrap ( shFilePath )
99
+ . WithStandardInputPipe ( PipeSource . FromString ( AppHandler . Instance . LinuxSudoPwd ) )
100
+ . ExecuteAsync ( ) ;
113
101
114
- _linuxSudoPid = - 1 ;
115
- }
116
- catch ( Exception ex )
117
- {
118
- Logging . SaveLog ( _tag , ex ) ;
119
- UpdateFunc ( false , ex . Message ) ;
120
- }
102
+ _linuxSudoPid = - 1 ;
121
103
}
122
104
123
105
private async Task < string > CreateLinuxShellFile ( string cmdLine , string fileName )
0 commit comments