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 9, 2024
1 parent ab26aa1 commit 6cbdab5
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 48 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Easily manage multiple FLOSS repositories.
```shell
onur grab
onur backup nuxt awesomewm gitignore
onur config c.misc cli11 https://github.com/cliutils/cli11 main
onur --help
```

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: 59 additions & 9 deletions src/database/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

#include <cstdlib>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <list>
#include <string>

#include <nlohmann/json.hpp>

#include "../include/konfig.hpp"
#include "../include/misc.hpp"
#include "../include/parse.hpp"
#include "../include/project.hpp"

Expand Down Expand Up @@ -52,29 +55,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 +93,50 @@ 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
{
bool result{ false };
for (auto config : multi ())
if (config.name == name)
result = true;
// std::for_each (multi ().begin (), multi ().end (),
// [name, &result] (Konfig config) {
// std::cout << "MEH";
// result = { config.name == name };
// std::cout << "FOOL";
// });

return result;
}

// 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 ())
{
std::cout << "Either name or url of project are missing. Exiting!"
<< std::endl;
return;
}

Project project{ entries };

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

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

std::cout << std::format ("Saving config with name {} as {}", konfig.name,
j.dump ());
}
2 changes: 1 addition & 1 deletion src/database/repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Repository::allConfigs (void) -> list<path>
list<path> result;
Globals globals;

printf (" Configurations: [");
printf ("Configurations: [");

for (auto config : directory_iterator (globals.onurDir))
{
Expand Down
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
79 changes: 67 additions & 12 deletions src/handlers/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
*/

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

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

using std::cout;
using std::endl;
Expand All @@ -31,25 +33,24 @@ Commands::Commands () {}
auto
Commands::grab (void) -> void
{
for (auto single : parse.multi ())
for (auto singleConfig : parse.multi ())
{
cout << "\n " << single.topic << ":" << endl;
cout << "\n " << singleConfig.name << ":" << endl;

for (auto subtopic : single.subtopics)
for (auto topic : singleConfig.topics)
{
cout << " + " << subtopic.first << endl;
for (auto project : subtopic.second)
cout << " + " << topic.first << endl;
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;
Expand All @@ -62,3 +63,57 @@ Commands::backup (void) -> void
{
cout << "Backing up" << endl;
}

auto
Commands::config (std::string name, ConfigEntries entries) -> void
{
std::string _name{ name };
std::optional<std::string> _topic;

if (name.contains ("."))
{
std::size_t dot_positon{ name.find (".") };
_name = { name.substr (0, dot_positon) };
_topic = { name.substr (dot_positon + 1) };
}

if (_topic.has_value ())
parse.save (_name, _topic.value (), entries);

// if (!parse.exist (_name))
// {
// if (!_topic.has_value ())
// {
// cout << "Provide a topic, exiting!";
// }

// cout << std::format ("Config does not exists: {}, imma creating it!",
// name);

// parse.save (_name, _topic.value (), entries);

// return;
// }

for (auto singleConfig : parse.multi ())
{
if (_name == singleConfig.name)
{
cout << "\n" << singleConfig.name << ":" << endl;
for (auto topic : singleConfig.topics)
{
if (_topic.has_value () && _topic.value () != topic.first)
continue;

cout << " + " << topic.first << endl;

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

std::string m{ std::format ("{} {} {} {}", name,
entries.name.has_value (),
entries.url.has_value (), entries.branch) };
}
}
17 changes: 9 additions & 8 deletions src/handlers/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ printProjectInfo (Project project) -> void
{
std::string::size_type nameLength = 27;
auto nameTruncated
= project.name.length () <= nameLength
? project.name
: project.name.substr (0, nameLength).append ("...");
= project.Name ().length () <= nameLength
? project.Name ()
: project.Name ().substr (0, nameLength).append ("...");
std::string::size_type urlLength = 60;
auto urlTruncated = project.url.length () <= urlLength
? project.url
: project.url.substr (0, urlLength).append ("...");
auto message{ format ("{:5}- {:35} {:75} {}", "", nameTruncated,
urlTruncated, project.branch) };
auto urlTruncated
= project.Url ().length () <= urlLength
? project.Url ()
: project.Url ().substr (0, urlLength).append ("...");
auto message{ format ("{:3}- {:35} {:75} {}", "", nameTruncated,
urlTruncated, project.Branch ()) };
cout << message << endl;
}
3 changes: 2 additions & 1 deletion src/include/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#include "actions.hpp"
#include "globals.hpp"
#include "misc.hpp"
#include "parse.hpp"
#include "project.hpp"

class Commands
{
Expand All @@ -31,4 +31,5 @@ class Commands

auto grab (void) -> void;
auto backup (void) -> void;
auto config (std::string name, ConfigEntries edit) -> void;
};
26 changes: 24 additions & 2 deletions src/include/konfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,35 @@
#include <list>
#include <map>

#include <nlohmann/json.hpp>

#include "project.hpp"

class Konfig
{
public:
Konfig (std::string &name, std::map<std::string, std::list<Project>> &topics)
: name (name), topics (topics) {};

Konfig ();

std::string topic;
std::map<std::string, std::list<Project> > subtopics;
std::string name;
std::map<std::string, std::list<Project>> topics;

nlohmann::json
to_json () const
{
nlohmann::json j;
j["name"] = name;
j["topics"] = nlohmann::json::object ();
for (const auto &[topic, projects] : topics)
{
j["topics"][topic] = nlohmann::json::array ();
for (const auto &project : projects)
{
j["topics"][topic].push_back (project.to_json ());
}
}
return j;
}
};
26 changes: 26 additions & 0 deletions src/include/misc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Onur is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Onur is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Onur. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <optional>
#include <string>

struct ConfigEntries
{
std::optional<std::string> name;
std::optional<std::string> url;
std::string branch{ "master" };
};
Loading

0 comments on commit 6cbdab5

Please sign in to comment.