Skip to content

Commit

Permalink
feat: edit/view configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
easbarba committed Aug 13, 2024
1 parent ab26aa1 commit c4ce0b7
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ along with Onur. If not, see <https://www.gnu.org/licenses/>.

# CHANGELOG

## 0.3.0

- feat: edit/view configurations

## 0.2.0

- feat: refactor, more tests and improve cli ui
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,30 @@ Easily manage multiple FLOSS repositories.
| [ruby](https://gitlab.com/easbarba/onur-ruby)



## Usage

```shell
onur grab
onur backup nuxt awesomewm gitignore
# grab all projects
onur grab

# grab only the c projects
onur grab c

# list the cpp configuration file
onur config cpp

# list topics of haskell
onur config haskell.

# list only the projects on misc topic of lisp
onur config lisp.misc

# add a new configuration with theses entries in the topic misc of c
onur config c.misc cli11 https://github.com/cliutils/cli11 main

# back up these projects as tar.gz
onur backup ecmascript.nuxt lua.awesomewm misc.gitignore

onur --help
```

Expand Down Expand Up @@ -90,7 +108,7 @@ depth = 1

## Installation

`onur` requires a c++ 20 compiler and `meson` to install, locally at `$HOME/.local/bin`, easily with : `make all install`.
`onur` requires a c++ 23 compiler and `meson` to install, locally at `$HOME/.local/bin`, easily with : `make all install`.

## Development

Expand Down
7 changes: 5 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ along with Onur. If not, see <https://www.gnu.org/licenses/>.

### High

- verbose
- flag: --verbose
- validation of repository links
- actions: --filter rust
- actions: --filter rust:misc
- actions: --only rust,haskel,commonlisp
- actions: --exclude rust,haskel,commonlisp

### Mid
- actions: onur config c.misc foo https://git@gmasd main

### Low

- config: move on these to a syntax check class
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ project(
'cpp',
license: 'GNU GPL version 3',
version: '0.2.0',
default_options: [ 'cpp_std=c++20', 'warning_level=3' ],
default_options: [ 'cpp_std=c++23', 'warning_level=3' ],
meson_version: '>=1.4.0',
)

Expand Down
68 changes: 57 additions & 11 deletions src/database/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#include <filesystem>
#include <fstream>
#include <list>
#include <string>

#include <nlohmann/json.hpp>
#include <print>
#include <string>

#include "../include/cli.hpp"
#include "../include/konfig.hpp"
#include "../include/parse.hpp"
#include "../include/project.hpp"
Expand Down Expand Up @@ -52,29 +53,29 @@ auto
Parse::single (path filepath) -> Konfig
{
Konfig result;
map<string, list<Project> > subtopiks;
map<string, list<Project>> _topics;

auto configParsed = parse_file (contents_of (filepath));
result.topic = { filepath.stem () };
result.name = { filepath.stem () };

for (auto &[subtopic, subtopics] : configParsed.items ())
for (auto &[topic, topics] : configParsed.items ())
{
list<Project> projects;
for (auto projekt : subtopics)
for (auto project : topics)
{
string branch{ "master" };
if (!projekt["branch"].is_null ())
branch = projekt["branch"];
if (!project["branch"].is_null ())
branch = project["branch"];

auto pkt{ Project (projekt["name"], projekt["url"], branch) };
auto pkt{ Project (project["name"], project["url"], branch) };

projects.push_back (pkt);
}

subtopiks[subtopic] = { projects };
_topics[topic] = { projects };
}

result.subtopics = { subtopiks };
result.topics = { _topics };
return result;
}

Expand All @@ -90,3 +91,48 @@ Parse::contents_of (string path_to_file) -> string
ifstream file (path_to_file);
return { istreambuf_iterator<char> (file), istreambuf_iterator<char>{} };
}

auto
Parse::exist (std::string name) -> bool
{
for (auto config : multi ())
if (config.name == name)
return true;

// std::for_each (multi ().begin (), multi ().end (),
// [name, &result] (Konfig config) {
// std::println ("MEH");
// result = { config.name == name };
// std::println ("FOOL");
// });

return false;
}

// Overload the to_json function for automatic conversion
void
to_json (nlohmann::json &j, const Konfig &k)
{
j = k.to_json ();
}

auto
Parse::save (std::string name, std::string topic,
ConfigEntries entries) -> void
{
// if (entries.name || entries.url.has_value () || !entries.branch.empty ())
// {
// std::println ("Either name or url of project are missing. Exiting!");
// return;
// }

Project project{ entries };

std::map<std::string, std::list<Project>> topics;
topics[topic] = { entries };

Konfig konfig{ name, topics };
nlohmann::json j = konfig;

std::println ("Saving config with name {} as \n{}", konfig.name, j.dump ());
}
4 changes: 0 additions & 4 deletions src/database/repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Repository::allConfigs (void) -> list<path>
list<path> result;
Globals globals;

