mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
Git: Add a method to generate the SSH keys
This commit is contained in:
@ -51,6 +51,9 @@ dependencies {
|
|||||||
|
|
||||||
// https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit
|
// https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit
|
||||||
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '4.4.1.201607150455-r'
|
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '4.4.1.201607150455-r'
|
||||||
|
|
||||||
|
// For reading a file to string
|
||||||
|
implementation 'commons-io:commons-io:2.5'
|
||||||
}
|
}
|
||||||
|
|
||||||
configurations {
|
configurations {
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.example.journal;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
|
import io.flutter.plugin.common.MethodChannel.Result;
|
||||||
|
|
||||||
|
public class GenerateSSHKeysTask extends AsyncTask<String, Void, Void> {
|
||||||
|
private Result result;
|
||||||
|
|
||||||
|
public GenerateSSHKeysTask(Result _result) {
|
||||||
|
result = _result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Void doInBackground(String... params) {
|
||||||
|
String keysDirPath = params[0];
|
||||||
|
File keysDir = new File(keysDirPath);
|
||||||
|
if (!keysDir.exists()) {
|
||||||
|
keysDir.mkdir();
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
result.error("FAILED", "Private key already exists", null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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("GenerateSSHKeys", ex.toString());
|
||||||
|
result.error("FAILED", ex.toString(), null);
|
||||||
|
return null;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
Log.d("GenerateSSHKeys", ex.toString());
|
||||||
|
result.error("FAILED", ex.toString(), null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String publicKey;
|
||||||
|
try {
|
||||||
|
publicKey = FileUtils.readFileToString(new File(publicKeyPath), Charset.defaultCharset());
|
||||||
|
} catch (IOException ex) {
|
||||||
|
Log.d("GenerateSSHKeys", ex.toString());
|
||||||
|
result.error("FAILED", "Failed to read the public key", null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.success(publicKey);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,7 @@ import org.eclipse.jgit.api.Git;
|
|||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
|
|
||||||
public class MainActivity extends FlutterActivity {
|
public class MainActivity extends FlutterActivity {
|
||||||
private static final String CHANNEL = "samples.flutter.io/battery";
|
private static final String CHANNEL = "gitjournal.io/git";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -56,93 +56,17 @@ public class MainActivity extends FlutterActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.notImplemented();
|
if (call.method.equals("generateSSHKeys")) {
|
||||||
|
String appFilesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||||
|
String sshKeysLocation = appFilesDir + "/ssh";
|
||||||
|
|
||||||
// Methods to add
|
new GenerateSSHKeysTask(result).execute(sshKeysLocation);
|
||||||
// git clone
|
return;
|
||||||
// git pull - merge by taking newest
|
}
|
||||||
// git add
|
|
||||||
// git commit
|
result.notImplemented();
|
||||||
// git push
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean gitClone(String url, String filePath) {
|
|
||||||
// TODO: Progress
|
|
||||||
// TODO: Credentials
|
|
||||||
// TODO: Handle errors!
|
|
||||||
|
|
||||||
File directory = new File("/git");
|
|
||||||
|
|
||||||
try {
|
|
||||||
Git git = Git.cloneRepository()
|
|
||||||
.setURI(url)
|
|
||||||
.setDirectory(directory)
|
|
||||||
.call();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (GitAPIException e) {
|
|
||||||
System.err.println("Error Cloning repository " + url + " : "+ e.getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
private void gitAdd(String gitRootUrl, String gitFileUrl) {
|
|
||||||
File directory = new File(gitRootUrl);
|
|
||||||
|
|
||||||
try {
|
|
||||||
Git git = Git.open(directory);
|
|
||||||
|
|
||||||
git.add()
|
|
||||||
.addFilepattern(gitFileUrl)
|
|
||||||
.call();
|
|
||||||
}
|
|
||||||
catch (GitAPIException e) {
|
|
||||||
System.err.println("Error Cloning repository " + url + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
catch (java.io.IOException e) {
|
|
||||||
System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void gitRemove(String gitRootUrl, String gitFileUrl) {
|
|
||||||
File directory = new File(gitRootUrl);
|
|
||||||
|
|
||||||
try {
|
|
||||||
Git git = Git.open(directory);
|
|
||||||
|
|
||||||
git.rm()
|
|
||||||
.addFilepattern(gitFileUrl)
|
|
||||||
.call();
|
|
||||||
}
|
|
||||||
catch (GitAPIException e) {
|
|
||||||
System.err.println("Error Cloning repository " + url + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
catch (java.io.IOException e) {
|
|
||||||
System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void gitCommit(String gitRootUrl, String message) {
|
|
||||||
File directory = new File(gitRootUrl);
|
|
||||||
|
|
||||||
try {
|
|
||||||
Git git = Git.open(directory);
|
|
||||||
|
|
||||||
git.commit()
|
|
||||||
.setAuthor("JournalApp", "none@example.com")
|
|
||||||
.setMessage(message)
|
|
||||||
.call();
|
|
||||||
}
|
|
||||||
catch (GitAPIException e) {
|
|
||||||
System.err.println("Error Cloning repository " + url + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
catch (java.io.IOException e) {
|
|
||||||
System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,9 @@ class GitApp extends StatelessWidget {
|
|||||||
buildGitButtons() {
|
buildGitButtons() {
|
||||||
return <Widget>[
|
return <Widget>[
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Remove Directory"),
|
child: Text("Generate Keys"),
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
print("FOO");
|
await generateSSHKeys();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
@ -42,7 +42,7 @@ buildGitButtons() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future gitClone() async {
|
Future gitClone() async {
|
||||||
const platform = const MethodChannel('samples.flutter.io/battery');
|
const platform = const MethodChannel('gitjournal.io/git');
|
||||||
|
|
||||||
print("Going to git clone");
|
print("Going to git clone");
|
||||||
await platform.invokeMethod('gitClone', {
|
await platform.invokeMethod('gitClone', {
|
||||||
@ -51,3 +51,16 @@ Future gitClone() async {
|
|||||||
});
|
});
|
||||||
print("FOO");
|
print("FOO");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future generateSSHKeys() async {
|
||||||
|
print("generateSSHKeys");
|
||||||
|
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}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user