11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4- using System . Threading . Tasks ;
54
65using Avalonia . Collections ;
7- using Avalonia . Threading ;
8-
96using CommunityToolkit . Mvvm . ComponentModel ;
107
118namespace SourceGit . ViewModels
@@ -31,41 +28,33 @@ public SSHKeyGenerator Generator
3128
3229 public SSHKeyHelper ( )
3330 {
34- _baseDir = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".ssh" ) ;
3531 Keys = new AvaloniaList < Models . SSHKeyPair > ( ) ;
3632
37- Task . Run ( ( ) =>
33+ var sshDir = new DirectoryInfo ( Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".ssh" ) ) ;
34+ if ( sshDir . Exists )
3835 {
39- var sshDir = new DirectoryInfo ( _baseDir ) ;
36+ var files = sshDir . GetFiles ( "*.pub" ) ;
4037 var keys = new List < Models . SSHKeyPair > ( ) ;
4138
42- if ( sshDir . Exists )
39+ foreach ( var file in files )
4340 {
44- var files = sshDir . GetFiles ( "*.pub" ) ;
45- foreach ( var file in files )
46- {
47- var privateKeyPath = file . FullName . Substring ( 0 , file . FullName . Length - 4 ) ;
48- if ( File . Exists ( privateKeyPath ) )
49- keys . Add ( new ( privateKeyPath ) ) ;
50- }
51-
52- keys . Sort ( ( l , r ) => l . Name . CompareTo ( r . Name ) ) ;
41+ var privateKeyPath = file . FullName . Substring ( 0 , file . FullName . Length - 4 ) ;
42+ if ( File . Exists ( privateKeyPath ) )
43+ keys . Add ( new ( privateKeyPath , file . FullName ) ) ;
5344 }
5445
5546 if ( keys . Count > 0 )
5647 {
57- Dispatcher . UIThread . Post ( ( ) =>
58- {
59- Keys . AddRange ( keys ) ;
60- SelectedKey = keys [ 0 ] ;
61- } ) ;
48+ keys . Sort ( ( l , r ) => l . Name . CompareTo ( r . Name ) ) ;
49+ Keys . AddRange ( keys ) ;
50+ SelectedKey = keys [ 0 ] ;
6251 }
63- } ) ;
52+ }
6453 }
6554
6655 public void OpenGenerator ( )
6756 {
68- Generator = new SSHKeyGenerator ( _baseDir ) ;
57+ Generator = new SSHKeyGenerator ( ) ;
6958 }
7059
7160 public void CloseGenerator ( )
@@ -75,18 +64,12 @@ public void CloseGenerator()
7564
7665 public void Generate ( )
7766 {
78- var succ = _generator . Run ( ) ;
79- if ( ! succ )
67+ var key = _generator . Run ( ) ;
68+ if ( key == null )
8069 return ;
8170
82- var keyFile = Path . Combine ( _baseDir , $ "{ _generator . Name } ") ;
83- if ( File . Exists ( keyFile ) )
84- {
85- var added = new Models . SSHKeyPair ( keyFile ) ;
86- Keys . Add ( added ) ;
87- SelectedKey = added ;
88- }
89-
71+ Keys . Add ( key ) ;
72+ SelectedKey = key ;
9073 Generator = null ;
9174 }
9275
@@ -98,10 +81,10 @@ public void DeleteSelected()
9881
9982 try
10083 {
101- if ( File . Exists ( key . FullPath ) )
102- File . Delete ( key . FullPath ) ;
103- if ( File . Exists ( $ " { key . FullPath } .pub" ) )
104- File . Delete ( $ " { key . FullPath } .pub" ) ;
84+ if ( File . Exists ( key . PrivateKeyPath ) )
85+ File . Delete ( key . PrivateKeyPath ) ;
86+ if ( File . Exists ( key . PublicKeyPath ) )
87+ File . Delete ( key . PublicKeyPath ) ;
10588
10689 var idx = Keys . IndexOf ( key ) ;
10790 if ( idx >= 0 )
@@ -122,7 +105,6 @@ public void DeleteSelected()
122105 }
123106 }
124107
125- private string _baseDir = null ;
126108 private Models . SSHKeyPair _selectedKey = null ;
127109 private SSHKeyGenerator _generator = null ;
128110 }
0 commit comments