Merge pull request #553 from kiwix/manager_return_code

This commit is contained in:
Matthieu Gautier 2022-06-03 09:11:59 +02:00 committed by GitHub
commit 78887d6815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -169,7 +169,10 @@ int handle_add(kiwix::Library* library, const std::string& libraryPath,
std::string zimPath = argv[i]; std::string zimPath = argv[i];
if (!zimPath.empty()) { if (!zimPath.empty()) {
auto _zimPathToSave = zimPathToSave == "." ? zimPath : zimPathToSave; auto _zimPathToSave = zimPathToSave == "." ? zimPath : zimPathToSave;
manager.addBookFromPathAndGetId(zimPath, _zimPathToSave, url, false); if (manager.addBookFromPathAndGetId(zimPath, _zimPathToSave, url, false).empty()) {
std::cerr << "Cannot add zim " << zimPath << " to the library." << std::endl;
resultCode = 1;
}
} else { } else {
std::cerr << "Invalid zim file path" << std::endl; std::cerr << "Invalid zim file path" << std::endl;
resultCode = 1; resultCode = 1;
@ -251,7 +254,7 @@ int main(int argc, char** argv)
/* Print usage)) if necessary */ /* Print usage)) if necessary */
if (libraryPath == "" || action == NONE) { if (libraryPath == "" || action == NONE) {
usage(); usage();
exit(1); return -1;
} }
/* Try to read the file */ /* Try to read the file */
@ -259,7 +262,10 @@ int main(int argc, char** argv)
? kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), libraryPath) ? kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), libraryPath)
: libraryPath; : libraryPath;
kiwix::Manager manager(&library); kiwix::Manager manager(&library);
manager.readFile(libraryPath, false); if (!manager.readFile(libraryPath, false)) {
std::cerr << "Cannot read the library " << libraryPath << std::endl;
return 1;
}
/* SHOW */ /* SHOW */
int exitCode = 0; int exitCode = 0;
@ -277,10 +283,18 @@ int main(int argc, char** argv)
break; break;
} }
/* Rewrite the library file */ if (exitCode) {
if (action == REMOVE || action == ADD) { return exitCode;
library.writeToFile(libraryPath);
} }
exit(exitCode); /* Rewrite the library file */
if (action == REMOVE || action == ADD) {
// writeToFile return true (1) if everything is ok => exitCode is 0
if (!library.writeToFile(libraryPath)) {
std::cerr << "Cannot write the library " << libraryPath << std::endl;
return 1;
}
}
return 0;
} }