SET(CKM_DB_TOOLS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/ckm-logic-ext.cpp
${CMAKE_CURRENT_SOURCE_DIR}/db-wrapper.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/ui.cpp
${KEY_MANAGER_PATH}/common/ckm-error.cpp
${KEY_MANAGER_PATH}/crypto/platform/decider.cpp
${KEY_MANAGER_PATH}/crypto/sw-backend/internals.cpp
${KEY_MANAGER_PATH}/crypto/sw-backend/obj.cpp
${KEY_MANAGER_PATH}/crypto/sw-backend/store.cpp
+ ${KEY_MANAGER_PATH}/dpl/core/src/colors.cpp
${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp
${KEY_MANAGER_PATH}/dpl/db/src/sql_connection.cpp
${KEY_MANAGER_PATH}/initial-values/BufferHandler.cpp
/*
- * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <unistd.h>
#include "db-wrapper.h"
+#include "ui.h"
using namespace std;
using namespace CKM;
int ret;
if (CKM_API_SUCCESS != (ret = source.unlock())) { // this should migrate database to newest version
- cerr << "Unlocking source database failed: " << ret << endl;
+ UI::error() << "unlocking source database failed: " << APICodeToString(ret) << endl;
return 1;
}
if (CKM_API_SUCCESS != (ret = target.unlock())) { // this should migrate database to newest version
- cerr << "Unlocking target database failed: " << ret << endl;
+ UI::error() << "unlocking target database failed: " << APICodeToString(ret) << endl;
return 1;
}
try {
target.saveRow(e);
} catch (const CKM::DB::SqlConnection::Exception::Base &e) {
- cerr << "Sql exception. Migration failed or object already exist in database: " << e.DumpToString() << endl;
+ UI::error()
+ << "sql exception, migration failed or object already exists in the database: "
+ << e.DumpToString() << endl;
}
}
}
}
if (uid1 == uid2) {
- cerr << "Source database and target database must be different!" << endl;
+ UI::error() << "source and target databases must be different" << endl;
return 1;
}
return mergeDatabase(uid1, password1, uid2, password2, filters);
} catch (const CKM::DB::SqlConnection::Exception::InvalidColumn &e) {
- cerr << "Invalid Column exception was catched. Probably migration failed. " << e.DumpToString() << endl;
+ UI::error() << "invalid column exception, probably a failed migration: " << e.DumpToString()
+ << endl;
} catch (const invalid_argument &e) {
- cerr << "Argument could not be converted" << endl;
+ UI::error() << "argument could not be converted: " << e.what() << endl;
} catch (const out_of_range &e) {
- cerr << "Out of range" << endl;
+ UI::error() << "argument out of range: " << e.what() << endl;
} catch (const exception &e) {
- cerr << "Unexpected error: " << e.what() << endl;
+ UI::error() << "unexpected exception: " << e.what() << endl;
} catch (...) {
- cerr << "Unknown exception" << endl;
+ UI::error() << "unknown exception" << endl;
}
return 1;
/*
- * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <unistd.h>
#include "db-wrapper.h"
+#include "ui.h"
using namespace std;
using namespace CKM;
int retCode;
if (CKM_API_SUCCESS != (retCode = dbw.unlock())) {
- cerr << "Unlocking database failed: " << APICodeToString(retCode) << endl;
+ UI::error() << "unlocking database failed: " << APICodeToString(retCode) << endl;
return -1;
}
- cout << "Database unlocked" << endl;
+ UI::info() << "database unlocked" << endl;
while (true) {
string cmd;
if (argcmd.empty()) {
- cout << "> ";
+ cmd = UI::promptLine(">");
- if (!getline(cin, cmd)) {
+ if (!std::cin) {
cout << "exit" << endl;
break; // EOF
}
}
dbw.lock();
- cout << "Database locked" << endl;
+ UI::info() << "database locked" << endl;
return 0;
- } catch (const std::invalid_argument &e) {
- cerr << "Argument could not be converted" << endl;
- } catch (const std::out_of_range &e) {
- cerr << "Out of range" << endl;
- } catch (const std::exception &e) {
- cerr << "Exception: " << e.what() << endl;
+ } catch (const invalid_argument &e) {
+ UI::error() << "argument could not be converted: " << e.what() << endl;
+ } catch (const out_of_range &e) {
+ UI::error() << "argument out of range: " << e.what() << endl;
+ } catch (const exception &e) {
+ UI::error() << "unexpected error: " << e.what() << endl;
} catch (...) {
- cerr << "Unexpected exception occurred" << endl;
+ UI::error() << "unknown exception" << endl;
}
return -1;
}
* @version 1.0
*/
#include "db-wrapper.h"
+#include "ui.h"
#include <string>
#include <algorithm>
displayRow(row, trim);
}
} catch (const DB::SqlConnection::Exception::Base &e) {
- std::cerr << e.GetMessage() << std::endl;
+ UI::error() << "sql exception: " << e.GetMessage() << std::endl;
} catch (const Exc::Exception &e) {
- std::cerr << e.message() << std::endl;
+ UI::error() << "unexpected exception: " << e.message() << std::endl;
} catch (const std::exception &e) {
- std::cerr << e.what() << std::endl;
+ UI::error() << "unexpected exception: " << e.what() << std::endl;
} catch (...) {
- std::cerr << "Unexpected exception occurred" << std::endl;
+ UI::error() << "unknown exception" << std::endl;
}
}
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @file ui.cpp
+ * @author Mateusz Cegielka (m.cegielka@samsung.com)
+ * @version 1.0
+ */
+
+#include <dpl/colors.h>
+#include "ui.h"
+
+#include <unistd.h>
+
+namespace CKM {
+
+namespace UI {
+
+std::ostream& info()
+{
+ return std::cerr
+ << Colors::Text::BOLD_GREEN_BEGIN << "info:"
+ << Colors::Text::BOLD_GREEN_END << " ";
+}
+
+std::ostream& warning()
+{
+ return std::cerr
+ << Colors::Text::BOLD_YELLOW_BEGIN << "warning:"
+ << Colors::Text::BOLD_YELLOW_END << " ";
+}
+
+std::ostream& error()
+{
+ return std::cerr
+ << Colors::Text::BOLD_RED_BEGIN << "error:"
+ << Colors::Text::BOLD_RED_END << " ";
+}
+
+std::string promptLine(const std::string &prompt)
+{
+ std::cerr
+ << Colors::Text::BOLD_WHITE_BEGIN << prompt
+ << Colors::Text::BOLD_WHITE_END << " ";
+
+ std::string buf;
+ std::getline(std::cin, buf);
+ return buf;
+}
+
+std::string promptPassword(const std::string &prompt)
+{
+ std::string colorPrompt
+ = Colors::Text::BOLD_WHITE_BEGIN + prompt
+ + Colors::Text::BOLD_WHITE_END + " ";
+ char *password = getpass(colorPrompt.c_str());
+ return password ? password : "";
+}
+
+} // namespace UI
+
+} // namespace CKM
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @file ui.h
+ * @author Mateusz Cegielka (m.cegielka@samsung.com)
+ * @version 1.0
+ */
+
+#pragma once
+
+#include <iostream>
+#include <string>
+
+namespace CKM {
+
+namespace UI {
+
+std::ostream& info();
+std::ostream& warning();
+std::ostream& error();
+
+std::string promptLine(const std::string &prompt);
+std::string promptPassword(const std::string &prompt);
+
+} // namespace UI
+
+} // namespace CKM