Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Environment.Exit instead of returning null #1129

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading