-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (96 loc) · 3.19 KB
/
Copy pathmain.cpp
File metadata and controls
112 lines (96 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* @file main.cpp
* @brief Main entry point for the VCS application.
*
* Initialize the VCS environment and handle user commands.
*
* @author Joseph Lefkovitz (https://github.com/lefkovitzj)
*/
#include <string>
#include <vector>
#include <filesystem>
#include <format>
/* Utils Imports */
#include "utils/io.h"
/* Core Imports */
#include "core/configuration.h"
#include "core/index.h"
/* Command Imports */
#include "commands/add.h"
#include "commands/config.h"
#include "commands/init.h"
#include "commands/status.h"
const float VCS_VERSION_NUM = 0.1;
const std::string VCS_SOURCE_URL = "https://github.com/lefkovitzj/vcs";
// Store the path at which the VCS data is found.
const std::filesystem::path vcs_dir = std::filesystem::current_path() / ".vcs";
bool vcs_inited = std::filesystem::is_directory(vcs_dir);
std::string user_name = "";
std::string user_email = "";
std::vector<std::string> index_file_paths;
std::vector<std::string> index_file_blobs;
void help_menu() {
info_out("Help menu");
}
void version_menu() {
info_out(std::format("Version {}", VCS_VERSION_NUM));
info_out(std::format("Find the most up-to-date version of VCS at {}", VCS_SOURCE_URL));
}
int main(int argc, char **argv) {
std::vector<std::string> args;
for (int i = 1; i < argc; i++) {
args.push_back(argv[i]);
}
if (args.empty()) {
err_out("No argument given\n");
return 0;
}
// Load config, if applicable.
vcs_config read_config = load_config(vcs_dir);
user_name = read_config.user_name;
user_email = read_config.user_email;
std::string base_cmd = args.at(0);
if (base_cmd == "-h" || base_cmd == "help") {
help_menu();
}
else if (base_cmd == "-v" || base_cmd == "version") {
version_menu();
}
else if (base_cmd == "--config") {
handle_config(vcs_dir, args, user_name, user_email);
}
else if (base_cmd == "init") {
init_vcs(vcs_inited, vcs_dir, user_name, user_email);
}
else if (base_cmd == "status") {
if (! vcs_inited) {
err_out("Cannot check status - VCS not initialized yet");
}
vcs_status(vcs_dir);
}
else if (base_cmd == "add") {
if (! vcs_inited) {
err_out("Cannot add files - VCS not initialized yet");
}
if (args.size() > 1) {
for (size_t i = 1; i < args.size(); i++) {
// If they pass '.', we use current_path(), otherwise the specific path
std::filesystem::path p = (args[i] == ".")
? std::filesystem::current_path()
: std::filesystem::absolute(std::filesystem::path(args[i]));
add_file(vcs_dir, p, vcs_inited, index_file_paths, index_file_blobs);
}
make_index(vcs_dir / "index", user_name, user_email, index_file_paths, index_file_blobs);
}
else {
err_out("'add' requires one or more arguments");
}
}
else {
err_out(std::format("No such argument {}", base_cmd));
}
// Store final state of configuration variables.
vcs_config final_config = {user_name, user_email};
store_config(vcs_dir, final_config);
// Exit successfully.
return 0;
}