예제 #1
0
        /// <summary>
        /// Attempts to read available backup configurations from "configuration.json"
        /// located in the executable directory.
        /// </summary>
        public Configuration()
        {
            string cFile;

            ConfigFileSection[] configFile;

            // Read the configuration file
            try
            {
                cFile = File.ReadAllText(AssemblyDirectory + "\\configuration.json");
            }
            catch (FileNotFoundException notFoundEx)
            {
                Help.ConfigurationFile();
                throw new Exception("The configuration.json file was not found.", notFoundEx);
            }
            catch (Exception ex)
            {
                Help.ConfigurationFile();
                throw new Exception("There was a general failure reading the configuration.json file.", ex);
            }



            // Parse the JSON configuration file
            Log.Write("Foldup is checking the configuration file... ");
            try
            {
                configFile = Newtonsoft.Json.JsonConvert.DeserializeObject <ConfigFileSection[]>(cFile);
            }
            catch (Exception nsEx)
            {
                Help.ConfigurationFile();
                throw new Exception("Unable to parse the configuration.json file.", nsEx);
            }



            // Validate the data in the configuration file
            if (configFile.Count() == 0)
            {
                Help.ConfigurationFile();
                throw new Exception("No \"backups\" section was found in the configuration file.");
            }



            // Build a new list of validated configuration sections
            List <ConfigFileSection> validSections = new List <ConfigFileSection>();
            // Build a list of invalid configuration sections for error reporting
            List <ConfigFileSection> invalidSections = new List <ConfigFileSection>();


            // sectionNumber is used for error reporting if a section does not have a title
            int sectionNumber = 1;

            foreach (ConfigFileSection section in configFile)
            {
                bool valid = true;
                if (string.IsNullOrWhiteSpace(section.title))
                {
                    section.title = "section " + sectionNumber.ToString();
                    Errors.Add(new Exception("Configuration section " + sectionNumber.ToString() + " has no title."));
                    valid = false;
                }
                // --all is a command line switch so it can't be a backup also
                if (section.title == "all")
                {
                    Errors.Add(new Exception("A configuration section cannot be named \"all\"."));
                    valid = false;
                }
                if (section.title.IndexOf(' ') > -1)
                {
                    Errors.Add(new Exception("Configuration section \"" + section.title + "\" cannot have spaces in its title."));
                    valid = false;
                }
                if (string.IsNullOrWhiteSpace(section.source))
                {
                    Errors.Add(new Exception("Configuration section \"" + section.title + "\" has no source folder specified."));
                    valid = false;
                }
                if (string.IsNullOrWhiteSpace(section.dest))
                {
                    Errors.Add(new Exception("Configuration section \"" + section.title + "\" has no destination folder specified."));
                    valid = false;
                }
                if (valid == true)
                {
                    validSections.Add(section);
                }
                else
                {
                    invalidSections.Add(section);
                }
                sectionNumber++;
            }



            // If errors were encountered list them.
            if (Errors.Count() > 0)
            {
                string errCount = "";
                errCount += Errors.Count().ToString();
                if (Errors.Count == 1)
                {
                    errCount += " error was";
                }
                else
                {
                    errCount += " errors were";
                }
                errCount += " encountered.";
                Log.WriteLine(errCount, ConsoleColor.Red);

                foreach (Exception ex in Errors)
                {
                    Log.WriteLine(ex.Message);
                }
                Log.WriteLine();
            }
            else
            {
                Log.WriteLine("Seems fine.");
            }



            // Are there any valid sections?
            if (validSections.Count() == 0)
            {
                // No valid sections so we can't possibly continue.
                Help.ConfigurationFileSection();
                throw new Exception("No valid backup configurations were found.");
            }
            // At least one good section exists so we can continue.
            if (invalidSections.Count() > 0)
            {
                string badSectionCount = invalidSections.Count().ToString();
                if (invalidSections.Count() == 1)
                {
                    badSectionCount += " section";
                }
                else
                {
                    badSectionCount += " sections";
                }
                badSectionCount += " could not be processed.";
                Log.Write(badSectionCount);
            }

            // Convert the JSON data into ConfigurationSections
            foreach (ConfigFileSection cFileSection in validSections)
            {
                ConfigurationSection section;
                try
                {
                    section = new ConfigurationSection(cFileSection);
                    this.Backups.Add(section);
                }
                catch (Exception configEx)
                {
                    throw new Exception("Error in configuration section \"" + cFileSection.title + "\"", configEx);
                }
            }
        }
예제 #2
0
파일: Help.cs 프로젝트: thelowman/foldup
        public static void Usage(ConfigurationSection section)
        {
            List <string> notes = new List <string>();

            Log.Write(" --" + section.title, ConsoleColor.White);
            tab(20);
            Log.WriteLine(section.description);

            tab(10);
            Log.Write("from:");
            tab(20);
            if (section.source.Exists)
            {
                Log.WriteLine(section.source.ToString(), ConsoleColor.Green);
            }
            else
            {
                Log.WriteLine(section.source.ToString(), ConsoleColor.Red);
                notes.Add("The specified source directory does not exist.  The process will fail.");
            }

            tab(10);
            Log.Write("to:");
            tab(20);
            if (section.dest.Exists)
            {
                Log.WriteLine(section.dest.ToString(), ConsoleColor.Green);
            }
            else
            {
                Log.WriteLine(section.dest.ToString(), ConsoleColor.Yellow);
                notes.Add("The specified destination directory does not exist.  Will attempt to create it.");
            }

            tab(10);
            Log.Write("ignore:");
            if (section.ignoreFolders.Length == 0)
            {
                tab(20);
                Log.WriteLine("none");
            }
            else
            {
                tab(20);
                for (int i = 0; i < section.ignoreFolders.Length; i++)
                {
                    Log.Write(section.ignoreFolders[i]);
                    if (i < section.ignoreFolders.Length - 1)
                    {
                        Log.Write(", ");
                    }
                }
                Log.WriteLine();
            }

            foreach (string note in notes)
            {
                tab(10);
                Log.WriteLine(note);
            }
            //tab(10);
            //Log.Write("Use ");
            //Log.Write("--" + section.title, ConsoleColor.White);
            //Log.WriteLine(" to run this from the command line.");
        }