From 61fcdd9aa07f206220f86a08e8eb2efb8fcbed7f Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Wed, 27 Jan 2021 10:26:45 -0700 Subject: [PATCH] ci: enforce code style --- .clang-format | 8 +++++ .github/workflows/build.yml | 4 +++ Makefile | 3 ++ infoconv.cpp | 20 +++++------- infoedit.cpp | 61 ++++++++++++++++--------------------- infoedit.hpp | 25 ++++++--------- 6 files changed, 58 insertions(+), 63 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2f28dff --- /dev/null +++ b/.clang-format @@ -0,0 +1,8 @@ +--- +BasedOnStyle: Mozilla +AllowShortFunctionsOnASingleLine: Empty +BinPackArguments: true +BinPackParameters: true +ColumnLimit: 100 +ReflowComments: true +SortIncludes: true diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 03481e6..b3c5f3a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,6 +9,10 @@ jobs: - name: install dependencies run: | sudo apt-get install libboost-dev libboost-program-options-dev + - name: check code style + run: | + make lint + [[ $(git status --porcelain | tee >(cat 1>&2) | wc -l) -eq 0 ]] - name: build and install run: | make diff --git a/Makefile b/Makefile index 10b6b7c..f1f580f 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ infoconv: infoconv.cpp infoedit: infoedit.hpp infoedit.cpp $(CXX) -o infoedit -std=c++11 infoedit.cpp -lboost_program_options +lint: + clang-format-8 -i -style=file *.[hc]pp + install: infoconv infoedit install infoconv /usr/local/bin/infoconv install infoedit /usr/local/bin/infoedit diff --git a/infoconv.cpp b/infoconv.cpp index 45ba25c..27364c8 100644 --- a/infoconv.cpp +++ b/infoconv.cpp @@ -10,10 +10,10 @@ static void usage() { std::cerr << "USAGE\n" - " infoconv info2json < config.info > config.json\n" - " infoconv json2info < config.info > config.info\n" - " infoconv info2xml < config.info > config.xml\n" - " infoconv xml2info < config.xml > config.info\n"; + " infoconv info2json < config.info > config.json\n" + " infoconv json2info < config.info > config.info\n" + " infoconv info2xml < config.info > config.xml\n" + " infoconv xml2info < config.xml > config.info\n"; } static void @@ -58,17 +58,13 @@ main(int argc, char** argv) std::string action(argv[1]); if (action == "info2json") { info2json(); - } - else if (action == "json2info") { + } else if (action == "json2info") { json2info(); - } - else if (action == "info2xml") { + } else if (action == "info2xml") { info2xml(); - } - else if (action == "xml2info") { + } else if (action == "xml2info") { xml2info(); - } - else { + } else { usage(); return 2; } diff --git a/infoedit.cpp b/infoedit.cpp index 3d4200e..c889e16 100644 --- a/infoedit.cpp +++ b/infoedit.cpp @@ -14,10 +14,10 @@ */ #include "infoedit.hpp" -#include #include -#include #include +#include +#include namespace infoedit { @@ -31,8 +31,7 @@ InfoEditor::load(const std::string& fileName) try { boost::property_tree::info_parser::read_info(input, m_info); - } - catch (boost::property_tree::info_parser::info_parser_error& error) { + } catch (boost::property_tree::info_parser::info_parser_error& error) { std::stringstream msg; msg << "Failed to parse configuration file"; msg << " " << error.message() << " line " << error.line(); @@ -62,8 +61,7 @@ InfoEditor::remove(const std::string& section) std::size_t pos = section.find_last_of("."); if (pos == std::string::npos) { m_info.erase(section.c_str()); - } - else { + } else { boost::optional child = m_info.get_child_optional(section.substr(0, pos)); if (child) { @@ -101,50 +99,45 @@ main(int argc, char** argv) std::string value; namespace po = boost::program_options; - po::options_description description("Usage\n" - " infoedit [-f file] [-s|d|a|r path] [-v value]\n" - "Options"); - - description.add_options() - ("help,h", "print this help message") - ("file,f", po::value(&configFile), "the file to edit") - ("section,s", po::value(§ionPath), "the section to modify") - ("path,p", po::value(§ionPath), "the path to add - can be duplicate") - ("value,v", po::value(&value), "the value used to modify some section or add path") - ("delete,d", po::value(§ionPath), "the sub tree to delete") - ("add,a", po::value(§ionPath), "adds a sub tree") - ("replace,r", po::value(§ionPath), "replace the sub tree") - ; + po::options_description opts("Usage\n" + " infoedit [-f file] [-s|d|a|r path] [-v value]\n" + "Options"); + opts.add_options()("help,h", "print this help message"); + opts.add_options()("file,f", po::value(&configFile), "the file to edit"); + opts.add_options()("section,s", po::value(§ionPath), "the section to modify"); + opts.add_options()("path,p", po::value(§ionPath), + "the path to add - can be duplicate"); + opts.add_options()("value,v", po::value(&value), + "the value used to modify some section or add path"); + opts.add_options()("delete,d", po::value(§ionPath), "the sub tree to delete"); + opts.add_options()("add,a", po::value(§ionPath), "adds a sub tree"); + opts.add_options()("replace,r", po::value(§ionPath), "replace the sub tree"); po::variables_map vm; try { - po::store(po::command_line_parser(argc, argv).options(description).run(), vm); - po::notify(vm); - } - catch (const std::exception& e) { - std::cerr << "ERROR: " << e.what() - << "\n" - << description; + po::store(po::command_line_parser(argc, argv).options(opts).run(), vm); + po::notify(vm); + } catch (const std::exception& e) { + std::cerr << "ERROR: " << e.what() << "\n" << opts; return 1; } if (vm.count("help") > 0) { - std::cout << description; + std::cout << opts; return 0; } if (vm.count("file") == 0) { std::cerr << "ERROR: the file to edit should be specified" << "\n" - << description; + << opts; return 1; } InfoEditor editor; try { editor.load(configFile); - } - catch (const std::exception& e) { + } catch (const std::exception& e) { std::cerr << "ERROR: " << e.what() << std::endl; return 1; } @@ -179,15 +172,13 @@ main(int argc, char** argv) if (vm.count("replace") > 0) { editor.remove(sectionPath).insert(sectionPath, std::cin); } - } - catch (...) { + } catch (...) { return 1; } try { editor.save(configFile); - } - catch (...) { + } catch (...) { std::cerr << "Unable to save the edit to file" << std::endl; return 1; } diff --git a/infoedit.hpp b/infoedit.hpp index 45ab8c7..aecdf31 100644 --- a/infoedit.hpp +++ b/infoedit.hpp @@ -16,10 +16,10 @@ #ifndef INTEGRATION_TESTS_INSTALL_HELPERS_INFOEDIT_INFOEDIT_HPP #define INTEGRATION_TESTS_INSTALL_HELPERS_INFOEDIT_INFOEDIT_HPP +#include +#include #include #include -#include -#include namespace infoedit { @@ -31,28 +31,21 @@ public: public: Error(const std::string& what) : std::runtime_error(what) - { - } + {} }; public: - void - load(const std::string& fileName); + void load(const std::string& fileName); - InfoEditor& - modify(const std::string& section, const std::string& value); + InfoEditor& modify(const std::string& section, const std::string& value); - InfoEditor& - add(const std::string& section, const std::string& value); + InfoEditor& add(const std::string& section, const std::string& value); - InfoEditor& - remove(const std::string& section); + InfoEditor& remove(const std::string& section); - InfoEditor& - insert(const std::string& section, std::istream& stream); + InfoEditor& insert(const std::string& section, std::istream& stream); - void - save(const std::string& fileName); + void save(const std::string& fileName); private: boost::property_tree::ptree m_info;