-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ForceOps.Lib example (ForceOps.DeleteExample) and docs to README.md
- Loading branch information
Showing
7 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ForceOps.Lib\ForceOps.Lib.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using ForceOps.Lib; | ||
|
||
var fileOrDirectories = args.Select(arg => Path.Combine(Environment.CurrentDirectory, arg)).ToArray(); | ||
var forceOpsContext = new ForceOpsContext(); | ||
var fileAndDirectoryDeleter = new FileAndDirectoryDeleter(forceOpsContext); | ||
|
||
RelaunchHelpers.RunWithRelaunchAsElevated(() => | ||
{ | ||
foreach (var fileOrDirectory in fileOrDirectories) | ||
{ | ||
fileAndDirectoryDeleter.DeleteFileOrDirectory(fileOrDirectory, true); | ||
} | ||
}, () => args.ToList(), forceOpsContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Serilog; | ||
|
||
namespace ForceOps.Lib; | ||
|
||
public static class RelaunchHelpers | ||
{ | ||
public static void RunWithRelaunchAsElevated(Action action, Func<List<string>> buildArgsForRelaunch, ForceOpsContext forceOpsContext, ILogger? logger = null, bool disableElevate = false) | ||
{ | ||
logger = logger ?? forceOpsContext.loggerFactory.CreateLogger<object>(); | ||
try | ||
{ | ||
action(); | ||
} | ||
catch (Exception ex) when (IsExceptionCausedByPermissions(ex) && !forceOpsContext.elevateUtils.IsProcessElevated() && !disableElevate) | ||
{ | ||
var args = buildArgsForRelaunch(); | ||
var childOutputFile = GetChildOutputFile(); | ||
args.AddRange(new[] { "2>&1", ">", childOutputFile }); | ||
logger.Information($"Unable to perform operation as an unelevated process. Retrying as elevated and logging to \"{childOutputFile}\"."); | ||
var childProcessExitCode = forceOpsContext.relaunchAsElevated.RelaunchAsElevated(args, childOutputFile); | ||
if (childProcessExitCode != 0) | ||
{ | ||
throw new AggregateException($"Child process failed with exit code {childProcessExitCode}."); | ||
} | ||
else | ||
{ | ||
logger.Information("Successfully deleted as admin"); | ||
} | ||
} | ||
} | ||
|
||
static string GetChildOutputFile() | ||
{ | ||
return Path.GetTempFileName(); | ||
} | ||
|
||
|
||
static bool IsExceptionCausedByPermissions(Exception ex) | ||
{ | ||
if (ex is FileNotFoundException) | ||
{ | ||
return false; | ||
} | ||
return ex is IOException || ex is UnauthorizedAccessException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using static ForceOps.Test.TestUtil; | ||
|
||
namespace ForceOps.Test; | ||
|
||
public class DeleteExampleTest | ||
{ | ||
[Fact] | ||
public void DeleteExampleWorks() | ||
{ | ||
var tempFilePath = GetTemporaryFileName(); | ||
using var launchedProcess = HoldLockOnFileUsingPowershell(tempFilePath); | ||
var process = Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = GetDeleteExampleExePath(), | ||
Arguments = tempFilePath, | ||
RedirectStandardOutput = true | ||
})!; | ||
|
||
var standardOutput = process.StandardOutput.ReadToEnd(); | ||
process.WaitForExit(); | ||
Assert.Equal(0, process.ExitCode); | ||
Assert.False(File.Exists(tempFilePath)); | ||
Assert.Contains("INF] Could not delete file", standardOutput); | ||
} | ||
|
||
private static string GetDeleteExampleExePath() | ||
{ | ||
var currentAssemblyPath = Assembly.GetExecutingAssembly().Location; | ||
var currentDirectory = Path.GetDirectoryName(currentAssemblyPath)!; | ||
var deleteExampleExe = Path.Combine(currentDirectory.Replace("ForceOps.Test", "ForceOps.DeleteExample"), "ForceOps.DeleteExample.exe"); | ||
|
||
if (!File.Exists(deleteExampleExe)) | ||
{ | ||
throw new FileNotFoundException($"{deleteExampleExe} did not exist."); | ||
} | ||
|
||
return deleteExampleExe; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters