Fix sshkey custom comment

Custom comments had been implemented but were not working.
This commit is contained in:
Vishesh Handa
2019-01-16 23:37:19 +01:00
parent 4292555286
commit f74229dbc7
4 changed files with 16 additions and 11 deletions

View File

@ -14,6 +14,7 @@ import org.apache.commons.io.FileUtils;
import io.flutter.plugin.common.MethodChannel.Result;
public class GenerateSSHKeysTask extends AsyncTask<String, Void, Void> {
private final static String TAG = "GenerateSSHKeys";
private Result result;
public GenerateSSHKeysTask(Result _result) {
@ -28,13 +29,14 @@ public class GenerateSSHKeysTask extends AsyncTask<String, Void, Void> {
}
String comment = params[1];
Log.d(TAG, "Comment: " + comment);
final String privateKeyPath = keysDir + "/id_rsa";
final String publicKeyPath = keysDir + "/id_rsa.pub";
File privateKeyFile = new File(privateKeyPath);
if (privateKeyFile.exists()) {
Log.d("GenerateSSHKeys", "Private key already exists");
Log.d(TAG, "Private key already exists");
result.error("FAILED", "Private key already exists", null);
return null;
}
@ -48,11 +50,11 @@ public class GenerateSSHKeysTask extends AsyncTask<String, Void, Void> {
kpair.writePublicKey(publicKeyPath, comment);
kpair.dispose();
} catch (JSchException ex) {
Log.d("GenerateSSHKeys", ex.toString());
Log.d(TAG, ex.toString());
result.error("FAILED", ex.getMessage(), null);
return null;
} catch (IOException ex) {
Log.d("GenerateSSHKeys", ex.toString());
Log.d(TAG, ex.toString());
result.error("FAILED", ex.getMessage(), null);
return null;
}
@ -61,7 +63,7 @@ public class GenerateSSHKeysTask extends AsyncTask<String, Void, Void> {
try {
publicKey = FileUtils.readFileToString(new File(publicKeyPath), Charset.defaultCharset());
} catch (IOException ex) {
Log.d("GenerateSSHKeys", ex.toString());
Log.d(TAG, ex.toString());
result.error("FAILED", "Failed to read the public key", null);
return null;
}

View File

@ -124,6 +124,7 @@ public class MainActivity extends FlutterActivity {
} else if (call.method.equals("generateSSHKeys")) {
String comment = call.argument("comment");
if (comment == null || comment.isEmpty()) {
Log.d("generateSSHKeys", "Defaulting to default comment");
comment = "Generated on Android";
}
@ -136,7 +137,7 @@ public class MainActivity extends FlutterActivity {
try {
publicKey = FileUtils.readFileToString(new File(publicKeyPath), Charset.defaultCharset());
} catch (IOException ex) {
Log.d("GenerateSSHKeys", ex.toString());
Log.d("getSSHPublicKey", ex.toString());
result.error("FAILED", "Failed to read the public key", null);
}

View File

@ -21,9 +21,10 @@ class GitApp extends StatelessWidget {
buildGitButtons() {
return <Widget>[
RaisedButton(
child: Text("Generate Keys"),
onPressed: generateSSHKeys,
),
child: Text("Generate Keys"),
onPressed: () {
generateSSHKeys(comment: "Git Sample App");
}),
RaisedButton(
child: Text("Git Clone"),
onPressed: () async {

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
const _platform = const MethodChannel('gitjournal.io/git');
@ -29,11 +30,11 @@ Future<String> gitClone(String cloneUrl, String folderName) async {
return null;
}
Future<String> generateSSHKeys({comment: ""}) async {
print("generateSSHKeyss");
Future<String> generateSSHKeys({@required String comment}) async {
print("generateSSHKeyss: " + comment);
try {
String publicKey = await _platform.invokeMethod('generateSSHKeys', {
comment: comment,
'comment': comment,
});
print("Public Key " + publicKey);
return publicKey;