Skip to content

Commit dc0b7a8

Browse files
committed
Update build configuration and enhance command structures
- Updated macOS version in CI configuration from 13 to 15. - Added submodule checkout step in CI for better dependency management. - Included Ninja build system in macOS and Ubuntu CI jobs. - Enhanced CreateContainerRequest structure to support labels. - Added methods for label handling in CreateContainerCmd. - Integrated EventsCmd into DockerClient and added related tests. - Improved exception handling in DockerException class. - Updated RemoveImageCmd to support force and no-prune options. - Added unit and integration tests for new command functionalities.
1 parent cd26f67 commit dc0b7a8

18 files changed

Lines changed: 466 additions & 105 deletions

.github/workflows/build_cmake.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ env:
1111

1212
jobs:
1313
macos-native-x86_64:
14-
name: 'macOS 13'
14+
name: 'macOS 15'
1515
# Use latest image, but hardcode version to avoid silent upgrades (and breaks).
1616
# See: https://github.com/actions/runner-images#available-images.
17-
runs-on: macos-13 # Use M1 once available https://github.com/github/roadmap/issues/528
17+
runs-on: macos-15 # Use M1 once available https://github.com/github/roadmap/issues/528
1818
steps:
1919
- name: Checkout
2020
uses: actions/checkout@v4
2121
- name: Clang version
2222
run: clang --version
2323
- name: cmake version
2424
run: cmake -version
25+
- name: Checkout submodules
26+
run: git submodule update --init --recursive
2527
- name: install docker
2628
run: |
2729
brew install colima docker
@@ -32,7 +34,7 @@ jobs:
3234

3335
- name: Install Homebrew packages
3436
run: |
35-
brew install cmake boost spdlog nlohmann-json llvm curl
37+
brew install cmake boost spdlog nlohmann-json llvm curl ninja
3638
ln -s "$(brew --prefix llvm)/bin/clang-format" "/usr/local/bin/clang-format"
3739
ln -s "$(brew --prefix llvm)/bin/clang-tidy" "/usr/local/bin/clang-tidy"
3840
ln -s "$(brew --prefix llvm)/bin/clang-apply-replacements" "/usr/local/bin/clang-apply-replacements"
@@ -46,13 +48,13 @@ jobs:
4648
name: "Ubuntu"
4749
runs-on: ubuntu-latest
4850
steps:
49-
- uses: actions/checkout@v2
51+
- uses: actions/checkout@v4
5052
- name: Checkout submodules
5153
run: git submodule update --init --recursive
5254
- name: Create build directory and run CMake
5355
run: |
5456
sudo apt-get -y update
55-
sudo apt-get install build-essential cmake g++-10 gcc-10 libgtest-dev make libssl-dev python3-dev autotools-dev libicu-dev libbz2-dev libboost-all-dev libspdlog-dev nlohmann-json3-dev llvm curl libcurl4-openssl-dev
57+
sudo apt-get install -y build-essential cmake g++-10 gcc-10 libgtest-dev make libssl-dev python3-dev autotools-dev libicu-dev libbz2-dev libboost-all-dev libspdlog-dev nlohmann-json3-dev llvm curl libcurl4-openssl-dev ninja-build
5658
ls
5759
g++ --version
5860
CMAKE_POLICY_VERSION_MINIMUM=3.5 cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja -S . -B build

include/create_container_cmd.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define INCLUDE_CREATE_CONTAINER_CMD_HPP
33

44
#include <memory>
5+
#include <map>
56
#include <nlohmann/json.hpp>
67

78
#include "abstr_sync_docker_cmd_exec.hh"
@@ -15,6 +16,7 @@ struct CreateContainerRequest {
1516
std::string hostName;
1617
std::string name;
1718
std::vector<std::string> cmd;
19+
std::map<std::string, std::string> labels;
1820
};
1921

2022
struct HostConfig {
@@ -33,6 +35,10 @@ class CreateContainerCmd
3335

3436
CreateContainerCmd& withCmd(const std::vector<std::string>& cmd);
3537

38+
CreateContainerCmd& withLabels(const std::map<std::string, std::string>& labels);
39+
40+
CreateContainerCmd& withLabel(const std::string& key, const std::string& value);
41+
3642
CreateContainerRequest getRequest() { return request; }
3743

3844
~CreateContainerCmd() override = default;

include/docker_client.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DOCKER_CLIENT_H
33

44
#include "create_container_cmd.hh"
5+
#include "events_cmd.hh"
56
#include "info_cmd.hh"
67
#include "ping_cmd.hh"
78
#include "pull_image_cmd.hh"
@@ -43,6 +44,7 @@ class DockerClient {
4344

4445
std::shared_ptr<command::RemoveContainerCmd> removeContainerCmd(std::string id);
4546

47+
std::shared_ptr<command::EventsCmd> eventsCmd();
4648
};
4749

4850
}; // namespace dockercpp

include/docker_exception.hh

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
11
#ifndef DOCKER_EXCEPTION_H
22
#define DOCKER_EXCEPTION_H
33

