Source

Bitbucket Backup / src / BitbucketBackup / Config.cs

Christian Specht 9cf2e9b 
Christian Specht 2f70e49 
Christian Specht 9cf2e9b 
Christian Specht 2dcf34b 


Christian Specht 4f6c48e 


Christian Specht 32b498b 
Christian Specht 2dcf34b 
Christian Specht 4f6c48e 
Christian Specht 2436b14 




Christian Specht 8b3568d 

Christian Specht 9cf2e9b 



Christian Specht 7879e20 




Christian Specht 22a53ba 
Christian Specht 9cf2e9b 


Christian Specht 2f70e49 




Christian Specht 22a53ba 
Christian Specht 9cf2e9b 


Christian Specht 8b3568d 

Christian Specht 4f6c48e 

Christian Specht 9cf2e9b 







Christian Specht 2f70e49 




Christian Specht 9cf2e9b 



Christian Specht 4f6c48e 



Christian Specht 9cf2e9b 







Christian Specht 2f70e49 




Christian Specht 9cf2e9b 



Christian Specht 2dcf34b 
Christian Specht 8b3568d 
Christian Specht 2436b14 





































Christian Specht 9cf2e9b 
Christian Specht 8b3568d 
Christian Specht d2ea0dd 
Christian Specht 2dcf34b 
Christian Specht 2436b14 
Christian Specht d2ea0dd 


Christian Specht 9cf2e9b 









Christian Specht 2436b14 
Christian Specht 9cf2e9b 
Christian Specht 4c004dd 






























Christian Specht 9cf2e9b 
Christian Specht 2436b14 
Christian Specht 9cf2e9b 

Christian Specht 2436b14 



Christian Specht 2dcf34b 

using System;
using System.IO;
using BitbucketBackup.Properties;

namespace BitbucketBackup
{
    /// <summary>
    /// helper class for getting values out of the config file
    /// </summary>
    internal class Config : IConfig
    {
        /// <summary>
        /// default timeout for pulling
        /// </summary>
        const int DefaultPullTimeout = 60;

        /// <summary>
        /// Bitbucket password
        /// </summary>
        public string PassWord
        {
            get
            {
                if (String.IsNullOrEmpty(Settings.Default.PassWord))
                {
                    return Settings.Default.PassWord;
                }

                return StringEncryptor.Decrypt(Settings.Default.PassWord);
            }
            private set
            {
                if (value == string.Empty)
                {
                    throw new ClientException(Resources.InputPasswordInvalid, null);
                }

                Settings.Default.PassWord = StringEncryptor.Encrypt(value);
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// Bitbucket username
        /// </summary>
        public string UserName
        {
            get
            {
                return Settings.Default.UserName;
            }
            private set
            {
                if (value == string.Empty)
                {
                    throw new ClientException(Resources.InputUserInvalid, null);
                }

                Settings.Default.UserName = value;
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// Folder on local machine where backups are saved
        /// </summary>
        public string BackupFolder
        {
            get
            {
                return Settings.Default.BackupFolder;
            }
            private set
            {
                if (value == string.Empty || !Directory.Exists(value))
                {
                    throw new ClientException(Resources.InputBackupFolderInvalid, null);
                }

                Settings.Default.BackupFolder = value;
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// Timeout for pulling (only for Mercurial at the moment)
        /// </summary>
        public int PullTimeout
        {
            get
            {
                return Settings.Default.PullTimeout;
            }
        }

        /// <summary>
        /// Timeout for pulling (setting only)
        /// </summary>
        /// <remarks>different property for saving, because input is a string, but needs to be saved as an int</remarks>
        internal string PullTimeoutInternal
        {
            set
            {
                int timeout;

                if (String.IsNullOrEmpty(value))
                {
                    timeout = DefaultPullTimeout;
                }
                else
                {
                    if (!int.TryParse(value, out timeout) || timeout < 1)
                    {
                        throw new ClientException(Resources.InputPullTimeoutInvalid, null);
                    }
                }

                Settings.Default.PullTimeout = timeout;
                Settings.Default.Save();
            }
        }

        /// <summary>
        /// Checks if the config is complete (if none of the settings is empty)
        /// </summary>
        public bool IsComplete()
        {
            // don't check PullTimeout because it's optional!
            return this.UserName != string.Empty
                && this.PassWord != string.Empty
                && this.BackupFolder != string.Empty;
        }

        /// <summary>
        /// Asks for the config settings
        /// </summary>
        public void Input()
        {
            Console.WriteLine(Resources.InputUser);
            this.UserName = Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine(Resources.InputPassword);

            string pw = string.Empty;
            ConsoleKeyInfo key;

            do
            {
                key = Console.ReadKey(true);

                switch (key.Key)
                {
                    case ConsoleKey.Backspace:
                        if (pw.Length > 0)
                        {
                            pw = pw.Substring(0, pw.Length - 1);
                            Console.Write("\b \b");
                        }
                        break;

                    case ConsoleKey.Enter:
                        Console.WriteLine();
                        break;

                    default:
                        pw += key.KeyChar;
                        Console.Write("*");
                        break;
                }
            }
            while (key.Key != ConsoleKey.Enter);

            this.PassWord = pw;

            Console.WriteLine();
            Console.WriteLine(Resources.InputBackupFolder);
            this.BackupFolder = Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine(String.Format(Resources.InputPullTimeout, DefaultPullTimeout));
            this.PullTimeoutInternal = Console.ReadLine();
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.