Skip to content

Commit 6dcda2d

Browse files
Add sonarqube support
and: - fix and error in checks definio. Use CheckID instead of ID - remove unneded lib: gRPC::grpc_cronet - remove obsolete vcpkg port definition
1 parent 9403912 commit 6dcda2d

28 files changed

Lines changed: 398 additions & 2666 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Sonar
2+
.scannerwork
3+
14
# Prerequisites
25
*.d
36

.vscode/launch.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(gdb) Launch",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/build/bin/leader",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"setupCommands": [
19+
{
20+
"description": "Enable pretty-printing for gdb",
21+
"text": "-enable-pretty-printing",
22+
"ignoreFailures": true
23+
}
24+
]
25+
},
26+
{
27+
"name": "(Windows) Launch",
28+
"type": "cppvsdbg",
29+
"request": "launch",
30+
"program": "${workspaceFolder}/build/bin/leader.exe",
31+
"args": [],
32+
"stopAtEntry": false,
33+
"cwd": "${workspaceFolder}",
34+
"environment": [],
35+
"externalConsole": false
36+
}
37+
]
38+
}

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
"xtr1common": "cpp",
8888
"xtree": "cpp",
8989
"xutility": "cpp",
90-
"resumable": "cpp"
90+
"resumable": "cpp",
91+
"compare": "cpp",
92+
"concepts": "cpp",
93+
"coroutine": "cpp",
94+
"fstream": "cpp",
95+
"random": "cpp",
96+
"pointers": "cpp",
97+
"*.ipp": "cpp"
9198
}
9299
}

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Use cmake to build it on Windows, macOS and Linux.
5757
5858
### Dependencies using VCPKG
5959
60-
```
60+
```shell
6161
./vcpkg install boost
6262
./vcpkg install fmt
6363
./vcpkg install uriparser
@@ -78,3 +78,15 @@ target_link_libraries(main PRIVATE consulcpp)
7878

7979
- [oatpp-consul](https://github.com/oatpp/oatpp-consul): C++ Consul integration for oatpp applications
8080
- [Ppconsul](https://github.com/oliora/ppconsul): C++ client for Consul
81+
82+
## Quality Checks
83+
84+
### Sonarqube
85+
86+
1. Install and add to the path both build-wrapper and sonar-scanner
87+
2. Set the SONAR_TOKEN environment variable
88+
3. Use the sonarqube target:
89+
90+
```shell
91+
cmake --build build --target sonarqube
92+
```

cmake/Tools.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ function(target_clang_format_setup target)
5050
clang_format_setup(${target_sources})
5151
endfunction()
5252

53+
function(sonarqube_setup)
54+
if( WIN32 )
55+
find_program( buildwrapper_tmp build-wrapper-win-x86-64.exe )
56+
elseif( APPLE )
57+
find_program( buildwrapper_tmp build-wrapper-macosx-x86 )
58+
else()
59+
find_program( buildwrapper_tmp build-wrapper-linux-x86-64 )
60+
endif()
61+
if( buildwrapper_tmp )
62+
set( BUILDWRAPPER_EXECUTABLE ${buildwrapper_tmp})
63+
unset( buildwrapper_tmp )
64+
65+
add_custom_target( sonarqube-build
66+
COMMAND
67+
${CMAKE_MAKE_PROGRAM} clean
68+
COMMAND
69+
${BUILDWRAPPER_EXECUTABLE}
70+
--out-dir bw-output
71+
${CMAKE_MAKE_PROGRAM}
72+
COMMENT
73+
"Run sonarqube build"
74+
WORKING_DIRECTORY
75+
${CMAKE_BINARY_DIR}
76+
)
77+
78+
add_custom_target( sonarqube
79+
COMMAND
80+
sonar-scanner
81+
COMMENT
82+
"Run sonarqube analysis"
83+
WORKING_DIRECTORY
84+
${CMAKE_SOURCE_DIR}
85+
)
86+
87+
add_dependencies( sonarqube sonarqube-build )
88+
endif()
89+
endfunction()
5390
###
5491

5592
# Enable clang-tidy either setting the app or using the option

examples/leader/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif()
3333
target_link_libraries( ${PROJECT_NAME}
3434
consulcpp
3535
protobuf::libprotobuf
36-
gRPC::gpr gRPC::grpc gRPC::grpc++ gRPC::grpc_cronet
36+
gRPC::gpr gRPC::grpc gRPC::grpc++
3737
)
3838

3939
if( UNIX )
@@ -51,6 +51,7 @@ target_include_directories( ${PROJECT_NAME}
5151
)
5252

5353
clang_format_setup( ${SOURCES} )
54+
sonarqube_setup()
5455

5556
include(GNUInstallDirs)
5657

examples/leader/main.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <csignal>
88
#include <chrono>
99
#include <thread>
10+
#include <optional>
1011

1112
#include "../common/helpers.h"
1213

@@ -28,9 +29,7 @@ class HealthServiceImpl final : public grpc::health::v1::Health::Service
2829
}
2930
};
3031

