Skip to content

Commit 8b5006d

Browse files
authored
Merge pull request #2 from coding-cpp/fix/1
add isDocker function
2 parents a021ab8 + 488c771 commit 8b5006d

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

docker/build.dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM alpine:3.20.2 AS builder
2+
3+
RUN apk update && apk add --no-cache g++ make cmake
4+
5+
WORKDIR /app
6+
7+
COPY example /app/example
8+
COPY include /app/include
9+
COPY lib /app/lib
10+
COPY src /app/src
11+
COPY CMakeLists.txt /app/CMakeLists.txt
12+
13+
RUN mkdir build && cd build && cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_EXE_LINKER_FLAGS="-static" .. && make
14+
RUN chmod +x /app/build/brewtils
15+
16+
FROM scratch
17+
18+
WORKDIR /app
19+
20+
COPY --from=builder /app/build/brewtils /app/brewtils
21+
22+
ENTRYPOINT ["/app/brewtils"]

example/main.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ int main(int argc, char **argv) {
5656
logger::info(file);
5757
}
5858

59+
logger::info("---------------------------");
60+
logger::info(brewtils::os::isDocker() ? "Running in Docker"
61+
: "Not in Docker");
62+
5963
logger::info("---------------------------");
6064
logger::info(brewtils::os::file::getMimeType("test.txt"));
6165

include/brewtils/os.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ std::string joinPath(const std::string &path1,
1212

1313
std::string basePath(const std::string &path) noexcept(true);
1414

15+
bool isDocker() noexcept(true);
16+
1517
} // namespace os
1618

1719
} // namespace brewtils

include/brewtils/os/file.h

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <filesystem>
44
#include <fstream>
55
#include <map>
6-
#include <set>
7-
#include <string>
86

97
#include <logger/log.h>
108

src/os.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ std::string brewtils::os::basePath(const std::string &path) noexcept(true) {
1818
size_t pos = base.find_last_of('/');
1919
return base.substr(pos + 1, base.size());
2020
}
21+
22+
bool brewtils::os::isDocker() noexcept(true) {
23+
return brewtils::os::file::exists("/.dockerenv");
24+
}

src/os/dir.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ brewtils::os::dir::tree(const std::string &path) noexcept(false) {
5757
std::set<std::string> files;
5858
for (const std::filesystem::directory_entry &entry :
5959
std::filesystem::recursive_directory_iterator(path)) {
60-
relativePath = std::filesystem::relative(entry.path(), path).string();
61-
files.insert(relativePath);
60+
try {
61+
if (!entry.is_regular_file() && !entry.is_directory()) {
62+
continue;
63+
}
64+
65+
relativePath = std::filesystem::relative(entry.path(), path).string();
66+
files.insert(relativePath);
67+
} catch (const std::filesystem::filesystem_error &e) {
68+
continue;
69+
}
6270
}
6371

6472
return files;

0 commit comments

Comments
 (0)