Ejemplo n.º 1
0
        public static bool Delete(string path, bool isFile)
        {
            if (!Util.IsWindows)
            {
                // Delete the destination as su
                //		sudo rm -rf destination
                var args = $"-c 'sudo rm -rf \"{path}\"'";

                if (Verbose)
                {
                    Console.WriteLine($"{ShellProcessRunner.MacOSShell} {args}");
                }

                var p = new ShellProcessRunner(new ShellProcessRunnerOptions(ShellProcessRunner.MacOSShell, args)
                {
                    RedirectOutput = Verbose
                });

                p.WaitForExit();

                return(true);
            }
            else
            {
                try
                {
                    if (isFile)
                    {
                        File.Delete(path);
                    }
                    else
                    {
                        Directory.Delete(path, true);
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    Util.Exception(ex);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        public static Task <ShellProcessRunner.ShellProcessResult> WrapShellCommandWithSudo(string cmd, string workingDir, bool verbose, string[] args)
        {
            var actualCmd  = cmd;
            var actualArgs = string.Join(" ", args);

            if (!Util.IsWindows)
            {
                actualCmd  = ShellProcessRunner.MacOSShell;
                actualArgs = $"-c 'sudo {cmd} {actualArgs}'";
            }

            var cli = new ShellProcessRunner(new ShellProcessRunnerOptions(actualCmd, actualArgs)
            {
                WorkingDirectory = workingDir, Verbose = verbose
            });

            return(Task.FromResult(cli.WaitForExit()));
        }
Ejemplo n.º 3
0
        public static async Task <bool> WrapCopyWithShellSudo(string destination, bool isFile, Func <string, Task <bool> > wrapping)
        {
            var intermediate = destination;

            var destDir = intermediate;

            if (isFile)
            {
                destDir = new FileInfo(destDir).Directory.FullName;
            }

            if (!Util.IsWindows)
            {
                if (isFile)
                {
                    intermediate = Path.Combine(Path.GetTempFileName());
                }
                else
                {
                    intermediate = Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString());
                }
            }

            // If windows, we'll delete the directory or file at destination here
            if (Util.IsWindows)
            {
                var dir = isFile ? new FileInfo(destination).Directory.FullName : new DirectoryInfo(destination).FullName;

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }

            var r = await wrapping(intermediate);

            if (r && !Util.IsWindows)
            {
                // Copy a file to a destination as su
                //		sudo mkdir -p destDir && sudo cp -pP intermediate destination

                // Copy a folder recursively to the destination as su
                //		sudo mkdir -p destDir && sudo cp -pPR intermediate/ destination
                var args = isFile
                                        ? $"-c 'sudo mkdir -p \"{destDir}\" && sudo cp -pP \"{intermediate}\" \"{destination}\"'"
                                        : $"-c 'sudo mkdir -p \"{destDir}\" && sudo cp -pPR \"{intermediate}/\" \"{destination}\"'"; // note the / at the end of the dir

                if (Verbose)
                {
                    Console.WriteLine($"{ShellProcessRunner.MacOSShell} {args}");
                }

                var p = new ShellProcessRunner(new ShellProcessRunnerOptions(ShellProcessRunner.MacOSShell, args)
                {
                    RedirectOutput = Verbose
                });

                p.WaitForExit();
            }

            return(r);
        }
Ejemplo n.º 4
0
        public static ShellProcessResult Run(string executable, string args)
        {
            var p = new ShellProcessRunner(new ShellProcessRunnerOptions(executable, args));

            return(p.WaitForExit());
        }