Skip to content

Commit

Permalink
feat: add CLI commands to manage source registries
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Apr 20, 2024
1 parent 6bec8a5 commit 7b8d670
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/cli/src/cli-commands/site/site-add-cli-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void SiteAddCliCommand::run()
}

m_profile->addSite(SiteFactory::fromUrl(url, source, m_profile));
stdOut << "Site added:" << url;
stdOut << "Site added: " << url << Qt::endl;
emit finished(0);
return;
}
Expand All @@ -80,7 +80,7 @@ void SiteAddCliCommand::run()
}

m_profile->addSite(SiteFactory::fromUrl(url, source, m_profile));
stdOut << "Site added:" << url;
stdOut << "Site added: " << url << Qt::endl;
emit finished(0);
});
sourceGuesser.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "source-registry-add-cli-command.h"
#include <QDebug>
#include <utility>
#include "models/profile.h"
#include "models/source-registry.h"


SourceRegistryAddCliCommand::SourceRegistryAddCliCommand(QStringList arguments, Profile *profile, QObject *parent)
: CliAction(std::move(arguments), parent), m_profile(profile)
{
m_parser.addHelpOptionOnly();
m_parser.setApplicationDescription("Add a new source registry");
m_parser.setPrefix({ "source-registry", "add" });

m_parser.addPositionalArgument("url", "The URL of the source registry to add", "<url>");
}


bool SourceRegistryAddCliCommand::validate()
{
// One and only one source registry can be added at a time
if (m_parser.positionalArguments().length() != 1) {
m_parser.showHelp();
return false;
}

return true;
}