31-
static std::unique_ptr<grpc::Server> gServer;
32-
33-
void RunServer( const consulcpp::Service & service )
32+
void TestService( consulcpp::Service & service )
3433
{
3534
std::string serverAddress = fmt::format( "{}:{}", service.mAddress, service.mPort );
3635
HealthServiceImpl healthService;
@@ -39,21 +38,30 @@ void RunServer( const consulcpp::Service & service )
3938
builder.AddListeningPort( serverAddress, grpc::InsecureServerCredentials() );
4039
builder.RegisterService( &healthService );
4140

42-
gServer = builder.BuildAndStart();
43-
if( gServer ) {
44-
spdlog::info( "Server listening on {}", serverAddress );
45-
gServer->Wait();
41+
if( std::unique_ptr<grpc::Server> server = builder.BuildAndStart(); server ){
42+
std::thread grpcThread( &grpc::Server::Wait, server.get() );
43+
spdlog::info( "Server listening on {}:{}", service.mAddress, service.mPort );
44+
loop();
45+
server->Shutdown();
46+
grpcThread.join();
4647
spdlog::info( "Server stopped" );
4748
} else {
48-
spdlog::critical( "Cannot start gRPC server on {}", serverAddress );
49+
spdlog::critical( "Cannot start gRPC server on {}:{}", service.mAddress, service.mPort );
4950
}
5051
}
51-
5252
int main( int argc, char * argv[] )
5353
{
54-
consulcpp::Consul consul;
55-
56-
if( consul.connect() ) {
54+
unsigned short port = 50051;
55+
56+
if( argc > 1 ) {
57+
if( auto maybePort = consulcpp::utils::asPort( argv[1] ); maybePort ){
58+
port = maybePort.value();
59+
}else{
60+
spdlog::error( "This is not a valid port number {}", argv[1] );
61+
return 1;
62+
}
63+
}
64+
if( consulcpp::Consul consul; consul.connect() ) {
5765
spdlog::info( "Agent address {}", consul.address() );
5866

5967
consulcpp::Service service;
@@ -62,20 +70,18 @@ int main( int argc, char * argv[] )
6270
service.mId = fmt::format( "{}", std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ).count() );
6371
service.mName = "leader-demo";
6472
service.mAddress = consul.address();
65-
if( argc > 1 ) {
66-
service.mPort = std::stoi( argv[ 1 ] );
67-
} else {
68-
service.mPort = 50051;
69-
}
73+
service.mPort = port;
74+
75+
check.mId = "presence";
7076
check.mInterval = "5s";
7177
check.mDeregisterCriticalServiceAfter = "1m";
7278
check.mGRPC = fmt::format( "{}:{}/Health", service.mAddress, service.mPort );
7379
service.mChecks = { check };
7480

7581
// Purge death services
7682
std::vector<consulcpp::Service> otherServices = consul.services().findInCatalog( service.mName, service.mTags );
77-
for( auto service: otherServices ) {
78-
consul.services().destroy( service );
83+
for( const auto & otherService: otherServices ) {
84+
consul.services().destroy( otherService );
7985
}
8086

8187
consul.services().create( service );
@@ -84,21 +90,15 @@ int main( int argc, char * argv[] )
8490

8591
consul.kv().set( "my-key", "my-value" );
8692

87-
consulcpp::Leader::Status leader = consul.leader().acquire( service, session );
88-
if( leader == consulcpp::Leader::Status::Yes ) {
93+
if( consulcpp::Leader::Status leader = consul.leader().acquire( service, session ); leader == consulcpp::Leader::Status::Yes ) {
8994
spdlog::info( "I'm the leader" );
9095
} else {
9196
spdlog::info( "I'm a follower" );
9297
}
93-
std::thread grpcThread( RunServer, service );
94-
loop();
95-
if( gServer ) {
96-
gServer->Shutdown();
97-
}
98-
grpcThread.join();
9998

100-
auto val = consul.kv().get( "my-key" );
101-
if( val ) {
99+
TestService( service );
100+
101+
if( auto val = consul.kv().get( "my-key" ); val ) {
102102
spdlog::info( "Key value was {}", val.value() );
103103
if( consul.kv().destroy( "my-key" ) ) {
104104
spdlog::info( "Key deleted" );

include/consulcpp/Consul.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ class ConsulCPP_API Consul
2222
//! Creates a consul services object pointing to a local agent at http://127.0.0.1:8500
2323
Consul();
2424
//! Creates a consul services object pointing to a local agent at "agentAddress"
25-
Consul( const std::string & agentAddress );
25+
explicit Consul( std::string_view agentAddress );
2626
~Consul();
2727

2828
/*! Connects to the local agent
2929
*/
30-
bool connect();
30+
[[nodiscard]] bool connect();
3131

3232
//! Address where consul agent is running
33-
std::string address() const;
33+
[[nodiscard]] std::string address() const;
3434
//! Agent end point address, including the schema and port
35-
std::string agentAddress() const;
35+
[[nodiscard]] std::string agentAddress() const;
3636
//! Consul API Version
37-
std::string agentAPIVersion() const;
37+
[[nodiscard]] std::string agentAPIVersion() const;
3838

3939
// Services access
40-
Services & services() const;
40+
[[nodiscard]] Services & services() const;
4141
// Sessions access
42-
Sessions & sessions() const;
42+
[[nodiscard]] Sessions & sessions() const;
4343
// Leader election access
44-
Leader & leader() const;
44+
[[nodiscard]] Leader & leader() const;
4545
// KV Storer access
46-
KV & kv() const;
46+
[[nodiscard]] KV & kv() const;
4747

4848
private:
4949
struct Private;

include/consulcpp/ConsulCpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
#include <consulcpp/Sessions.h>
77
#include <consulcpp/Leader.h>
88
#include <consulcpp/KV.h>
9+
#include <consulcpp/Utils.h>

include/consulcpp/Export.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,3 @@
1919
#define ConsulCPP_API
2020
#endif
2121

22-
// Temporary hack for Ubuntu 16 default gcc compiler
23-
// https://codereview.stackexchange.com/a/136373
24-
#if __has_include(<optional>)
25-
#include <optional>
26-
namespace stdx {
27-
using namespace ::std;
28-
}
29-
#elif __has_include(<experimental/optional>)
30-
#include <experimental/optional>
31-
namespace stdx {
32-
using namespace ::std;
33-
using namespace ::std::experimental;
34-
}
35-
#endif

0 commit comments

Comments
 (0)