private static void ProcessLabelCommand(ConsoleCommand command) { switch (command.Current) { case "-get": PrintLabels(); return; default: ConsoleWriter.WriteInYellow($"Unknown label command '{command}'"); return; } }
static void Main(string[] args) { string credentialsPath = args[0]; string tokenDir = args[1]; if (!File.Exists(credentialsPath) || !Directory.Exists(tokenDir)) { throw new ArgumentException("Must pass in credentials file location and token directory"); } _log = new ConsoleTraceLogger("Gman.UI.Iron"); var factory = new GmailServiceFactory(); _service = factory.BuildAsync(new GmailServiceConfig { ApiScopes = new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailSend }, AppName = "Mekmak Gman", CredentialsFilePath = credentialsPath, TokenDirectory = tokenDir }).Result; _messageProvider = new MessageProvider(_log.GetSubLogger("MessageProvider"), _service); while (true) { Console.Write("> "); var command = ConsoleCommand.New(Console.ReadLine()); if (command.Current == "quit" || command.Current == "exit") { Console.WriteLine("Goodbye!"); return; } try { ProcessCommand(command); } catch (Exception ex) { Console.WriteLine(); ConsoleWriter.WriteInRed($"ERROR: {ex.Message}"); ConsoleWriter.WriteInRed($"{ex.StackTrace}"); Console.WriteLine(); } } }
private static void ProcessCommand(ConsoleCommand command) { switch (command.Current) { case "label": ProcessLabelCommand(command.Advance()); return; case "message": ProcessMessageCommand(command.Advance()); return; default: ConsoleWriter.WriteInYellow($"Unknown command '{command}'"); return; } }
private static void PrintMessages(ConsoleCommand command) { string labelId = command.Current; ConsoleWriter.WriteInGreen($"Fetching messages with label {labelId}.."); var messages = _messageProvider.GetMessagesWithLabel(TraceId.New(), labelId); if (!messages.Any()) { ConsoleWriter.WriteInGreen($"No messages found for label {labelId}"); return; } foreach (var message in messages.OrderBy(m => m.Id)) { ConsoleWriter.WriteInGreen($"{message.Id}\t{message.Date}\t{message.Subject}"); } }
private static void DownloadWithLabel(ConsoleCommand command) { string labelId = command.Current; string dir = command.Advance().Current; Directory.CreateDirectory(dir); ConsoleWriter.WriteInGreen($"Downloading messages with label {labelId} to {dir}.."); var messages = _messageProvider.GetMessagesWithLabel(TraceId.New(), labelId); if (!messages.Any()) { ConsoleWriter.WriteInGreen($"No messages found for label {labelId}"); return; } foreach (var message in messages) { SaveMessage(message, dir); } }
private static void UploadReceipts(ConsoleCommand command) { const string sentFileName = "sent.txt"; string photoDir = command.Current; ConsoleWriter.WriteInGreen($"Reading dir {photoDir}.."); var directoryInfo = new DirectoryInfo(photoDir); FileInfo sentFile = directoryInfo.GetFiles(sentFileName).FirstOrDefault(); if (sentFile == null) { string sentFilePath = Path.Combine(directoryInfo.FullName, sentFileName); File.WriteAllText(sentFilePath, ""); sentFile = directoryInfo.GetFiles(sentFileName).Single(); if (sentFile == null) { throw new IOException($"Could not open sent file at {sentFilePath}"); } } var sentIds = new HashSet <string>(File.ReadAllLines(sentFile.FullName).Where(l => !string.IsNullOrWhiteSpace(l))); var ignoredIds = new HashSet <FileInfo>(); var imagesToSend = new HashSet <FileInfo>(); List <FileInfo> photoFiles = directoryInfo.GetFiles().Where(f => !f.Name.EndsWith(".txt")).ToList(); foreach (FileInfo photoFile in photoFiles) { if (sentIds.Contains(photoFile.Name)) { ignoredIds.Add(photoFile); } else { imagesToSend.Add(photoFile); } } if (imagesToSend.Count == 0) { ConsoleWriter.WriteInYellow("All photos were already sent - aborting"); return; } ConsoleWriter.WriteInGreen($"About to send {imagesToSend.Count} emails, {ignoredIds.Count} were already sent - proceed? (y/n)"); string input = Console.ReadLine() ?? "n"; if (!"y".Equals(input.Trim().ToLowerInvariant())) { ConsoleWriter.WriteInYellow("Aborting"); return; } foreach (FileInfo toSend in imagesToSend) { try { ConsoleWriter.WriteInGreen($"Sending {toSend.Name}.."); _messageProvider.SendMessage("TAXES AUTO 2019", toSend.Name, toSend.FullName); } catch (Exception ex) { ConsoleWriter.WriteInRed($"Failed to send message, aborting: {ex.Message}"); return; } try { File.AppendAllLines(sentFile.FullName, new[] { toSend.Name }); } catch (Exception ex) { ConsoleWriter.WriteInRed($"Could not update sent file with {toSend.Name} - aborting: {ex.Message}"); return; } } ConsoleWriter.WriteInGreen("Upload completed"); }