diff --git a/include/docker.hpp b/include/docker.hpp new file mode 100644 index 0000000..bb0d1c0 --- /dev/null +++ b/include/docker.hpp @@ -0,0 +1,38 @@ +#ifndef DOCKERXX_DOCKER_HPP +#define DOCKERXX_DOCKER_HPP + +#include +#include + + +namespace dockerxx { + + class Docker { + + /* Member Variables */ + private: + CURL* m_curl = nullptr; + bool m_remote_connection; + std::string m_uri; + + /* Constructors */ + public: + Docker(); + explicit Docker(std::string& uri); + + /* Member Functions */ + private: + std::string request(const std::string& path); + + public: + std::string ping(); + + /* Static Functions */ + private: + static size_t write_data(void* ptr, size_t size, size_t nmemb, std::string* userp); + + }; + +} /* namespace dockerxx */ + +#endif /* DOCKERXX_DOCKER_HPP */ \ No newline at end of file diff --git a/include/dockerxx.hpp b/include/dockerxx.hpp deleted file mode 100644 index 99aa069..0000000 --- a/include/dockerxx.hpp +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by fabitheguy on 9/19/24. -// - -#ifndef DOCKERXX_HPP -#define DOCKERXX_HPP - -#endif //DOCKERXX_HPP diff --git a/src/docker.cpp b/src/docker.cpp index 50d2380..820a799 100644 --- a/src/docker.cpp +++ b/src/docker.cpp @@ -1,3 +1,58 @@ -// -// Created by fabitheguy on 9/19/24. -// +#include +#include +#include + +using namespace dockerxx; + +/* Constructors */ +Docker::Docker() { this->m_remote_connection = false; } + +Docker::Docker(std::string &uri) { + this->m_remote_connection = true; + this->m_uri = std::move(uri); +} + +/* Member functions */ +std::string Docker::request(const std::string &path) { + CURLcode response_code; + std::string response_data; + + if (path.empty()) { + throw std::invalid_argument("Path is empty"); + } + + this->m_curl = curl_easy_init(); + + if (this->m_remote_connection) { + curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string(this->m_uri + path).c_str()); + } else { + curl_easy_setopt(this->m_curl, CURLOPT_URL, std::string("http://localhost" + path).c_str()); + curl_easy_setopt(this->m_curl, CURLOPT_UNIX_SOCKET_PATH, + std::string("/var/run/docker.sock").c_str()); + } + + curl_easy_setopt(this->m_curl, CURLOPT_WRITEFUNCTION, Docker::write_data); + curl_easy_setopt(this->m_curl, CURLOPT_WRITEDATA, &response_data); + + response_code = curl_easy_perform(this->m_curl); + + if (response_code != CURLE_OK) { + throw std::runtime_error("curl_easy_perform() failed: " + + std::string(curl_easy_strerror(response_code))); + } + + curl_easy_cleanup(this->m_curl); + + return response_data; +} + +std::string Docker::ping() { return request("/_ping"); } + +/* Static functions */ +size_t Docker::write_data(void *ptr, size_t size, size_t nmemb, std::string *userp) { + size_t real_size = size * nmemb; + + userp->append(static_cast(ptr), real_size); + + return real_size; +} \ No newline at end of file diff --git a/test/main.cpp b/test/main.cpp index 04ce544..558b442 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,5 +1,12 @@ +#include #include +TEST(DockerConnection, PingDaemon) { + dockerxx::Docker docker; + + ASSERT_EQ(docker.ping(), "OK"); +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);