Add startup config loading
This commit is contained in:
parent
c4d8162983
commit
fe1961947c
2 changed files with 68 additions and 16 deletions
|
|
@ -6,8 +6,6 @@
|
|||
|
||||
public bool Interactive { get; set; }
|
||||
|
||||
public bool Version { get; set; }
|
||||
|
||||
public IEnumerable<string> Expression { get; set; } = Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,14 @@ namespace cs_mic_cli
|
|||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
var verboseOption = new Option<bool>("--verbose", "-v")
|
||||
if (args.Length == 1 && args[0] == "--version")
|
||||
{
|
||||
PrintVersion(Console.Out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
var verboseOption = new Option<bool>("--verbose")
|
||||
{
|
||||
Description = "Displays error messages from the parser if a soft error occurs."
|
||||
};
|
||||
|
|
@ -18,10 +25,6 @@ namespace cs_mic_cli
|
|||
{
|
||||
Description = "Starts an interactive session."
|
||||
};
|
||||
var versionOption = new Option<bool>("--version", "-v")
|
||||
{
|
||||
Description = "Prints the version number of CS-MIC."
|
||||
};
|
||||
var expressionArgument = new Argument<string[]>("expression")
|
||||
{
|
||||
Arity = ArgumentArity.ZeroOrMore,
|
||||
|
|
@ -50,7 +53,6 @@ namespace cs_mic_cli
|
|||
{
|
||||
Verbose = parseResult.GetValue(verboseOption),
|
||||
Interactive = parseResult.GetValue(interactiveOption),
|
||||
Version = parseResult.GetValue(versionOption),
|
||||
Expression = parseResult.GetValue(expressionArgument) ?? Array.Empty<string>()
|
||||
};
|
||||
|
||||
|
|
@ -74,14 +76,7 @@ namespace cs_mic_cli
|
|||
CSMic.Interpreter.Errors.errorStream = TextWriter.Null;
|
||||
}
|
||||
|
||||
if(options.Version)
|
||||
{
|
||||
Console.WriteLine(Functions.Version.VersionCLI);
|
||||
Console.WriteLine(Functions.Version.VersionCSMIC);
|
||||
Console.WriteLine(Functions.Version.VersionStandardLibrary);
|
||||
|
||||
return 0;
|
||||
}
|
||||
LoadUserConfig(inputInterpreter, options);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
|
||||
{
|
||||
|
|
@ -120,6 +115,65 @@ namespace cs_mic_cli
|
|||
return 0;
|
||||
}
|
||||
|
||||
private static void LoadUserConfig(InputInterpreter inputInterpreter, Options options)
|
||||
{
|
||||
var configPath = GetUserConfigPath();
|
||||
var configDirectory = Path.GetDirectoryName(configPath);
|
||||
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(configDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(configDirectory);
|
||||
}
|
||||
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
File.Create(configPath).Dispose();
|
||||
}
|
||||
|
||||
foreach (var line in File.ReadLines(configPath))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
inputInterpreter.Interpret(line);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine("Failed to load config: {0}", configPath);
|
||||
|
||||
if (options.Verbose)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetUserConfigPath()
|
||||
{
|
||||
var configHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(configHome))
|
||||
{
|
||||
configHome = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
||||
".config");
|
||||
}
|
||||
|
||||
return Path.Combine(configHome, "csmic", "init.csmic");
|
||||
}
|
||||
|
||||
private static void PrintVersion(TextWriter output)
|
||||
{
|
||||
output.WriteLine(Functions.Version.VersionCLI);
|
||||
output.WriteLine(Functions.Version.VersionCSMIC);
|
||||
output.WriteLine(Functions.Version.VersionStandardLibrary);
|
||||
}
|
||||
|
||||
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression, ExitRequestedState? exitRequestedState = null)
|
||||
{
|
||||
inputInterpreter.Interpret(expression);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue