private static PostAnalysis AnalysePosts(Options options) { int failures = 0; int successes = 0; var postList = new List<post>(); foreach (var filename in Directory.GetFiles(options.InputDirectory, "*.xml")) { Logger.Log("Filename: {0}", filename); try { postList.Add(Miniblog.Utils.ExtractPostFromFile(filename)); successes++; } catch (Exception ex) { Logger.Log("Failed to parse post: {0}", ex.Message); failures++; } } return new PostAnalysis() { Successes = successes, Failures = failures, Posts = postList }; }
private static bool PerformValidation(Options options) { Logger.Log("Validating..."); bool valid = true; // Check input path exists if (!Directory.Exists(options.InputDirectory)) { Logger.Log("Input directory does not exist."); valid = false; } if (!Directory.Exists(options.OutputPath)) { Directory.CreateDirectory(options.OutputPath); } Logger.Log(valid ? "OK" : "Failed"); return valid; }
static void Main(string[] args) { var options = new Options(); if (CommandLine.Parser.Default.ParseArguments(args, options)) { // Values are available here if (options.Verbose) { Logger.Log("Input Directory: {0}", options.InputDirectory); Logger.Log("Output Path: {0}", options.OutputPath); } } var valid = PerformValidation(options); if (!valid) { return; } var tempFolder = Path.Combine(options.OutputPath, "temp"); if (!Directory.Exists(tempFolder)) { Directory.CreateDirectory(tempFolder); } var postAnalysis = AnalysePosts(options); bool continueToImport = true; if (postAnalysis.Failures > 0) { Console.WriteLine("There were {0} failures. Do you want to import {1} entries. Y/N", postAnalysis.Failures, postAnalysis.Successes); var proceed = Console.ReadKey(); continueToImport = (proceed.KeyChar == 'y' || proceed.KeyChar == 'Y'); if (continueToImport) { Logger.Log("Continuing with import."); } } if (postAnalysis.Posts.Count == 0) { Console.WriteLine("There are no posts to import."); continueToImport = false; } if (!continueToImport) { Logger.Log("Exiting"); return; } var buildGhostObject = Ghost.Utils.BuildGhostObject(postAnalysis.Posts, options); SaveJsonFile(buildGhostObject, options.OutputPath); //UploadImagesToCloudinary(); Directory.Delete(tempFolder, true); Logger.Log("Finished"); Console.ReadKey(); }