Пример #1
0
        public static void Main(string[] args)
        {
            try
            {
                // Parse commandline settings.
                SettingsParser settingsParser = new SettingsParser();
                settingsParser.ParseSettingsFromCommandlineArguments(args);

                WriteErrorMessageAndQuitIfNoArgumentsGiven(args);
                ShowHelpIfArgumentsSpecifyItSo(settingsParser.ApplicationSettings.ShowHelp, settingsParser.Options);

                // Initialize logging.
                _loggerFactory = new LoggerFactory();
                _loggerFactory.AddConsole();
                AddAnyExternalLogProviders(_loggerFactory);
                _logger = _loggerFactory.CreateLogger <Program>();

                string applicationVersion = GetAssemblyVersion();
                _logger.LogInformation($"**** DotNetSftp - version {applicationVersion} ****");



                // Handle settings file functionality.
                bool settingsFilePathIsSpecified = !string.IsNullOrWhiteSpace(settingsParser.ApplicationSettings.SettingsFilePath);
                if (settingsFilePathIsSpecified)
                {
                    bool settingsFileExists = File.Exists(settingsParser.ApplicationSettings.SettingsFilePath);

                    if (settingsFileExists)
                    {
                        settingsParser = ImportSettingsFromFile(settingsParser.ApplicationSettings.SettingsFilePath, _logger, settingsParser.ApplicationSettings.SettingsKeyFilePath);
                    }
                    else
                    {
                        SaveSettingsToFile(settingsParser, settingsParser.ApplicationSettings.SettingsFilePath, _logger, settingsParser.ApplicationSettings.SettingsKeyFilePath);
                    }
                }

                // if the settings-file contains info on disk-logging, re-configure the logger.
                if (!string.IsNullOrWhiteSpace(settingsParser.ApplicationSettings.DiskLogLocation))
                {
                    _logger.LogInformation($"** Logging to directory {settingsParser.ApplicationSettings.DiskLogLocation}");
                    ConfigureFileLogger(settingsParser.ApplicationSettings.DiskLogLocation, _loggerFactory);
                    _logger = _loggerFactory.CreateLogger <Program>();
                }

                // Validate settings.
                settingsParser.ValidateTransferSettings();
                settingsParser.ValidateApplicationSettings();

                // Create sftp client
                _dotNetSftpClient = new DotNetSftpClient(settingsParser.ConnectivitySettings, _logger);
                _dotNetSftpClient.CreateConnection();

                // Initiate transfer according to settings
                if (settingsParser.TransferSettings.TransferType == 'u')
                {
                    _dotNetSftpClient.Upload(settingsParser.TransferSettings);
                }
                else if (settingsParser.TransferSettings.TransferType == 'd')
                {
                    _dotNetSftpClient.Download(settingsParser.TransferSettings);
                }
                else
                {
                    throw new ArgumentException($"Bad transfer-type '{settingsParser.TransferSettings.TransferType}' specified.");
                }
            }
            catch (SettingsParsingException e)
            {
                // output some error message
                Console.Write("dotnetsftp.exe: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `dotnetsftp --help' for more information.");

                Environment.Exit(0);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error occured.");
                throw;
            }
        }