Settings
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Mono.Options;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isn
|
||||
{
|
||||
@ -33,19 +35,30 @@ namespace isn
|
||||
|
||||
pushReports.Add(report);
|
||||
}
|
||||
if (storApiKey)
|
||||
{
|
||||
EnsureKeyStored();
|
||||
}
|
||||
return pushReports;
|
||||
}
|
||||
static OptionSet storeoptions = new OptionSet {
|
||||
{ "s|source=", "use source", val => source = source ?? val },
|
||||
{ "h|help", "show this message and exit", h => shouldShowPushHelp = h != null },
|
||||
};
|
||||
private static string _configFileName =
|
||||
Path.Combine(
|
||||
Path.Combine(Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.UserProfile), ".isn"),
|
||||
"config.json")
|
||||
;
|
||||
|
||||
public class IsnSourceSettings {
|
||||
internal string Source { get; set; }
|
||||
|
||||
internal string[] Keys { get; set; }
|
||||
}
|
||||
public static IEnumerable<IsnSourceSettings> Sources{ get; protected set; }
|
||||
public class IsnSourceSettings
|
||||
{
|
||||
internal string Source { get; set; }
|
||||
|
||||
internal string[] Keys { get; set; }
|
||||
}
|
||||
public static IEnumerable<IsnSourceSettings> Sources { get; protected set; }
|
||||
|
||||
private static void StoreApiKey(IEnumerable<string> storeArgs)
|
||||
{
|
||||
@ -56,19 +69,56 @@ namespace isn
|
||||
Console.Error.WriteLine("Push Options:");
|
||||
storeoptions.WriteOptionDescriptions(Console.Out);
|
||||
}
|
||||
else {
|
||||
foreach (string keyv in storeArgs)
|
||||
{
|
||||
EnsureKeyStored(source,keyv);
|
||||
}
|
||||
else
|
||||
{
|
||||
apiKey = args[0];
|
||||
EnsureKeyStored();
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureKeyStored(string source, string keyv)
|
||||
public static void EnsureKeyStored()
|
||||
{
|
||||
var pkeyv = _protector.Protect(keyv);
|
||||
|
||||
if (source == null) return;
|
||||
|
||||
if (Settings.Sources.ContainsKey(source))
|
||||
{
|
||||
if (apiKey == null)
|
||||
{
|
||||
// Une suppression
|
||||
Settings.Sources.Remove(source);
|
||||
if (Program.Settings.DefaultSource==source) Settings.DefaultSource = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Une mise À jour
|
||||
string ptd = Protector.Protect(apiKey);
|
||||
Settings.Sources[source].ApiKey = ptd;
|
||||
if (Program.Settings.DefaultSource==null) Settings.DefaultSource = source;
|
||||
}
|
||||
}
|
||||
else if (apiKey != null)
|
||||
{
|
||||
// une addition
|
||||
string ptd = Protector.Protect(apiKey);
|
||||
var sUri = new Uri(source);
|
||||
Settings.Sources.Add(source, new SourceSettings { ApiKey = ptd });
|
||||
}
|
||||
else return;
|
||||
|
||||
|
||||
FileInfo cfgSettingIf = new FileInfo(_configFileName);
|
||||
if (!cfgSettingIf.Directory.Exists) cfgSettingIf.Directory.Create();
|
||||
File.WriteAllText(cfgSettingIf.FullName, JsonConvert.SerializeObject(Program.Settings));
|
||||
}
|
||||
|
||||
public static void LoadConfig()
|
||||
{
|
||||
FileInfo cfgSettingIf = new FileInfo(_configFileName);
|
||||
if (cfgSettingIf.Exists)
|
||||
{
|
||||
var json = File.ReadAllText(cfgSettingIf.FullName);
|
||||
settings = JsonConvert.DeserializeObject<Settings>(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -12,8 +12,10 @@ namespace isn
|
||||
static OptionSet options = new OptionSet {
|
||||
{ "h|help", "show this message and exit", h => shouldShowHelp = h != null },
|
||||
};
|
||||
|
||||
static OptionSet pushoptions = new OptionSet {
|
||||
{ "k|api-key=", "use api key", val => apiKey = apiKey ?? val },
|
||||
{ "p|store-api-key", "store used api key (=<true|false>)", val => storApiKey = val != null },
|
||||
{ "s|source=", "use source", val => source = source ?? val },
|
||||
{ "h|help", "show this message and exit", h => shouldShowPushHelp = h != null },
|
||||
};
|
||||
@ -28,20 +30,31 @@ namespace isn
|
||||
private static string apiKey = null;
|
||||
private static string source = null;
|
||||
private static int pushKO = 0;
|
||||
private static bool storApiKey = false;
|
||||
public static IDataProtector Protector { get; set; } = new DefaultDataProtector();
|
||||
|
||||
private static readonly isn.IDataProtector _protector = new DefaultDataProtector();
|
||||
static Settings settings = null;
|
||||
|
||||
static Program()
|
||||
|
||||
public static Settings Settings
|
||||
{
|
||||
get
|
||||
{
|
||||
if (settings == null)
|
||||
LoadConfig();
|
||||
if (settings == null)
|
||||
settings = new Settings
|
||||
{
|
||||
DataProtectionTitle = "isn",
|
||||
Sources = new Dictionary<string, SourceSettings>()
|
||||
};
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
|
||||
|
||||
var commandSet = new CommandSet("isn");
|
||||
|
||||
|
||||
var srclst = new Command("list")
|
||||
{
|
||||
Run = sargs => SourceList(sargs)
|
||||
|
@ -25,8 +25,10 @@ namespace isn
|
||||
{
|
||||
var wrqueryHandler = new UploadFilesToServerUsingWebRequest();
|
||||
// await wrqueryHandler.UploadFilesToServerAsync(report, new Uri(source), fi, apikey);
|
||||
if (source == null) source = Program.Settings.DefaultSource;
|
||||
if (Program.Settings.Sources.ContainsKey(source))
|
||||
if (apikey is null) apikey = Program.Protector.UnProtect(Program.Settings.Sources[source].ApiKey);
|
||||
wrqueryHandler.UploadFilesToServer(report, new Uri(source), fi, apikey);
|
||||
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
|
18
src/isn/Settings.cs
Normal file
18
src/isn/Settings.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace isn
|
||||
{
|
||||
public class SourceSettings
|
||||
{
|
||||
public string ApiKey { get; set; }
|
||||
public string Alias { get; set; }
|
||||
}
|
||||
public class Settings
|
||||
{
|
||||
public string DataProtectionTitle {get; set; }
|
||||
public Dictionary<string, SourceSettings> Sources {get; set; }
|
||||
|
||||
public bool AutoUpdateApiKey { get; set; } = false;
|
||||
public string DefaultSource { get; set; }
|
||||
}
|
||||
}
|
@ -68,15 +68,27 @@ namespace isn
|
||||
}
|
||||
catch (Exception rex)
|
||||
{
|
||||
report.Executed = false;
|
||||
report.StackTrace = rex.StackTrace;
|
||||
report.Message = rex.Message;
|
||||
Console.Error.WriteLine(rex.Message);
|
||||
Console.Error.WriteLine("Stack trace:");
|
||||
Console.Error.WriteLine(rex.StackTrace);
|
||||
WebResponse eresp = httpWebRequest.GetResponse();
|
||||
if (!CheckResponse(eresp, report))
|
||||
throw new Exception("Invalid server response type");
|
||||
|
||||
}
|
||||
}, httpWebRequest);
|
||||
|
||||
WebResponse resp = httpWebRequest.GetResponse();
|
||||
if (!CheckResponse(resp, report)) throw new Exception("Invalid server response type");
|
||||
if (Program.Settings.AutoUpdateApiKey)
|
||||
Program.EnsureKeyStored();
|
||||
|
||||
}
|
||||
|
||||
static bool CheckResponse(WebResponse resp, PushReport report)
|
||||
{
|
||||
Stream stream = resp.GetResponseStream();
|
||||
StreamReader re = new StreamReader(stream);
|
||||
if (resp is HttpWebResponse)
|
||||
@ -87,9 +99,11 @@ namespace isn
|
||||
report.StatusCode = hrep.StatusCode.ToString();
|
||||
report.OK = hrep.StatusCode == HttpStatusCode.Accepted
|
||||
|| hrep.StatusCode == HttpStatusCode.OK;
|
||||
return true;
|
||||
}
|
||||
else throw new Exception("Invalid server response type");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
||||
@ -149,10 +163,8 @@ namespace isn
|
||||
{
|
||||
report.Message = rex.Message;
|
||||
Console.Error.WriteLine(rex.Message);
|
||||
#if DEBUG
|
||||
Console.Error.WriteLine("Stack trace:");
|
||||
Console.Error.WriteLine(rex.StackTrace);
|
||||
#endif
|
||||
throw;
|
||||
}
|
||||
}, httpWebRequest);
|
||||
|
Reference in New Issue
Block a user