Skip to content

Commit

Permalink
Add proper CLI (#7)
Browse files Browse the repository at this point in the history
* feat(cli): add proper cli using args

* fix: add windows specific commands for command line git interaction (#6)
  • Loading branch information
Leonhard Breuer authored Aug 6, 2024
1 parent aca2d28 commit 20d934b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(fkYAML)

FetchContent_Declare(
args
GIT_REPOSITORY https://github.com/Taywee/args
GIT_TAG master
)

FetchContent_MakeAvailable(args)

add_executable(git_utils src/main.cpp
src/gitutil.cpp
src/gitutil.h
Expand All @@ -26,9 +34,7 @@ add_executable(git_utils src/main.cpp
src/version.h
src/config.cpp
src/config.h)

target_include_directories(${PROJECT_NAME} PRIVATE ${args_SOURCE_DIR})
target_link_libraries(git_utils PRIVATE fkYAML::fkYAML)

install(TARGETS git_utils DESTINATION bin)

include(CPack)
15 changes: 13 additions & 2 deletions src/gitutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
#include <memory>
#include <vector>
#include <regex>
#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#define popen _popen
#define pclose _pclose
#else
#include <unistd.h>
#include <cstdio>
#endif
#include "gitutil.h"

#include "common.h"
Expand Down Expand Up @@ -44,15 +53,17 @@ std::string GitUtil::getLog() {

std::vector<Commit> GitUtil::getCommits() {
std::vector<Commit> commits;
for (const auto log = getLog(); auto commit: splitGitLog(log))
const auto log = getLog();
for (auto commit: splitGitLog(log))
commits.emplace_back(commit);
std::reverse(commits.begin(), commits.end());
return commits;
}

Version GitUtil::calculateVersion() {
Version version;
for (const auto& commits = getCommits(); const auto &commit: commits) {
const auto& commits = getCommits();
for (const auto &commit: commits) {
const auto type = commit.getCommitType();
const auto minorVersionTypes = config.getConvention()->getTypesForVersion(VersionType::MINOR);
if ( contains<std::string>(minorVersionTypes, type))
Expand Down
36 changes: 32 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
#include <iostream>

#include <args.hxx>
#include "gitutil.h"

int main() {
GitUtil util;
std::cout << util.getVersion() << std::endl;
int main(int argc, char **argv) {
GitUtil gu;
args::ArgumentParser parser("git utils", "© 2024 Leonhard Breuer");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::CompletionFlag completion(parser, {"complete"});
// commands
args::Group commands(parser, "commands");
args::Command version(commands, "version", "generates the next version");
// parsing
try {
parser.ParseCLI(argc, argv);
if (version)
std::cout << gu.getVersion() << std::endl;
else
std::cout << "No command submitted" << std::endl;
} catch(const args::Completion& e) {
std::cout << e.what();
return 0;
} catch(const args::Help&) {
std::cout << parser << std::endl;
} catch(const args::ParseError& e) {
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
} catch(const args::ValidationError& e) {
std::cout << e.what() << std::endl;
std::cout << parser.Help() << std::endl;
return 1;
}
// GitUtil util;
// std::cout << util.getVersion() << std::endl;
return 0;
}

0 comments on commit 20d934b

Please sign in to comment.