Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
navrocky committed Apr 5, 2024
1 parent f8640cc commit 60ae7ac
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 1.1.0 (2024.04.05)

* Add bash completion
* Add installation script
* Support for `docker compose` command
* Terminate partially started containers if the entire workspace cannot be started

# 1.0.0 (2024.04.04)

* Initial release
2 changes: 2 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set(SOURCES
state_repository.cpp
process_executor.h
process_executor.cpp
compose_executor.h
compose_executor.cpp

yaml_workspaces_repository.h
yaml_workspaces_repository.cpp
Expand Down
30 changes: 30 additions & 0 deletions cli/compose_executor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "compose_executor.h"

#include <format>

using namespace std;

ComposeExecutorImpl::ComposeExecutorImpl(ProcessExecutorPtr processExecutor)
: processExecutor(processExecutor)
{
if (processExecutor->exec("docker compose --help > /dev/null") == 0)
composeCommand = "docker compose";
else
composeCommand = "docker-compose";
}

void ComposeExecutorImpl::up(const std::string& file, bool detach)
{
std::string command = format("{} -f {} up", composeCommand, file);
if (detach)
command += " -d";
processExecutor->execOrThrow(command);
}

void ComposeExecutorImpl::down(const std::string& file, bool withVolumes)
{
std::string command = format("{} -f {} down", composeCommand, file);
if (withVolumes)
command += " -v";
processExecutor->execOrThrow(command);
}
23 changes: 23 additions & 0 deletions cli/compose_executor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "process_executor.h"

class ComposeExecutor {
public:
virtual void up(const std::string& file, bool detach) = 0;
virtual void down(const std::string& file, bool withVolumes) = 0;
};

using ComposeExecutorPtr = std::shared_ptr<ComposeExecutor>;

class ComposeExecutorImpl : public ComposeExecutor {
public:
ComposeExecutorImpl(ProcessExecutorPtr processExecutor);

void up(const std::string& file, bool detach);
void down(const std::string& file, bool withVolumes);

private:
ProcessExecutorPtr processExecutor;
std::string composeCommand;
};
5 changes: 3 additions & 2 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "commands/remove_command.h"
#include "commands/up_command.h"

static const char* APP_VERSION = "1.0.0";
static const char* APP_VERSION = "1.1.0";

using namespace std;
using namespace Args;
Expand Down Expand Up @@ -48,7 +48,8 @@ int main(int argc, char** argv)
auto workspacesRepo = make_shared<YamlWorkspacesRepository>(yamlConfig);
auto stateRepo = make_shared<YamlStateRepository>(yamlConfig);
auto processExecutor = make_shared<ProcessExecutor>();
auto workspaceService = make_shared<WorkspaceService>(workspacesRepo, stateRepo, processExecutor);
auto composeExecutor = make_shared<ComposeExecutorImpl>(processExecutor);
auto workspaceService = make_shared<WorkspaceService>(workspacesRepo, stateRepo, composeExecutor);
Commands commands = {
make_shared<AddCommand>(workspaceService),
make_shared<RemoveCommand>(workspaceService),
Expand Down
9 changes: 7 additions & 2 deletions cli/process_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
#include <format>
#include <stdexcept>

void ProcessExecutor::exec(const std::string& cmdLine)
int ProcessExecutor::exec(const std::string &cmdLine)
{
auto res = std::system(cmdLine.c_str());
return std::system(cmdLine.c_str());
}

void ProcessExecutor::execOrThrow(const std::string& cmdLine)
{
auto res = exec(cmdLine);
if (res != 0)
throw std::runtime_error(std::format("Command exited with status {}: {}", res, cmdLine));
}
3 changes: 2 additions & 1 deletion cli/process_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

class ProcessExecutor {
public:
void exec(const std::string& cmdLine);
int exec(const std::string& cmdLine);
void execOrThrow(const std::string& cmdLine);
};

using ProcessExecutorPtr = std::shared_ptr<ProcessExecutor>;
21 changes: 13 additions & 8 deletions cli/workspace_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ using namespace std;
namespace tc = termcolor;

WorkspaceService::WorkspaceService(
const WorkspacesRepositoryPtr& repo, const StateRepositoryPtr& stateRepo, const ProcessExecutorPtr& processExecutor)
const WorkspacesRepositoryPtr& repo, const StateRepositoryPtr& stateRepo, const ComposeExecutorPtr& composeExecutor)
: wpRepo(repo)
, stateRepo(stateRepo)
, processExecutor(processExecutor)
, composeExecutor(composeExecutor)
{
}

Expand Down Expand Up @@ -53,10 +53,7 @@ void WorkspaceService::down(bool purge)
if (!currentWpName.has_value())
return;
auto wp = getWorkspace(currentWpName.value());
string additionalFlags;
if (purge)
additionalFlags += " -v";
processExecutor->exec(format("docker-compose -f {} down{}", wp.composeFile, additionalFlags));
composeExecutor->down(wp.composeFile, purge);
stateRepo->setCurrentWorkspace(std::nullopt);
cout << "" << "Workspace \"" << tc::bold << *currentWpName << tc::reset << "\" stopped" << endl;
if (purge)
Expand All @@ -70,8 +67,16 @@ void WorkspaceService::up(const std::string& name, bool clean)
if (currentWpName.has_value() && *currentWpName != name)
down(false);
if (clean)
processExecutor->exec(format("docker-compose -f {} down -v", wp.composeFile));
processExecutor->exec(format("docker-compose -f {} up -d", wp.composeFile));
composeExecutor->down(wp.composeFile, true);
try {
composeExecutor->up(wp.composeFile, true);
} catch (...) {
cerr << "" << "Cannot start workspace \"" << tc::bold << name << tc::reset
<< "\". Shutting down partially started containers." << endl;
composeExecutor->down(wp.composeFile, false);
throw;
}

stateRepo->setCurrentWorkspace(name);
cout << "" << "Workspace \"" << tc::bold << name << tc::reset << "\" activated" << endl;
}
Expand Down
6 changes: 3 additions & 3 deletions cli/workspace_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include <memory>

#include "process_executor.h"
#include "compose_executor.h"
#include "state_repository.h"
#include "workspaces_repository.h"

class WorkspaceService {
public:
WorkspaceService(const WorkspacesRepositoryPtr& repo, const StateRepositoryPtr& stateRepo,
const ProcessExecutorPtr& processExecutor);
const ComposeExecutorPtr& composeExecutor);

void list(bool namesOnly);
void add(const std::string& name, const std::string& composeFile);
Expand All @@ -23,7 +23,7 @@ class WorkspaceService {

WorkspacesRepositoryPtr wpRepo;
StateRepositoryPtr stateRepo;
ProcessExecutorPtr processExecutor;
ComposeExecutorPtr composeExecutor;
};

using WorkspaceServicePtr = std::shared_ptr<WorkspaceService>;
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e
set -x

curl -L https://github.com/navrocky/dcw/releases/download/1.0.0/dcw -o /usr/local/bin/dcw
curl -L https://github.com/navrocky/dcw/releases/download/1.1.0/dcw -o /usr/local/bin/dcw
chmod +x /usr/local/bin/dcw
curl -L https://github.com/navrocky/dcw/raw/master/completion.bash -o /etc/bash_completion.d/dcw_completion.bash

Expand Down

0 comments on commit 60ae7ac

Please sign in to comment.