printf (" Configurations: [");

for (auto config : directory_iterator (globals.onurDir))
{
if (config.path ().extension () != ".json")
Expand All @@ -46,10 +44,8 @@ Repository::allConfigs (void) -> list<path>
if (file.peek () == std::ifstream::traits_type::eof ())
continue;

printf (" %s ", config.path ().stem ().c_str ());
result.push_back (config);
}

printf (" ]\n");
return result;
}
2 changes: 1 addition & 1 deletion src/handlers/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ auto
Actions::klone (Project project, path dirpath) -> void
{
auto finalCommand{ format (
"git clone --single-branch --depth=1 --quiet {} {}", project.url,
"git clone --single-branch --depth=1 --quiet {} {}", project.Url (),
dirpath.string ()) };
system (finalCommand.c_str ());
}
Expand Down
134 changes: 115 additions & 19 deletions src/handlers/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,147 @@
*/

#include <filesystem>
#include <iostream>
#include <ostream>
#include <format>
#include <optional>
#include <print>
#include <string>

#include "../include/commands.hpp"
#include "../include/globals.hpp"
#include "helpers.hpp"

using std::cout;
using std::endl;
using std::filesystem::exists;
using std::filesystem::path;

Commands::Commands () {}

auto
Commands::grab (void) -> void
Commands::grab (std::optional<std::string> name) -> void
{
for (auto single : parse.multi ())
for (auto singleConfig : parse.multi ())
{
cout << "\n " << single.topic << ":" << endl;
if (name.has_value () && name.value () != singleConfig.name)
continue;

for (auto subtopic : single.subtopics)
std::println ("{}: ", singleConfig.name);

for (auto topic : singleConfig.topics)
{
cout << " + " << subtopic.first << endl;
for (auto project : subtopic.second)
std::println (" + {}", topic.first);
for (auto project : topic.second)
{
auto placeholder{ path (globals.projectsDir / single.topic
/ subtopic.first / project.name) };
auto dirpath{ placeholder };
auto finalpath{ path (globals.projectsDir / singleConfig.name
/ topic.first / project.Name ()) };

printProjectInfo (project);

if (exists (dirpath / ".git" / "config"))
actions.pull (dirpath);
if (exists (finalpath / ".git" / "config"))
actions.pull (finalpath);
else
actions.klone (project, dirpath);
actions.klone (project, finalpath);
}

cout << endl;
std::println ();
}
}
}

auto
Commands::backup (void) -> void
{
cout << "Backing up" << endl;
std::println ("Backing up");
}

ConfigTopic
Commands::configTopicNew (std::string &name)
{
ConfigTopic meh;

if (name.contains ("."))
{
meh.dot = { true };

std::size_t dot_positon{ name.find (".") };
meh.name = { name.substr (0, dot_positon) };
meh.topic = { name.substr (dot_positon + 1) };
}

return meh;
}

auto
Commands::config (std::string name, ConfigEntries entries) -> void
{
ConfigTopic config{ configTopicNew (name) };

if (!parse.exist (config.name.value ()))
{
std::println ("No configuration by {} found!", config.name.value ());
return;
}

if (name.ends_with ("."))
{
printSingleConfig (config, true);
return;
}

if (!config.topic.has_value ())
{
printSingleConfig (config);
return;
}
else
{
printSingleConfig (config, true);
return;
}

// std::println (
// "Please provide a topic and entries to save a new one. Exiting!",
// config.name);

if (!entries.name.has_value () || !entries.url.has_value ())
{
std::println ("Enter name and url entries at least. Exiting!");
return;
}

parse.save (config.name.value (), config.topic.value (), entries);
}

auto
foo () -> void
{
std::print ("Configurations: [");
// printf (" %s ", config.path ().stem ().c_str ());
std::println (" ]\n");
}

void
Commands::printSingleConfig (ConfigTopic config, bool onlytopics)
{
for (auto singleConfig : parse.multi ())
{
if (config.name.value () != singleConfig.name)
continue;

std::println ("{}:", singleConfig.name);
for (auto topic : singleConfig.topics)
{
if (onlytopics)
{
std::print (" {} ", topic.first);
continue;
}

if (config.topic.has_value ()
&& config.topic.value () != topic.first)
continue;

std::println (" + {}", topic.first);

for (auto project : topic.second)
printProjectInfo (project);
}
}
}
Loading

0 comments on commit c4ce0b7

Please sign in to comment.