void SourceRegistryAddCliCommand::run()
{
// Build a source registry instance using the positional arguments
const QString url = m_parser.positionalArguments().first();
auto *sourceRegistry = new SourceRegistry(url);

// Try to load the source registry first to ensure it's valid
connect(sourceRegistry, &SourceRegistry::loaded, [=](bool ok) {
if (!ok) {
qWarning() << "The source registry could not be added:" << url;
sourceRegistry->deleteLater();
emit finished(1);
return;
}

QTextStream stdOut(stdout);
m_profile->addSourceRegistry(sourceRegistry);
stdOut << "Source registry added: " << url << Qt::endl;
emit finished(0);
});
sourceRegistry->load();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SOURCE_REGISTRY_ADD_CLI_COMMAND_H
#define SOURCE_REGISTRY_ADD_CLI_COMMAND_H

#include <QObject>
#include "../cli-action.h"


class Profile;

class SourceRegistryAddCliCommand : public CliAction
{
Q_OBJECT

public:
explicit SourceRegistryAddCliCommand(QStringList arguments, Profile *profile, QObject *parent = nullptr);

bool validate() override;
void run() override;

private:
Profile *m_profile;
};

#endif // SOURCE_REGISTRY_ADD_CLI_COMMAND_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "source-registry-cli-command.h"
#include "source-registry-add-cli-command.h"
#include "source-registry-list-cli-command.h"
#include "source-registry-remove-cli-command.h"


SourceRegistryCliCommand::SourceRegistryCliCommand(QStringList arguments, Profile *profile, QObject *parent)
: CliSubcommand(std::move(arguments), parent), m_profile(profile)
{
m_parser.addHelpOptionOnly();
m_parser.setApplicationDescription("Manage source registries");
m_parser.setPrefix({ "source-registry" });

m_parser.addCommand("list", "List source registries");
m_parser.addCommand("add", "Add a new source registry");
m_parser.addCommand("remove", "Remove an existing source registry");
}


CliCommand *SourceRegistryCliCommand::getCommand(const QString &command)
{
if (command == "list") {
return new SourceRegistryListCliCommand(m_arguments, m_profile, parent());
}
if (command == "add") {
return new SourceRegistryAddCliCommand(m_arguments, m_profile, parent());
}
if (command == "remove") {
return new SourceRegistryRemoveCliCommand(m_arguments, m_profile, parent());
}
return nullptr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef SOURCE_REGISTRY_CLI_COMMAND_H
#define SOURCE_REGISTRY_CLI_COMMAND_H

#include <QObject>
#include "../cli-subcommand.h"


class Profile;

class SourceRegistryCliCommand : public CliSubcommand
{
Q_OBJECT

public:
explicit SourceRegistryCliCommand(QStringList arguments, Profile *profile, QObject *parent = nullptr);

CliCommand *getCommand(const QString &command) override;

private:
Profile *m_profile;
};

#endif // SOURCE_REGISTRY_CLI_COMMAND_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "source-registry-list-cli-command.h"
#include <QDebug>
#include <QSettings>
#include <utility>
#include "models/profile.h"
#include "models/source-registry.h"


SourceRegistryListCliCommand::SourceRegistryListCliCommand(QStringList arguments, Profile *profile, QObject *parent)
: CliAction(std::move(arguments), parent), m_profile(profile)
{
m_parser.addHelpOptionOnly();
m_parser.setApplicationDescription("List source registries");
m_parser.setPrefix({ "source-registry", "list" });
}


bool SourceRegistryListCliCommand::validate()
{
// Nothing to validate
return true;
}

void SourceRegistryListCliCommand::run()
{
// Log a warning and early return if no source registries are found
const QList<SourceRegistry*> &sourceRegistries = m_profile->getSourceRegistries();
if (sourceRegistries.isEmpty()) {
qWarning() << "No source registries found";
emit finished(0);
return;
}

// Print all source registries' urls
QTextStream stdOut(stdout);
for (SourceRegistry *sourceRegistry : sourceRegistries) {
stdOut << sourceRegistry->jsonUrl() << Qt::endl;
}

emit finished(0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SOURCE_REGISTRY_LIST_CLI_COMMAND_H
#define SOURCE_REGISTRY_LIST_CLI_COMMAND_H

#include <QObject>
#include "../cli-action.h"


class Profile;

class SourceRegistryListCliCommand : public CliAction
{
Q_OBJECT

public:
explicit SourceRegistryListCliCommand(QStringList arguments, Profile *profile, QObject *parent = nullptr);

bool validate() override;
void run() override;

private:
Profile *m_profile;
};

#endif // SOURCE_REGISTRY_LIST_CLI_COMMAND_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "source-registry-remove-cli-command.h"
#include <QDebug>
#include <utility>
#include "models/profile.h"
#include "models/source-registry.h"


SourceRegistryRemoveCliCommand::SourceRegistryRemoveCliCommand(QStringList arguments, Profile *profile, QObject *parent)
: CliAction(std::move(arguments), parent), m_profile(profile)
{
m_parser.addHelpOptionOnly();
m_parser.setApplicationDescription("Remove an existing source registry");
m_parser.setPrefix({ "source-registry", "remove" });

m_parser.addPositionalArgument("url", "The URL of the source registry to remove", "<url>");
}


bool SourceRegistryRemoveCliCommand::validate()
{
// One and only one source registry can be removed at a time
if (m_parser.positionalArguments().length() != 1) {
m_parser.showHelp();
return false;
}

return true;
}

void SourceRegistryRemoveCliCommand::run()
{
const QString url = m_parser.positionalArguments().first();

// Find the correct source registry
auto sourceRegistryIt = std::find_if(
m_profile->getSourceRegistries().constBegin(),
m_profile->getSourceRegistries().constEnd(),
[&](SourceRegistry *registry) { return registry->jsonUrl() == url; }
);
if (sourceRegistryIt == m_profile->getSourceRegistries().constEnd()) {
qWarning() << "The source registry was not found:" << url;
emit finished(1);
return;
}

// Actually remove the source registry
QTextStream stdOut(stdout);
m_profile->removeSourceRegistry(*sourceRegistryIt);
stdOut << "Source registry removed: " << url << Qt::endl;
emit finished(0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SOURCE_REGISTRY_REMOVE_CLI_COMMAND_H
#define SOURCE_REGISTRY_REMOVE_CLI_COMMAND_H

#include <QObject>
#include "../cli-action.h"


class Profile;

class SourceRegistryRemoveCliCommand : public CliAction
{
Q_OBJECT

public:
explicit SourceRegistryRemoveCliCommand(QStringList arguments, Profile *profile, QObject *parent = nullptr);

bool validate() override;
void run() override;

private:
Profile *m_profile;
};

#endif // SOURCE_REGISTRY_REMOVE_CLI_COMMAND_H
15 changes: 9 additions & 6 deletions src/cli/src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "cli-commands/get-tags-cli-command.h"
#include "cli-commands/load-tag-database-cli-command.h"
#include "cli-commands/site/site-cli-command.h"
#include "cli-commands/source-registry/source-registry-cli-command.h"
#include "printers/json-printer.h"
#include "printers/simple-printer.h"
#include "logger.h"
Expand Down Expand Up @@ -44,6 +45,7 @@ int parseAndRunCliArgsV2(QCoreApplication *app, Profile *profile, bool defaultTo
}

parser.addCommand("source", "Manage sources");
parser.addCommand("source-registry", "Manage source registries");

parser.process(*app);

Expand All @@ -62,10 +64,13 @@ int parseAndRunCliArgsV2(QCoreApplication *app, Profile *profile, bool defaultTo
arguments.removeAll("--cli");

// Handle each specific command separately
CliCommand *cmd = nullptr;
QScopedPointer<CliCommand> cmd;
const QString command = parser.command();
if (command == "source") {
cmd = new SiteCliCommand(arguments, profile);
cmd.reset(new SiteCliCommand(arguments, profile));
}
if (command == "source-registry") {
cmd.reset(new SourceRegistryCliCommand(arguments, profile));
}

// If we're here, that means that help was requested or no command was passed
Expand All @@ -75,9 +80,7 @@ int parseAndRunCliArgsV2(QCoreApplication *app, Profile *profile, bool defaultTo
}

// Actually run the command
int exitCode = cmd->execute();
cmd->deleteLater();
return exitCode;
return cmd->execute();
}


Expand All @@ -90,7 +93,7 @@ int parseAndRunCliArgs(QCoreApplication *app, Profile *profile, bool defaultToGu
// Go through the new CLI for the various commands supported by it
const QStringList args = app->arguments();
bool guessUseCLI = (defaultToGui && (args.contains("-c") || args.contains("--cli"))) || (!defaultToGui && !args.contains("-g") && !args.contains("--gui"));
if (guessUseCLI && args.contains("source")) {
if (guessUseCLI && (args.contains("source") || args.contains("source-registry"))) {
return parseAndRunCliArgsV2(app, profile, defaultToGui, params, positionalArgs);
}

Expand Down

0 comments on commit 7b8d670

Please sign in to comment.