Git Clone: Cleanup code

This commit is contained in:
Vishesh Handa
2019-01-07 13:34:21 +01:00
parent ae6158a0a8
commit 0ba4bf68d1
3 changed files with 34 additions and 55 deletions

View File

@ -33,33 +33,13 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... params) {
String url = params[0];
String filesDir = params[1];
File directory = new File(filesDir + "/git");
String cloneDirPath = params[1];
final String privateKeyPath = params[2];
Log.d("GitClone Directory", filesDir);
File keysDir = new File(filesDir + "/keys");
if (!keysDir.exists()) {
keysDir.mkdir();
}
final String privateKeyPath = filesDir + "/keys/id_rsa";
final String publicKeyPath = filesDir + "/keys/id_rsa.pub";
File cloneDir = new File(cloneDirPath);
Log.d("GitClone Directory", cloneDirPath);
try {
// Generate key pair
try {
JSch jsch = new JSch();
KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 1024 * 4);
kpair.writePrivateKey(privateKeyPath);
kpair.writePublicKey(publicKeyPath, "Auto generated Key");
kpair.dispose();
} catch (JSchException ex) {
Log.d("GitClone", ex.toString());
} catch (IOException ex) {
Log.d("GitClone", ex.toString());
}
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
protected void configure(Host host, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
@ -94,6 +74,8 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.addIdentity(privateKeyPath);
JSch.setConfig("PreferredAuthentications", "publickey");
Log.d("identityNames", defaultJSch.getIdentityNames().toString());
return defaultJSch;
}
@ -101,7 +83,7 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
CloneCommand cloneCommand = Git.cloneRepository()
.setURI(url)
.setDirectory(directory);
.setDirectory(cloneDir);
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
@ -113,18 +95,20 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
cloneCommand.call();
} catch (TransportException e) {
// FIXME: Return a better error message?
System.err.println("Transport Error Cloning repository " + url + " : " + e.getMessage());
return null;
} catch (GitAPIException e) {
System.err.println("Error Cloning repository " + url + " : " + e.getMessage());
Log.d("gitClone", e.toString());
result.error("FAILED", e.toString(), null);
return null;
} catch (GitAPIException e) {
Log.d("gitClone", e.toString());
result.error("FAILED", e.toString(), null);
return null;
} catch (Exception e) {
Log.d("gitClone", e.toString());
result.error("FAILED", e.toString(), null);
return null;
}
result.success(null);
return null;
}
protected void onPostExecute(Void taskResult) {
result.success(null);
}
}

View File

@ -34,25 +34,18 @@ public class MainActivity extends FlutterActivity {
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("gitClone")) {
String cloneUrl = call.argument("cloneUrl");
String filePath = call.argument("filePath");
String folderName = call.argument("folderName");
if (cloneUrl.isEmpty() || filePath.isEmpty()) {
if (cloneUrl.isEmpty() || folderName.isEmpty()) {
result.error("Invalid Parameters", "Arguments Invalid", null);
return;
}
String filesDir = PathUtils.getFilesDir(getApplicationContext());
Log.d("vhanda", filesDir);
String cloneLocation = filesDir + "/git";
String cloneLocation = filesDir + "/" + folderName;
new GitCloneTask(result).execute(cloneUrl, cloneLocation);
/*
if (gitClone(cloneUrl, filePath)) {
result.success(null);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
*/
final String privateKeyPath = filesDir + "/ssh/id_rsa";
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
return;
}

View File

@ -45,22 +45,24 @@ Future gitClone() async {
const platform = const MethodChannel('gitjournal.io/git');
print("Going to git clone");
await platform.invokeMethod('gitClone', {
'cloneUrl': "root@bcn.vhanda.in:git/notes",
'filePath': "/",
});
print("FOO");
try {
await platform.invokeMethod('gitClone', {
'cloneUrl': "root@bcn.vhanda.in:git/notes",
'folderName': "journal",
});
print("Done");
} on PlatformException catch (e) {
print("gitClone Failed: '${e.message}'.");
}
}
Future generateSSHKeys() async {
print("generateSSHKeys");
print("generateSSHKeyss");
try {
const platform = const MethodChannel('gitjournal.io/git');
String publicKey = await platform.invokeMethod('generateSSHKeys', {});
print("Public Key " + publicKey);
} on PlatformException catch (e) {
print("Failed to generateSSHKeys: '${e.message}'.");
} catch (e) {
print("EX: '${e.message}'.");
}
}