Skip to content

Commit 7f4f146

Browse files
Ability to specify local config
1 parent fd2fcb1 commit 7f4f146

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

Include/runcpp2/ConfigParsing.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace runcpp2
1212
bool WriteDefaultConfig(const std::string& userConfigPath);
1313

1414
bool ReadUserConfig(std::vector<Data::Profile>& outProfiles,
15-
std::string& outPreferredProfile);
15+
std::string& outPreferredProfile,
16+
const std::string& customConfigPath = "");
1617

1718
bool ParseScriptInfo( const std::string& scriptInfo,
1819
Data::ScriptInfo& outScriptInfo);

Include/runcpp2/runcpp2.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace runcpp2
2424
BUILD,
2525
VERSION,
2626
LOG_LEVEL,
27+
CONFIG_FILE,
2728
COUNT
2829
};
2930

Src/runcpp2/ConfigParsing.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,37 @@ bool runcpp2::WriteDefaultConfig(const std::string& userConfigPath)
177177

178178

179179
bool runcpp2::ReadUserConfig( std::vector<Data::Profile>& outProfiles,
180-
std::string& outPreferredProfile)
180+
std::string& outPreferredProfile,
181+
const std::string& customConfigPath)
181182
{
182183
INTERNAL_RUNCPP2_SAFE_START();
183184

184185
ssLOG_FUNC_DEBUG();
185186

186-
std::string configPath = GetConfigFilePath();
187+
std::string configPath = !customConfigPath.empty() ? customConfigPath : GetConfigFilePath();
187188
if(configPath.empty())
188189
return false;
189190

190-
if(!ghc::filesystem::exists(configPath))
191+
std::error_code e;
192+
if(!ghc::filesystem::exists(configPath, e))
191193
{
194+
if(!customConfigPath.empty())
195+
{
196+
ssLOG_ERROR("Config file doesn't exist: " << configPath);
197+
return false;
198+
}
199+
192200
ssLOG_INFO("Config file doesn't exist. Creating one at: " << configPath);
193201
if(!WriteDefaultConfig(configPath))
194202
return false;
195203
}
196204

205+
if(ghc::filesystem::is_directory(configPath, e))
206+
{
207+
ssLOG_ERROR("Config file path is a directory: " << configPath);
208+
return false;
209+
}
210+
197211
//Read compiler profiles
198212
std::string userConfigContent;
199213
{

Src/runcpp2/main.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ int main(int argc, char* argv[])
175175
int currentArgIndex = 0;
176176
std::unordered_map<runcpp2::CmdOptions, std::string> currentOptions;
177177
{
178-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 13, "Update this");
178+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 14, "Update this");
179179
std::unordered_map<std::string, runcpp2::OptionInfo> longOptionsMap =
180180
{
181181
{
@@ -225,10 +225,14 @@ int main(int argc, char* argv[])
225225
{
226226
"--log-level",
227227
runcpp2::OptionInfo(runcpp2::CmdOptions::LOG_LEVEL, true)
228+
},
229+
{
230+
"--config",
231+
runcpp2::OptionInfo(runcpp2::CmdOptions::CONFIG_FILE, true)
228232
}
229233
};
230234

231-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 13, "Update this");
235+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 14, "Update this");
232236
std::unordered_map<std::string, const runcpp2::OptionInfo&> shortOptionsMap =
233237
{
234238
{"-rc", longOptionsMap.at("--reset-cache")},
@@ -241,7 +245,8 @@ int main(int argc, char* argv[])
241245
{"-t", longOptionsMap.at("--create-script-template")},
242246
{"-w", longOptionsMap.at("--watch")},
243247
{"-b", longOptionsMap.at("--build")},
244-
{"-v", longOptionsMap.at("--version")}
248+
{"-v", longOptionsMap.at("--version")},
249+
{"-c", longOptionsMap.at("--config")}
245250
};
246251

247252
currentArgIndex = ParseArgs(longOptionsMap, shortOptionsMap, currentOptions, argc, argv);
@@ -258,7 +263,7 @@ int main(int argc, char* argv[])
258263
//Help message
259264
if(currentOptions.count(runcpp2::CmdOptions::HELP))
260265
{
261-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 13, "Update this");
266+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 14, "Update this");
262267
ssLOG_BASE("Usage: runcpp2 [options] [input_file]");
263268
ssLOG_BASE("Options:");
264269
ssLOG_BASE(" -rc, --[r]eset-[c]ache Deletes compiled source files cache only");
@@ -272,6 +277,7 @@ int main(int argc, char* argv[])
272277
ssLOG_BASE(" -w, --[w]atch Watch script changes and output any compiling errors");
273278
ssLOG_BASE(" -b, --[b]uild Build the script and copy output files to the working directory");
274279
ssLOG_BASE(" -v, --[v]ersion Show the version of runcpp2");
280+
ssLOG_BASE(" -c, --[c]onfig <file> Use specified config file instead of default");
275281
ssLOG_BASE(" --log-level <level> Sets the log level (Normal, Info, Debug) for runcpp2.");
276282

277283
return 0;
@@ -340,12 +346,16 @@ int main(int argc, char* argv[])
340346
std::vector<runcpp2::Data::Profile> profiles;
341347
std::string preferredProfile;
342348

343-
if(!runcpp2::ReadUserConfig(profiles, preferredProfile))
349+
std::string configPath;
350+
if(currentOptions.count(runcpp2::CmdOptions::CONFIG_FILE))
351+
configPath = currentOptions.at(runcpp2::CmdOptions::CONFIG_FILE);
352+
353+
if(!runcpp2::ReadUserConfig(profiles, preferredProfile, configPath))
344354
{
345355
ssLOG_ERROR("Failed read user config");
346356
return -1;
347357
}
348-
358+
349359
ssLOG_DEBUG("\nprofiles:");
350360
for(int i = 0; i < profiles.size(); ++i)
351361
ssLOG_DEBUG("\n" << profiles.at(i).ToString(" "));

0 commit comments

Comments
 (0)