Skip to content

Commit 6b191d1

Browse files
committed
so much idk
1 parent 99d1b60 commit 6b191d1

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

arkr.cpp

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
#include "json.hpp"
1010
#include <unistd.h>
1111
#include <ctime>
12+
#include <algorithm>
1213
using json = nlohmann::json_abi_v3_11_3::json;
1314
using namespace std;
1415
const string version = "rolling";
1516
string get_mirlink() {
1617
array<char, 128> buffer;
1718
string result;
1819
unique_ptr<FILE, decltype(&pclose)> pipe(popen("cat /etc/arkr/mirlink | tr -d '\n'", "r"), pclose); /* http://example.com/packages/arch/ */
19-
if (!pipe) {
20-
throw runtime_error("popen() failed!");
21-
}
2220
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
2321
result += buffer.data();
2422
}
@@ -106,7 +104,12 @@ int main(int argc, char *argv[]) {
106104
string catcommand;
107105
for (int i = 2; i < argc; ++i) {
108106
if (action == 1) {
109-
packlist.push_back(argv[i]);
107+
string packtoadd = argv[i];
108+
if (find(packlist.begin(), packlist.end(), packtoadd) == packlist.end()) {
109+
packlist.push_back(packtoadd);
110+
} else {
111+
cout << "Package " << packtoadd << " is already installed. Reinstalling." << endl;
112+
}
110113
string whatpack = argv[i];
111114
if (whatpack.find('/') != string::npos) {
112115
stringstream ss(whatpack);
@@ -125,37 +128,55 @@ int main(int argc, char *argv[]) {
125128

126129
int ret = system(command.c_str());
127130
if (ret != 0) {
128-
throw runtime_error("Command to get files failed!\n" + command);
131+
cerr << ("Command to get files failed!\n" + command) << endl;
132+
return 1;
129133
}
130134

131135
string catcmd = "cat adds";
132136
cout << "Binaries to download:" << endl;
133137
ret = system(catcmd.c_str());
134138
if (ret != 0) {
135-
throw runtime_error("Failed to run command: " + catcmd);
139+
cerr << ("Failed to run command: " + catcmd) << endl;
140+
return 1;
136141
}
137142

138-
array<char, 128> buffer;
139-
string content;
140-
FILE* file = fopen("adds", "r");
141-
if (!file) {
142-
throw runtime_error("Failed to open adds file!");
143+
array<char, 128> addsbuffer;
144+
string addscontent;
145+
FILE* addsfile = fopen("adds", "r");
146+
if (!addsfile) {
147+
cerr << ("Failed to open adds file!") << endl;
148+
return 1;
143149
}
144-
while (fgets(buffer.data(), buffer.size(), file) != nullptr) {
145-
content += buffer.data();
150+
while (fgets(addsbuffer.data(), addsbuffer.size(), addsfile) != nullptr) {
151+
addscontent += addsbuffer.data();
146152
}
147-
fclose(file);
153+
fclose(addsfile);
148154
system(("wget -q " + setmirlink + packagename + "/optional.json").c_str());
155+
156+
157+
system(("wget -q " + setmirlink + packagename + "/version").c_str());
158+
array<char, 128> versionbuffer;
159+
string versioncontent;
160+
FILE* versionfile = fopen("version", "r");
161+
if (!versionfile) {
162+
cerr << ("Failed to open version file!") << endl;
163+
return 1;
164+
}
165+
while (fgets(versionbuffer.data(), versionbuffer.size(), versionfile) != nullptr) {
166+
versioncontent += versionbuffer.data();
167+
}
168+
fclose(versionfile);
149169
cout << "Optional packages:" << endl;
150170
system("cat optional.json"); cout << endl;
151-
stringstream ss(content);
171+
stringstream ss(addscontent);
152172
string line;
153173
while (getline(ss, line)) {
154174
string download_command = catcommand + line;
155175
cout << "Downloading " << line << endl;
156176
ret = system(download_command.c_str());
157177
if (ret != 0) {
158-
throw runtime_error("Failed to run command: " + download_command);
178+
cerr << ("Failed to run command: " + download_command) << endl;
179+
return 1;
159180
}
160181
};
161182
ss.clear();
@@ -167,9 +188,19 @@ int main(int argc, char *argv[]) {
167188
if (ret != 0) {
168189
throw runtime_error("Failed to run command: " + chmodcommand);
169190
}
170-
}
191+
};
192+
ss.clear();
193+
ss.seekg(0,ss.beg);
194+
arkrjson["packagever"][packagename] = versioncontent;
195+
171196
} else if (action==2) {
172-
packlist.erase(remove(packlist.begin(), packlist.end(), argv[i]));
197+
auto iter = remove(packlist.begin(), packlist.end(), argv[i]);
198+
if (iter != packlist.end()) {
199+
packlist.erase(iter, packlist.end());
200+
} else {
201+
cerr << "No package named that!" << endl;
202+
system("cat /etc/arkr.json");
203+
}
173204
}
174205
}
175206
if (arkrjson["packages"]==packlist){

0 commit comments

Comments
 (0)