public void Flush(Workspace workspace) { ConsoleEx.Info($"Running sink: {this.GetType().Name}"); var request = new RestRequest(@"api/workspaces", Method.POST); request.AddJsonBody(workspace); request.RequestFormat = DataFormat.Json; var restClient = new RestClient(serviceUri); var response = restClient.Post(request); if(response.StatusCode == HttpStatusCode.OK) { ConsoleEx.Ok("Workspace analysis report published."); var locationHeader = response.Headers.FirstOrDefault(h=> h.Name == "Location"); if (locationHeader != null) { string workspaceLocation = locationHeader.Value.ToString().ToLower(); ConsoleEx.Ok($"Visit {workspaceLocation}"); } } else { ConsoleEx.Error($"Workspace not published to target sink."); ConsoleEx.Error($"Status: {response.StatusCode}, Response: {response.Content}"); } }
//example 1: simple //>arnis /ws:"c:\github\arnis" //>arnis /ws:"c:\github\arnis" /sf:"c:\skip.txt" //example 1: publish into web sink //>arnis /web /r /u:[email protected] //>arnis /web /apk:"XXXX-XXXX" /ws:"c:\github\arnis" /wsn="arnis" static void Main(string[] args) { try { var settings = args.Select(a => { var parameter = a.Split('='); return new { Key = parameter[0].Replace("/", ""), Value = parameter.Length == 2 ? parameter[1] : null }; }).ToDictionary(m => m.Key, m => m.Value); PrintSettings(settings); //capture /sf var skipList = GetSkipList(settings); //capture /web var slashWebExist = settings.ContainsKey("web"); if (slashWebExist) { //capture /web /r var slashRExist = settings.ContainsKey("r"); if (slashRExist) { //capture /web /r /u var slashUExist = settings.ContainsKey("u"); if (slashUExist) { var userName = settings.SingleOrDefault(s => s.Key == "u").Value; var sinkRegistrar = new WebSinkRegistrar(); var registration = sinkRegistrar.Register(userName); if (null != registration) { string response = $"{Environment.NewLine} Api Key: {registration.ApiKey}" + $"{Environment.NewLine} Workspace: {registration.Workspace}" + $"{Environment.NewLine} Workspace Uri: {registration.WorkspaceUri}" + $"{Environment.NewLine} Account Uri: {registration.AccountUri}"; ConsoleEx.Ok($"Done! Please keep your API key secret.{response}"); } } } //capture /web /apk /ws /wsn var slashApkExist = settings.ContainsKey("apk"); if (slashApkExist) { var apiKey = settings.SingleOrDefault(s => s.Key == "apk").Value; //validate working folder /ws is required parameter string slashWs = settings.SingleOrDefault(s => s.Key == "ws").Value; if (null == slashWs) { throw new ArgumentException("Missing parameter", "ws"); } //validate working folder /wsn is required parameter string slashWsn = settings.SingleOrDefault(s => s.Key == "wsn").Value; if (null == slashWsn) { throw new ArgumentException("Missing parameter", "wsn"); } //run all trackers, at this point we have all the dependencies var trackerResult = TrackDependencies(slashWs, skipList); //create the workspace details var workspace = new Workspace { Name = slashWsn, Solutions = trackerResult.Solutions, Logs = trackerResult.Logs, ApiKey = apiKey }; var webSink = new WebSink(); webSink.Flush(workspace); } } else { //capture /ws string slashWs = settings.SingleOrDefault(s => s.Key == "ws").Value; if (null == slashWs) { throw new ArgumentException("Missing parameter", "ws"); } //fall back to default csv as default sink //run all trackers, at this point we have all the dependencies var trackerResult = TrackDependencies(slashWs, skipList); //create the workspace details string worspaceName = new DirectoryInfo(slashWs.TrimEnd(Path.DirectorySeparatorChar)).Name; var workspace = new Workspace { Name = worspaceName.ToLower(), Solutions = trackerResult.Solutions, Logs = trackerResult.Logs }; //run default sink SinkDependencies("csv", workspace); } } catch (Exception ex) { ConsoleEx.Error("Arnis.NET breaks ;(. \n" + ex.Message); } }
private static void SinkDependencies(string sink, Workspace workspace) { string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var assemblies = GetSinkAssemblies(path); var sinks = assemblies .SelectMany(s => s.GetTypes()) .Where(p => typeof(ISink).IsAssignableFrom(p) && !p.IsInterface && p.Name.ToLower() == $"{sink}sink".ToLower()); var sinkTasks = sinks.Select(t => { var tracker = Activator.CreateInstance(t) as ISink; return Task.Factory.StartNew(() => tracker?.Flush(workspace)); }); Task.WaitAll(sinkTasks.ToArray()); }