Skip to content

Commit

Permalink
Prewritten code for the tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdifatahCodes committed Nov 15, 2023
1 parent f68ed6c commit 155bef0
Show file tree
Hide file tree
Showing 20 changed files with 1,350 additions and 0 deletions.
Binary file added build/ObsidianGitSync
Binary file not shown.
20 changes: 20 additions & 0 deletions include/FsManipulator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef FSMANIPULATOR_HPP
#define FSMANIPULATOR_HPP

#include <string>

class FsManipulator {
public:
int is_dir_present(const std::string dir_path);
int create_dir(const std::string dir_path);
int delete_dir(const std::string dir_path);
int is_file_present(const std::string file_path);
int create_file(const std::string file_path);
int delete_file(const std::string file_path);
std::string read_config_var(const std::string file_path, const std::string config_var_name);
std::string add_config_var(const std::string file_path, const std::string config_var_name, const std::string input_value);
std::string update_config_var(const std::string file_path, const std::string config_var_name, const std::string input_value);
std::string delete_config_var(const std::string file_path, const std::string config_var_name);
};

#endif // FSMANIPULATOR_HPP
38 changes: 38 additions & 0 deletions include/GitOperations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef GITOPERATIONS_HPP
#define GITOPERATIONS_HPP

#include <git2.h>
#include <string>

class GitOperations {
public:
// For the is_repo_present method, use the var workspace_dir to look for a folder with the same name as var main_repository.
// If the folder is available, verify that it is a valid git repo using the var imported from the config.
// Try connecting to it using the provided credentials and links from the config.
// If all passes, return 0, if not, return a non-zero and log the error using Logging class methods.
int is_repo_present(const std::string config_path);
// Clone the repo using git2 and return a 0 for success and a non zero for error while logging everything.
int clone_repo(const std::string config_path);
git_repository* open_repo(const std::string config_path);
// are_changes_present checks for changes that are not commited and returns a 0 for true and non-zero for errors. Log everything.
int are_unindexed_changes_present(git_repository *repo);
// add_to_index would detect any new changes in the repo and add them to index to prepare them for a commit.
int add_to_index(git_repository *repo);
int are_uncommitted_changes_present(git_repository *repo);
int are_unpushed_commits_present(git_repository *repo);
// create_commit commits everything in the index and uses the vars git_username and commits_email from the config file. Return 0 or non-zero as above and log everthing.
int create_commit(const std::string config_path, git_repository *repo);
// push_to_remote pushes the commits to remote. Return 0 or non-zero like above and log everything.
int push_to_remote(const std::string config_path, git_repository *repo);
// Fetches latest changes from the remote repository.
int fetch_remote(const std::string config_path, git_repository *repo);
// Method to handle non-fast-forward error
int handle_non_fast_forward_error(const std::string config_path);
// Backup everything in repo_dir except .git dir to backup_dir
int backup_user_notes(const std::string repo_dir, const std::string backup_dir);
// Replace the contents of repo_dir except .git dir with the contents of backup_dir and delete backup_dir if success.
int recover_user_notes(const std::string repo_dir, const std::string backup_dir);

};

#endif // GITOPERATIONS_HPP
12 changes: 12 additions & 0 deletions include/Init.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef INIT_HPP
#define INIT_HPP

#include <string>

class Init {
public:
// root_dir in linux is "~/.config/ObsidianGitSync".
std::string init_for_linux_desktop(const std::string root_dir, const std::string workspace_dir);
};

#endif // INIT_HPP
13 changes: 13 additions & 0 deletions include/LoadEnv.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef LOADENV_HPP
#define LOADENV_HPP

#include <string>
#include <nlohmann/json.hpp>

class LoadEnv {
public:
std::string load_env_variables(const std::string file_path, const nlohmann::json json_data, const bool update_var);
std::string get_env_var(const std::string file_path, const std::string var_name, const bool update_var);
};

#endif // LOADENV_HPP
11 changes: 11 additions & 0 deletions include/Logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef LOGGING_HPP
#define LOGGING_HPP

#include <string>

class Logging {
public:
int log_to_file(const std::string new_data);
};

#endif // LOGGING_HPP
9 changes: 9 additions & 0 deletions include/NetworkChecker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef NETWORKCHECKER_HPP
#define NETWORKCHECKER_HPP

class NetworkChecker {
public:
int checkConnectivity();
};

#endif // NETWORKCHECKER_HPP
12 changes: 12 additions & 0 deletions include/Prompting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PROMPTING_HPP
#define PROMPTING_HPP

