forked from cvengler/sysget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (142 loc) · 4.27 KB
/
main.cpp
File metadata and controls
171 lines (142 loc) · 4.27 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <vector>
#include "utils.hpp"
#include "search.hpp"
#include "install.hpp"
#include "remove.hpp"
#include "update.hpp"
#include "clean.hpp"
#define CONFIG_PATH "/usr/local/share/sysget/config.txt"
using namespace std;
int main(int argc, char *argv[]) {
//First check if the config file exists
string pm;
if(!file_exists(CONFIG_PATH)) {
cout << "Please choose a package manager: " << endl << endl;
cout << "1. apt-get (Debian)" << endl;
cout << "2. xbps (Void)" << endl;
cout << "3. dnf (Fedora)" << endl;
cout << "4. yum (Enterprise Linux/Legacy Fedora)" << endl;
cout << "5. zypper (OpenSUSE)" << endl;
cout << "6. eopkg (Solus)" << endl;
cout << "7. pacman (Arch)" << endl;
cout << "8. emerge (Gentoo)" << endl;
cout << "9. pkg (FreeBSD)" << endl;
cout << "10. chromebrew (ChromeOS)" << endl;
cout << "11. homebrew (Mac OS)" << endl;
cout << "12. nix (Nix OS)" << endl;
cout << "13. snap (Independent)" << endl;
cout << "14. npm (Javascript, Global)" << endl << endl;
string input;
cin >> input;
//Create config files
vector<string> package_manager_list = { "apt-get", "xbps", "dnf", "yum", "zypper", "eopkg", "pacman", "emerge", "pkg", "chromebrew", "homebrew", "nix", "snap", "npm" }; //Thanks dwbrite
//Convert the input into an int
int input_int = stoi(input);
//Check if input is valid
if(input_int > package_manager_list.size() || input_int <= 0) {
cout << "Invalid input" << endl;
exit(1);
}
//We need to reduce the input with 1 because arrays start at 0
create_conf(CONFIG_PATH, package_manager_list[input_int - 1] + "\n");
exit(0);
}
pm = read_conf(CONFIG_PATH);
//If read_conf detects something invalid, the return will be ERROR
if(pm == "ERROR") {
cout << "Broken config" << endl;
if(remove(CONFIG_PATH) != 0) {
cout << "Error while deleting broken config file, are you root ?" << endl;
exit(1);
}
else {
exit(1);
}
}
//If user enters no operation
if(argc < 2) {
cout << "Error, you need an operation" << endl;
exit(1);
}
if(string(argv[1]) == "search") {
if(argc < 3) {
//If users enters no search parameter
cout << "Error, no search parameter" << endl;
exit(1);
}
search(pm, string(argv[2]));
}
else if(string(argv[1]) == "install") {
if(argc < 3) {
//If user enters no package to install
cout << "Error, no package to install" << endl;
exit(1);
}
install(pm, string(argv[2]));
}
else if(string(argv[1]) == "remove") {
if(argc < 3) {
//If user enters no package to remove
cout << "Error, no package to remove" << endl;
exit(1);
}
remove(pm, string(argv[2]));
}
else if(string(argv[1]) == "autoremove") {
autoremove(pm);
}
//Update will only refresh the database
else if(string(argv[1]) == "update") {
update(pm);
}
//Upgrading will not update the database
else if(string(argv[1]) == "upgrade") {
if(argc < 3) {
upgrade(pm);
}
else {
upgrade_pkg(pm, string(argv[2]));
}
}
//Clean will clean the download cache
else if(string(argv[1]) == "clean") {
clean(pm);
}
//Set will change the package manager
else if(string(argv[1]) == "set") {
if(argc < 3) {
cout << "Error, no package manager provided" << endl;
exit(1);
}
if(remove(CONFIG_PATH) != 0) {
cout << "Error while deleting the config file, are you root ?" << endl;
exit(1);
}
else {
create_conf(CONFIG_PATH, string(argv[2]) + "\n");
cout << "Package manager changed to " << string(argv[2]) << endl;
}
}
//Help
else if(string(argv[1]) == "help") {
cout << "Help of sysget" << endl;
cout << "sysget [OPTION] [ARGUMENT]" << endl;
cout << endl;
cout << "search [query]\t\tsearch for a package in the resporitories" << endl;
cout << "install [package]\tinstall a package from the repos" << endl;
cout << "remove [package]\tremoves a package" << endl;
cout << "autoremove\t\tremoves not needed packages (orphans)" << endl;
cout << "update\t\t\tupdate the database" << endl;
cout << "upgrade\t\t\tdo a system upgrade" << endl;
cout << "upgrade [package]\tupgrade a specific package" << endl;
cout << "clean\t\t\tclean the download cache" << endl;
cout << "set [NEW MANAGER]\tset a new package manager" << endl;
cout << endl;
}
else {
cout << "Unknown operation, try sysget help" << endl;
exit(1);
}
return 0;
}