-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CLI commands to manage source registries
- Loading branch information
Showing
10 changed files
with
281 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/cli/src/cli-commands/source-registry/source-registry-add-cli-command.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/cli/src/cli-commands/source-registry/source-registry-add-cli-command.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
32 changes: 32 additions & 0 deletions
32
src/cli/src/cli-commands/source-registry/source-registry-cli-command.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/cli/src/cli-commands/source-registry/source-registry-cli-command.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
41 changes: 41 additions & 0 deletions
41
src/cli/src/cli-commands/source-registry/source-registry-list-cli-command.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/cli/src/cli-commands/source-registry/source-registry-list-cli-command.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
51 changes: 51 additions & 0 deletions
51
src/cli/src/cli-commands/source-registry/source-registry-remove-cli-command.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/cli/src/cli-commands/source-registry/source-registry-remove-cli-command.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters