public static PublishSshOptions ParseArgs(string[] args)
        {
            var options = new PublishSshOptions();

            for (var idx = 0; idx < args.Length; idx++)
            {
                var arg = args[idx];
                switch (arg)
                {
                case "--ssh-host":
                    options.Host = GetValue(ref args, ref idx);
                    break;

                case "--ssh-port":
                    var value = GetValue(ref args, ref idx);
                    options.Port = Convert.ToInt32(value);
                    break;

                case "--ssh-user":
                    options.User = GetValue(ref args, ref idx);
                    break;

                case "--ssh-password":
                    options.Password = GetValue(ref args, ref idx);
                    break;

                case "--ssh-keyfile":
                    options.KeyFile = GetValue(ref args, ref idx);
                    break;

                case "--ssh-path":
                    options.Path = GetValue(ref args, ref idx);
                    break;

                case "--ssh-cmd-before":
                    options.CmdBefore = GetValue(ref args, ref idx);
                    break;

                case "--ssh-cmd-after":
                    options.CmdAfter = GetValue(ref args, ref idx);
                    break;

                case "-o":
                    options.LocalPath = GetValue(ref args, ref idx);
                    break;

                case "-?":
                case "-h":
                case "--help":
                    options.PrintHelp = true;
                    break;
                }
            }

            ValidateOptions(options);

            options.Args = args;

            return(options);
        }
 private static void ValidateOptions(PublishSshOptions options)
 {
     if (string.IsNullOrEmpty(options.Host) ||
         string.IsNullOrEmpty(options.User) ||
         string.IsNullOrEmpty(options.Path))
     {
         options.PrintHelp = true;
     }
 }
Exemple #3
0
        private static void PrepareOptions(PublishSshOptions options)
        {
            if (string.IsNullOrEmpty(options.LocalPath))
            {
                var tempPath = Path.Combine(Path.GetTempPath(), $"publish.{Guid.NewGuid()}");
                Directory.CreateDirectory(tempPath);
                options.LocalPath = tempPath;
            }

            options.Args = options.Args.Concat(new[] { "-o", options.LocalPath }).ToArray();
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            var options = PublishSshOptions.ParseArgs(args);

            if (options.PrintHelp)
            {
                PrintHelp();
                return;
            }

            PrepareOptions(options);

            var arguments = string.Join(" ", options.Args);

            if (!PublishLocal(arguments))
            {
                return;
            }

            var path      = options.Path;
            var localPath = options.LocalPath;

            if (!path.EndsWith("/"))
            {
                path = path + "/";
            }

            localPath = Path.GetFullPath(localPath) + Path.DirectorySeparatorChar;

            var localFiles = GetLocalFiles(localPath);

            Console.WriteLine();
            Console.WriteLine($"Uploading {localFiles.Count} files to {options.User}@{options.User}:{options.Port}{options.Path}");


            try
            {
                var runner = new Runner(options);
                runner.RunBefore();
                var uploader = new Uploader(options);
                uploader.UploadFiles(path, localFiles);
                runner.RunAfter();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error uploading files to server: {ex.Message}");
            }

            Directory.Delete(localPath, true);
        }
Exemple #5
0
        internal static ConnectionInfo CreateConnectionInfo(PublishSshOptions options)
        {
            var authenticationMethods = new List <AuthenticationMethod>();

            if (options.Password != null)
            {
                authenticationMethods.Add(
                    new PasswordAuthenticationMethod(options.User, options.Password));
            }

            if (options.KeyFile != null)
            {
                authenticationMethods.Add(
                    new PrivateKeyAuthenticationMethod(options.User, new PrivateKeyFile(options.KeyFile)));
            }

            var connectionInfo = new ConnectionInfo(
                options.Host,
                options.Port,
                options.User,
                authenticationMethods.ToArray());

            return(connectionInfo);
        }
Exemple #6
0
 public Uploader(PublishSshOptions publishSshOptions)
 {
     _connectionInfo = CreateConnectionInfo(publishSshOptions);
 }
Exemple #7
0
        //[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Main(string[] args)
        {
            InvocationTime = DateTime.UtcNow;
            Console.WriteLine($"Processor Count: {Environment.ProcessorCount}");

            var options = PublishSshOptions.ParseArgs(args);

            if (options.PrintHelp)
            {
                PrintHelp();
                return;
            }

            PrepareOptions(options);

            var arguments = string.Join(" ", options.Args);
            var localPath = Path.GetFullPath(options.LocalPath);

            Directory.CreateDirectory(localPath);

            using (FileSystemWatcher watcher = new FileSystemWatcher())
            {
                watcher.Path         = localPath;
                watcher.NotifyFilter = NotifyFilters.LastAccess
                                       | NotifyFilters.LastWrite
                                       | NotifyFilters.FileName
                                       | NotifyFilters.DirectoryName;

                // Add event handlers.
                watcher.Changed += OnChanged;
                watcher.Created += OnChanged;
                watcher.Deleted += OnChanged;
                watcher.Renamed += OnRenamed;

                // Begin watching.
                watcher.EnableRaisingEvents = true;

                // Wait for the user to quit the program.
                if (!PublishLocal(arguments))
                {
                    return;
                }
            }

            var path = options.Path;

            if (!path.EndsWith("/"))
            {
                path = path + "/";
            }
            localPath += Path.DirectorySeparatorChar;

            var localFiles = ChangedFiles;             // GetLocalFiles(localPath);

            if (localFiles.Count <= 0)
            {
                Console.WriteLine($"No files needs to be uploaded!");
            }
            else
            {
                Console.WriteLine($"\nUploading {localFiles.Count} files to {options.User}@{options.Host}:{options.Port}{options.Path}");

                try
                {
                    var runner = new Runner(options);
                    runner.RunBefore();
                    var uploader = new Uploader(options);
                    uploader.UploadFiles(path, localFiles.Select(f => new LocalFile(localPath, f)).ToList());
                    runner.RunAfter();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error uploading files to server: {ex.Message}");
                }
            }
            //Directory.Delete(localPath, true);
            Console.WriteLine($"\nPublished in {TimeSpan.FromSeconds((int)(DateTime.UtcNow - InvocationTime).TotalSeconds):g} !\nThanks for using dotnet publish-ssh!");
        }
Exemple #8
0
 public Runner(PublishSshOptions publishSshOptions)
 {
     connectionInfo = Uploader.CreateConnectionInfo(publishSshOptions);
     cmdBefore      = publishSshOptions.CmdBefore;
     cmdAfter       = publishSshOptions.CmdAfter;
 }