Example #1
0
        public async Task <ExecutionStatus> Execute(ComparisonOptions initialOptions)
        {
            Console.Write($"{AppProperties.Name}> ");

            var inputString = Console.ReadLine();

            if (string.IsNullOrEmpty(inputString))
            {
                return(statusFactory.ConstructFaulted(
                           new MissingInputException(
                               "You must enter either a directory ('C:\\to\\some\\directory'), or a pair of files separated by a coma ('C:\\path\\to\\first.png,C:\\path\\to\\second.jpg').")));
            }

            if (CommandIsGiven(inputString, ProgramCommands.ForTermination))
            {
                return(statusFactory.ConstructTerminated());
            }

            if (CommandIsGiven(inputString, ProgramCommands.ForHelp))
            {
                Console.WriteLine(ConsoleInterface.GetHelpText());

                return(statusFactory.ConstructNoOp());
            }

            var comparisonRequest = requestFactory.Construct(inputString);

            if (CommandIsGiven(inputString, ProgramCommands.ToChangeOptions))
            {
                initialOptions = OverwriteComparisonOptions(initialOptions);

                // We need to return here, so that we can reset the console
                // input. Otherwise we'll accidentally attempt to process "options"
                // as a directory.
                return(statusFactory.ConstructUpdated(comparisonRequest, initialOptions));
            }

            var imageComparer = comparisonFactory.Construct(comparisonRequest, initialOptions);

            Option <List <DeDupifyrResult> > duplicateResults = new None <List <DeDupifyrResult> >();

            Action printInstructions;

            // For now, just doing a try/catch at the highest level. I plan
            // to have much better handling. I'll implement the `Either` monad
            // and have the `Run` method return an instance of that. From there,
            // I can determine whether to write an error out, or to write the results.
            try
            {
                var sw      = Stopwatch.StartNew();
                var results = await imageComparer.Run(comparisonRequest);

                sw.Stop();

                Console.WriteLine($"Done in {sw.ElapsedMilliseconds} ms.");

                duplicateResults  = new Some <List <DeDupifyrResult> >(results);
                printInstructions = imageComparer.PrintInstructions();
            }
            catch (Exception exception)
            {
                return(statusFactory.ConstructFaulted(exception));
            }

            return(statusFactory.ConstructSuccess(
                       comparisonRequest,
                       duplicateResults,
                       printInstructions));
        }