#include <string>

class Prompting {
public:
std::string prompt_user_for_var(const std::string file_path, const std::string var_name, const std::string default_value = "");
std::string is_prompt_required(const std::string file_path, const std::string var_name, const std::string default_value = "", const bool update_var = false);
};

#endif // PROMPTING_HPP
17 changes: 17 additions & 0 deletions include/SshWorker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SSHWORKER_HPP
#define SSHWORKER_HPP

#include <string>

class SshWorker {
public:
// This class will use libssh library and retrieve vars like SSH_KEY_NAME, ROOT_DIR, SSH_DIR, COMMITS_EMAIL from config_path using the FsManipulator method read_config_var.
// Use the Logging method log_to_file that takes in a string to log everything, from successes to errors with errors being prepended with a string 'Error: '.
// Then use method from FsManipulator to check if the 2 ssh keys exist using the file path ssh_dir/ssh_key_name and ssh_dir/ssh_key_name.pub, if they exist, try to verify them that they are actually SSH_KEYTYPE_ED25519 and not just random file with the right names that we looked for, if they are verified ssh keys, return 0, else return a non-zero number and use logging class to log all the returns or log worthy events.
//
int is_ssh_setup(const std::string config_path);
// Create a new SSH_KEYTYPE_ED25519 keypair using the keynames ssh_key_name and add .pub to the public key, then create a private function that will be used by the inhabitants of ssh_worker.cpp only to change the email at the end of the pub key file to the var commits_email. Log everything including errors in the way specified above.
std::string setup_new_ssh_keypair(const std::string config_path);
};

#endif // SSHWORKER_HPP
71 changes: 71 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

# Function to install dependencies on Debian-based systems
install_on_debian() {
echo "Detected a Debian-based Linux distribution."
sudo apt-get update
sudo apt-get install -y libgit2-dev libssh2-1-dev libconfig++-dev libssh-dev libcurl4-openssl-dev
}

# Function to install dependencies on Red Hat-based systems
install_on_redhat() {
echo "Detected a Red Hat-based Linux distribution."
sudo yum install -y epel-release
sudo yum install -y libgit2-devel libssh2-devel libconfig-devel libssh-devel libcurl-devel
}

# Function to install dependencies on Fedora
install_on_fedora() {
echo "Detected Fedora."
sudo dnf install -y libgit2-devel libssh2-devel libconfig-devel libssh-devel libcurl-devel
}

# Function to install dependencies on Arch Linux
install_on_arch() {
echo "Detected Arch Linux."
sudo pacman -Syu --noconfirm
sudo pacman -S --noconfirm libgit2 libssh2 libconfig libssh curl
}

# Detect the distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
case $ID in
ubuntu | debian)
install_on_debian
;;
rhel | centos)
install_on_redhat
;;
fedora)
install_on_fedora
;;
arch | manjaro)
install_on_arch
;;
*)
echo "Your distribution ($ID) is not recognized. Please manually install the dependencies."
exit 1
;;
esac
else
echo "Cannot identify the Linux distribution. Please manually install the dependencies."
exit 1
fi

# Function to download and run the binary package
download_and_run() {
echo "Downloading the binary package from GitHub releases..."
# Replace with the direct link to your binary in GitHub releases
binary_url="https://github.com/username/repository/releases/download/v1.0/your-binary"
curl -L -o your-binary "$binary_url"

echo "Making the binary executable..."
chmod +x your-binary

echo "Running the binary..."
./your-binary
}

# After installing dependencies, download and run the binary
download_and_run
14 changes: 14 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Init.hpp"
#include <iostream>
#include <string>
#include <cstdlib> // For getenv

int main() {
Init init;
std::string home_dir = getenv("HOME");
std::string root_dir = home_dir + "/.config/ObsidianGitSync";
std::string workspace_dir = home_dir + "/Documents/Documents/Obsidian";

init.init_for_linux_desktop(root_dir, workspace_dir);
return 0;
}
18 changes: 18 additions & 0 deletions platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>

int main() {
#ifdef _WIN32
std::cout << "Running on Windows" << std::endl;
#elif __APPLE__
std::cout << "Running on Apple" << std::endl;
#elif __linux__
#ifdef __ANDROID__
std::cout << "Running on Android" << std::endl;
#else
std::cout << "Running on Real Linux" << std::endl;
#endif
#else
std::cout << "Unknown platform" << std::endl;
#endif
return 0;
}
Loading

0 comments on commit 155bef0

Please sign in to comment.