4-
class DockerException {
4+
#include <exception>
5+
#include <string>
56

7+
namespace dockercpp {
8+
9+
class DockerException : public std::runtime_error {
10+
public:
11+
DockerException(const std::string& message)
12+
: std::runtime_error(message), statusCode_(0) {}
13+
14+
DockerException(const std::string& message, int statusCode)
15+
: std::runtime_error(message), statusCode_(statusCode) {}
16+
17+
DockerException(const std::string& message, int statusCode, const std::string& responseBody)
18+
: std::runtime_error(message),
19+
statusCode_(statusCode),
20+
responseBody_(responseBody) {}
21+
22+
int getStatusCode() const { return statusCode_; }
23+
const std::string& getResponseBody() const { return responseBody_; }
24+
25+
private:
26+
int statusCode_;
27+
std::string responseBody_;
28+
};
29+
30+
// Specific Docker exceptions
31+
class DockerClientException : public DockerException {
32+
public:
33+
explicit DockerClientException(const std::string& message)
34+
: DockerException(message) {}
35+
};
36+
37+
class NotModifiedException : public DockerException {
38+
public:
39+
NotModifiedException(const std::string& message)
40+
: DockerException(message, 304) {}
641
};
742

43+
class NotFoundException : public DockerException {
44+
public:
45+
NotFoundException(const std::string& message)
46+
: DockerException(message, 404) {}
47+
};
48+
49+
class ConflictException : public DockerException {
50+
public:
51+
ConflictException(const std::string& message)
52+
: DockerException(message, 409) {}
53+
};
54+
55+
class InternalServerErrorException : public DockerException {
56+
public:
57+
InternalServerErrorException(const std::string& message)
58+
: DockerException(message, 500) {}
59+
};
60+
61+
} // namespace dockercpp
62+
863
#endif

include/remove_container_cmd.hh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class RemoveContainerCmd
1414
public std::enable_shared_from_this<RemoveContainerCmd> {
1515
public:
1616
virtual void withContainerId(std::string id) = 0;
17-
1817
virtual std::string getContainerId() = 0;
18+
virtual bool hasForceEnabled() const = 0;
19+
virtual RemoveContainerCmd& withForce(bool force) = 0;
1920

2021
~RemoveContainerCmd() {}
2122

@@ -43,11 +44,18 @@ class RemoveContainerCmdImpl
4344
void close() override {}
4445

4546
void withContainerId(std::string id) override;
46-
4747
std::string getContainerId() override;
48+
bool hasForceEnabled() const override { return m_force; }
49+
RemoveContainerCmd& withForce(bool force) override {
50+
m_force = force;
51+
return *this;
52+
}
4853

4954
~RemoveContainerCmdImpl() {}
50-
};
51-
} // namespace dockercpp::command
55+
56+
private:
57+
bool m_force{false};
58+
}; // namespace dockercpp::command
59+
}
5260

5361
#endif /* INCLUDE_REMOVE_CONTAINER_CMD_HPP */

include/remove_image_cmd.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class RemoveImageCmd : public SynchDockerCmd<std::string>,
1818

1919
virtual bool hasNoPruneEnabled() = 0;
2020

21-
virtual void withForce(bool force) = 0;
21+
virtual RemoveImageCmd& withForce(bool force) = 0;
2222

23-
virtual void withNoPrune(bool force) = 0;
23+
virtual RemoveImageCmd& withNoPrune(bool force) = 0;
2424

2525
~RemoveImageCmd() {}
2626
};
@@ -44,9 +44,9 @@ class RemoveImageCmdImpl : public RemoveImageCmd,
4444

4545
bool hasNoPruneEnabled() override;
4646

47-
void withForce(bool force) override;
47+
RemoveImageCmd& withForce(bool force) override;
4848

49-
void withNoPrune(bool force) override;
49+
RemoveImageCmd& withNoPrune(bool force) override;
5050

5151
std::string exec() override;
5252

include/start_container_cmd.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class StartContainerCmdImpl : public StartContainerCmd,
3535

3636
std::string getContainerId() override;
3737

38-
~StartContainerCmdImpl() {}
38+
~StartContainerCmdImpl() = default;
3939

4040
private:
4141
std::string m_containerId;

include/stop_container_cmd.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class StopContainerCmdImpl : public StopContainerCmd,
3333

3434
std::string getContainerId() override;
3535

36-
~StopContainerCmdImpl() {}
36+
~StopContainerCmdImpl() = default;
3737

3838
private:
3939
std::string m_containerId;

include/synch_docker_cmd.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class AbstrDockerCmd : SynchDockerCmd<RES_T> {
2929
: m_execution(std::move(execution)){};
3030

3131
RES_T exec() override {
32+
return RES_T();
3233
}
3334

34-
~AbstrDockerCmd() {}
35+
~AbstrDockerCmd() = default;
3536

3637
protected:
3738
std::unique_ptr<exec::DockerCmdSyncExec<CMD_T, RES_T>> m_execution;

src/create_container_cmd.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ CreateContainerCmd& CreateContainerCmd::withCmd(
2525
return *this;
2626
}
2727

28+
CreateContainerCmd& CreateContainerCmd::withLabels(
29+
const std::map<std::string, std::string>& labels) {
30+
request.labels = labels;
31+
return *this;
32+
}
33+
34+
CreateContainerCmd& CreateContainerCmd::withLabel(
35+
const std::string& key, const std::string& value) {
36+
request.labels[key] = value;
37+
return *this;
38+
}
39+
2840
CreateContainerCmdImpl::CreateContainerCmdImpl(
2941
std::unique_ptr<createcontainer::Exec> exec, std::string image)
3042
: AbstrDockerCmd<CreateContainerCmd, model::CreateContainerResponse>(
@@ -47,6 +59,10 @@ void to_json(nlohmann::json& j,
4759
if (!createContainerRequest.cmd.empty()) {
4860
j["Cmd"] = createContainerRequest.cmd;
4961
}
62+
63+
if (!createContainerRequest.labels.empty()) {
64+
j["Labels"] = createContainerRequest.labels;
65+
}
5066
}
5167

5268
} // namespace dockercpp::command

0 commit comments

Comments
 (0)