Skip to content

Commit

Permalink
Use Environment.Exit instead of returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Jul 2, 2024
1 parent 2922bf5 commit 85d9fbb
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/DownloadChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static void Download(ChatDownloadArgs inputOptions)
{
var progress = new CliTaskProgress(inputOptions.LogLevel);

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var downloadOptions = GetDownloadOptions(inputOptions, collisionHandler, progress);

var chatDownloader = new ChatDownloader(downloadOptions, progress);
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/DownloadClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static void Download(ClipDownloadArgs inputOptions)
FfmpegHandler.DetectFfmpeg(inputOptions.FfmpegPath, progress);
}

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var downloadOptions = GetDownloadOptions(inputOptions, collisionHandler, progress);

var clipDownloader = new ClipDownloader(downloadOptions, progress);
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/DownloadVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static void Download(VideoDownloadArgs inputOptions)

FfmpegHandler.DetectFfmpeg(inputOptions.FfmpegPath, progress);

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var downloadOptions = GetDownloadOptions(inputOptions, collisionHandler, progress);

var videoDownloader = new VideoDownloader(downloadOptions, progress);
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/MergeTs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static void Merge(TsMergeArgs inputOptions)

progress.LogInfo("The TS merger is experimental and is subject to change without notice in future releases.");

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var mergeOptions = GetMergeOptions(inputOptions, collisionHandler);

var tsMerger = new TsMerger(mergeOptions, progress);
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/RenderChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static void Render(ChatRenderArgs inputOptions)

FfmpegHandler.DetectFfmpeg(inputOptions.FfmpegPath, progress);

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var renderOptions = GetRenderOptions(inputOptions, collisionHandler, progress);

using var chatRenderer = new ChatRenderer(renderOptions, progress);
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/UpdateChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static void Update(ChatUpdateArgs inputOptions)
{
var progress = new CliTaskProgress(inputOptions.LogLevel);

var collisionHandler = new FileCollisionHandler(inputOptions);
var collisionHandler = new FileCollisionHandler(inputOptions, progress);
var updateOptions = GetUpdateOptions(inputOptions, collisionHandler, progress);

var chatUpdater = new ChatUpdater(updateOptions, progress);
Expand Down
21 changes: 15 additions & 6 deletions TwitchDownloaderCLI/Tools/FileCollisionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using TwitchDownloaderCLI.Models;
using TwitchDownloaderCLI.Modes.Arguments;
using TwitchDownloaderCore.Interfaces;
using TwitchDownloaderCore.Tools;

namespace TwitchDownloaderCLI.Tools
{
internal class FileCollisionHandler
{
private readonly IFileCollisionArgs _collisionArgs;
private readonly ITaskLogger _logger;

public FileCollisionHandler(IFileCollisionArgs collisionArgs)
public FileCollisionHandler(IFileCollisionArgs collisionArgs, ITaskLogger logger)
{
_collisionArgs = collisionArgs;
_logger = logger;
}

[return: MaybeNull]
public FileInfo HandleCollisionCallback(FileInfo fileInfo)
{
return _collisionArgs.OverwriteBehavior switch
{
OverwriteBehavior.Overwrite => fileInfo,
OverwriteBehavior.Exit => null,
OverwriteBehavior.Exit => Exit(fileInfo),
OverwriteBehavior.Rename => FilenameService.GetNonCollidingName(fileInfo),
OverwriteBehavior.Prompt => PromptUser(fileInfo),
_ => throw new ArgumentOutOfRangeException(nameof(_collisionArgs.OverwriteBehavior), _collisionArgs.OverwriteBehavior, null)
};
}

[return: MaybeNull]
private FileInfo Exit(FileInfo fileInfo)
{
_logger.LogInfo($"The file '{fileInfo.FullName}' already exists, exiting.");
Environment.Exit(1);
return null;
}

private static FileInfo PromptUser(FileInfo fileInfo)
{
// Deliberate use of Console.WriteLine instead of logger. Do not change.
Console.WriteLine($"The file '{fileInfo.FullName}' already exists.");

while (true)
Expand All @@ -43,7 +51,8 @@ private static FileInfo PromptUser(FileInfo fileInfo)
case "o" or "overwrite":
return fileInfo;
case "e" or "exit":
return null;
Environment.Exit(1);
break;
case "r" or "rename":
return FilenameService.GetNonCollidingName(fileInfo);
}
Expand Down

0 comments on commit 85d9fbb

Please sign in to comment.