-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandInterceptor.cs
More file actions
100 lines (79 loc) · 4.04 KB
/
CommandInterceptor.cs
File metadata and controls
100 lines (79 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
namespace WebDev.Tool;
using Spectre.Console.Cli;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WebDev.Tool.Helper.Internal;
public class CommandInterceptor(IDebugOutputHelper _debugOutputHelper, IEnvironmentHelper _environmentHelper) : ICommandInterceptor
{
public void Intercept(CommandContext context, CommandSettings settings)
{
var debugOutputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ".debug_enabled");
bool debugModeEnabled = File.Exists(debugOutputFile);
// If the command has a log level setting, set the log level
if (debugModeEnabled || (settings is LogCommandSettings logSettings && logSettings.Debug))
{
_debugOutputHelper.EnableDebugOutput();
}
else
{
_debugOutputHelper.DisableDebugOutput();
}
_debugOutputHelper.WriteInfoOutput("Intercepting command: " + context.Name, this);
var commandTokens = GetInvokedCommandTokens(Program.ProgramArgs);
_debugOutputHelper.WriteInfoOutput("Command tokens: " + string.Join(" ", commandTokens), this);
if (commandTokens.Count == 2) {
// If the command can run in the current env, return (it is either allowed to run via property or not explicitly set)
EnsureCommandCanRunInCurrentEnv(commandTokens[0] + ":" + context.Name);
} else {
EnsureCommandCanRunInCurrentEnv(context.Name);
}
}
private List<string> GetInvokedCommandTokens(IReadOnlyList<string> args)
{
// Global args start with '-' (e.g. --debug, --no-header, etc.). Commands are the first non-option tokens.
// We only need the first two tokens: <branch> [subcommand] OR <command>.
var tokens = args
.Where(a => !string.IsNullOrWhiteSpace(a))
.Where(a => !a.StartsWith("-", StringComparison.Ordinal))
.Take(2)
.ToList();
return tokens;
}
private bool BranchCanRunInCurrentEnv(string name)
{
var runOnlyInDevcontainer = CommandExtensions.IsRunOnlyInDevcontainer(name, false);
var runOnlyOnHost = CommandExtensions.IsRunOnlyOnHost(name, false);
_debugOutputHelper.WriteInfoOutput("Ensuring branch can run in current env: " + name + ", runOnlyInDevcontainer: " + runOnlyInDevcontainer + ", runOnlyOnHost: " + runOnlyOnHost, this);
if (IsAvailableInCurrentEnv(runOnlyInDevcontainer, runOnlyOnHost))
{
_debugOutputHelper.WriteInfoOutput("Branch can run in current env: " + name, this);
return true;
}
var expectedEnv = _environmentHelper.IsRunningInDevContainer() ? "on the host" : "in the devcontainer";
throw new InvalidOperationException($"The branch {name} cannot be executed here. It can only run {expectedEnv}.");
}
private void EnsureCommandCanRunInCurrentEnv(string name)
{
var runOnlyInDevcontainer = CommandExtensions.IsRunOnlyInDevcontainer(name);
var runOnlyOnHost = CommandExtensions.IsRunOnlyOnHost(name);
_debugOutputHelper.WriteInfoOutput("Ensuring command can run in current env: " + name + ", runOnlyInDevcontainer: " + runOnlyInDevcontainer + ", runOnlyOnHost: " + runOnlyOnHost, this);
if (IsAvailableInCurrentEnv(runOnlyInDevcontainer, runOnlyOnHost))
{
_debugOutputHelper.WriteInfoOutput("Command can run in current env: " + name, this);
return;
}
var expectedEnv = _environmentHelper.IsRunningInDevContainer() ? "on the host" : "in the devcontainer";
throw new InvalidOperationException($"The command {name} cannot be executed here. It can only run {expectedEnv}.");
}
private bool IsAvailableInCurrentEnv(bool runOnlyInDevcontainer, bool runOnlyOnHost)
{
// If no restriction is set, it's available everywhere.
if (!runOnlyInDevcontainer && !runOnlyOnHost)
{
return true;
}
return _environmentHelper.IsRunningInDevContainer() ? runOnlyInDevcontainer : runOnlyOnHost;
}
}