Skip to content

Commit caab357

Browse files
committed
create "createSettingsFile" function for when user dont have settings.json file
1 parent 7d2b3ca commit caab357

3 files changed

Lines changed: 177 additions & 3 deletions

File tree

src/library/settings/Settings.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,168 @@ bool Settings::getSetting()
6464

6565
return true;
6666
}
67+
68+
bool Settings::createSettingsFile()
69+
{
70+
json settings;
71+
72+
// Temporary variables to hold user input
73+
std::string botToken, appVersion = "1.2.1", serverName;
74+
int64_t chatId;
75+
int cpuCheckDuration, memoryCheckDuration, cpuLimit, memoryLimit;
76+
bool defaultMonitoringStatus;
77+
78+
// Set the app version directly
79+
settings["version"] = appVersion; // Use consistent key name here
80+
81+
// Bot token
82+
while (true)
83+
{
84+
std::cout << "Enter bot token: ";
85+
std::cin >> botToken;
86+
if (!botToken.empty())
87+
{
88+
settings["bot_token"] = botToken;
89+
break;
90+
}
91+
else
92+
{
93+
std::cout << "Bot token cannot be empty. Please enter a valid bot token.\n";
94+
}
95+
}
96+
97+
// Chat ID
98+
while (true)
99+
{
100+
std::cout << "Enter chat ID: ";
101+
if (std::cin >> chatId)
102+
{
103+
settings["chat_id"] = chatId;
104+
break;
105+
}
106+
else
107+
{
108+
std::cout << "Invalid input. Please enter a valid chat ID.\n";
109+
std::cin.clear();
110+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
111+
}
112+
}
113+
114+
// Server name
115+
while (true)
116+
{
117+
std::cout << "Enter server name: ";
118+
std::cin >> serverName;
119+
if (!serverName.empty())
120+
{
121+
settings["server_name"] = serverName;
122+
break;
123+
}
124+
else
125+
{
126+
std::cout << "Server name cannot be empty. Please enter a valid server name.\n";
127+
}
128+
}
129+
130+
// CPU check duration
131+
while (true)
132+
{
133+
std::cout << "Enter CPU check duration (ms): ";
134+
if (std::cin >> cpuCheckDuration)
135+
{
136+
settings["cpu_check_duration"] = cpuCheckDuration;
137+
break;
138+
}
139+
else
140+
{
141+
std::cout << "Invalid input. Please enter a valid duration in milliseconds.\n";
142+
std::cin.clear();
143+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
144+
}
145+
}
146+
147+
// Memory check duration
148+
while (true)
149+
{
150+
std::cout << "Enter memory check duration (ms): ";
151+
if (std::cin >> memoryCheckDuration)
152+
{
153+
settings["memory_check_duration"] = memoryCheckDuration;
154+
break;
155+
}
156+
else
157+
{
158+
std::cout << "Invalid input. Please enter a valid duration in milliseconds.\n";
159+
std::cin.clear();
160+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
161+
}
162+
}
163+
164+
// CPU usage limit
165+
while (true)
166+
{
167+
std::cout << "Enter CPU usage limit (%): ";
168+
if (std::cin >> cpuLimit)
169+
{
170+
settings["cpu_limit"] = cpuLimit;
171+
break;
172+
}
173+
else
174+
{
175+
std::cout << "Invalid input. Please enter a valid percentage.\n";
176+
std::cin.clear();
177+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
178+
}
179+
}
180+
181+
// Memory usage limit
182+
while (true)
183+
{
184+
std::cout << "Enter memory usage limit (%): ";
185+
if (std::cin >> memoryLimit)
186+
{
187+
settings["memory_limit"] = memoryLimit;
188+
break;
189+
}
190+
else
191+
{
192+
std::cout << "Invalid input. Please enter a valid percentage.\n";
193+
std::cin.clear();
194+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
195+
}
196+
}
197+
198+
// Default monitoring status
199+
while (true)
200+
{
201+
std::cout << "Enable default monitoring status? (1 for true, 0 for false): ";
202+
int monitoringInput;
203+
if (std::cin >> monitoringInput && (monitoringInput == 0 || monitoringInput == 1))
204+
{
205+
defaultMonitoringStatus = static_cast<bool>(monitoringInput);
206+
settings["default_monitoring_status"] = defaultMonitoringStatus;
207+
break;
208+
}
209+
else
210+
{
211+
std::cout << "Invalid input. Please enter 1 for true or 0 for false.\n";
212+
std::cin.clear();
213+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
214+
}
215+
}
216+
217+
// Write the JSON object to a file
218+
std::ofstream settings_file("settings.json");
219+
if (settings_file)
220+
{
221+
settings_file << settings.dump(4); // Pretty-print with indentation
222+
settings_file.close();
223+
std::cout << "Settings file created successfully.\n";
224+
return true;
225+
}
226+
else
227+
{
228+
std::cerr << "Error creating settings.json file.\n";
229+
return false;
230+
}
231+
}

src/library/settings/Settings.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Settings
1414

1515
// Load settings from the JSON file
1616
bool getSetting();
17+
bool createSettingsFile();
1718

1819
// Getter functions to access private member variables
1920
std::string getBotToken() const { return botToken; }

src/main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,24 @@ int app()
3131
// Get settings from setting file
3232
if (!settings.getSetting())
3333
{
34-
logger.logToConsole("Failed to set settings!");
35-
return 1;
34+
logger.logToConsole("Failed to load setting file . building setting...");
35+
36+
// build setting file
37+
if (!settings.createSettingsFile())
38+
{
39+
logger.logToConsole("Failed to create setting(settings.json) file");
40+
return 1;
41+
}
42+
43+
settings.getSetting();
3644
}
3745

3846
// set monitoring default status
3947
isMonitoringEnable = settings.getDefaultMonitoringStatus();
4048

4149
logger.logToConsole("Linux Monitoring v" + settings.getAppVersion() + " Service Started");
4250
logger.logToConsole(settings.getServerName());
43-
51+
4452
// Monitoring Objects
4553
CpuMonitor cpu(settings.getCpuCheckDuration());
4654
MemoryMonitor memory(settings.getMemoryCheckDuration());

0 commit comments

Comments
 (0)