mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
feat: ability to disable monitoring cmds (#840)
This commit is contained in:
@@ -3,11 +3,11 @@ import 'package:server_box/data/model/app/scripts/script_consts.dart';
|
||||
import 'package:server_box/data/model/app/scripts/shell_func.dart';
|
||||
|
||||
/// Abstract base class for platform-specific script builders
|
||||
abstract class ScriptBuilder {
|
||||
sealed class ScriptBuilder {
|
||||
const ScriptBuilder();
|
||||
|
||||
/// Generate a complete script for all shell functions
|
||||
String buildScript(Map<String, String>? customCmds);
|
||||
String buildScript(Map<String, String>? customCmds, [List<String>? disabledCmdTypes]);
|
||||
|
||||
/// Get the script file name for this platform
|
||||
String get scriptFileName;
|
||||
@@ -23,9 +23,6 @@ abstract class ScriptBuilder {
|
||||
|
||||
/// Get the script header for this platform
|
||||
String get scriptHeader;
|
||||
|
||||
/// Get the command divider for this platform
|
||||
String get cmdDivider => ScriptConstants.cmdDivider;
|
||||
}
|
||||
|
||||
/// Windows PowerShell script builder
|
||||
@@ -53,13 +50,19 @@ class WindowsScriptBuilder extends ScriptBuilder {
|
||||
@override
|
||||
String getCustomCmdsString(ShellFunc func, Map<String, String>? customCmds) {
|
||||
if (func == ShellFunc.status && customCmds != null && customCmds.isNotEmpty) {
|
||||
return '\n${customCmds.values.map((cmd) => '\t$cmd').join('\n')}';
|
||||
final sb = StringBuffer();
|
||||
for (final e in customCmds.entries) {
|
||||
final cmdDivider = ScriptConstants.getCustomCmdSeparator(e.key);
|
||||
sb.writeln(' Write-Host "$cmdDivider"');
|
||||
sb.writeln(' ${e.value}');
|
||||
}
|
||||
return '\n$sb';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@override
|
||||
String buildScript(Map<String, String>? customCmds) {
|
||||
String buildScript(Map<String, String>? customCmds, [List<String>? disabledCmdTypes]) {
|
||||
final sb = StringBuffer();
|
||||
sb.write(scriptHeader);
|
||||
|
||||
@@ -69,7 +72,7 @@ class WindowsScriptBuilder extends ScriptBuilder {
|
||||
|
||||
sb.write('''
|
||||
function ${func.name} {
|
||||
${_getWindowsCommand(func).split('\n').map((e) => e.isEmpty ? '' : ' $e').join('\n')}$customCmdsStr
|
||||
${_getWindowsCommand(func, disabledCmdTypes).split('\n').map((e) => e.isEmpty ? '' : ' $e').join('\n')}$customCmdsStr
|
||||
}
|
||||
|
||||
''');
|
||||
@@ -92,14 +95,20 @@ switch (\$args[0]) {
|
||||
}
|
||||
|
||||
/// Get Windows-specific command for a shell function
|
||||
String _getWindowsCommand(ShellFunc func) => switch (func) {
|
||||
ShellFunc.status => WindowsStatusCmdType.values.map((e) => e.cmd).join(cmdDivider),
|
||||
String _getWindowsCommand(ShellFunc func, [List<String>? disabledCmdTypes]) => switch (func) {
|
||||
ShellFunc.status => _getWindowsStatusCommand(disabledCmdTypes: disabledCmdTypes ?? []),
|
||||
ShellFunc.process => 'Get-Process | Select-Object ProcessName, Id, CPU, WorkingSet | ConvertTo-Json',
|
||||
ShellFunc.shutdown => 'Stop-Computer -Force',
|
||||
ShellFunc.reboot => 'Restart-Computer -Force',
|
||||
ShellFunc.suspend =>
|
||||
'Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Application]::SetSuspendState(\'Suspend\', \$false, \$false)',
|
||||
};
|
||||
|
||||
/// Get Windows status command with command-specific separators
|
||||
String _getWindowsStatusCommand({required List<String> disabledCmdTypes}) {
|
||||
final cmdTypes = WindowsStatusCmdType.values.where((e) => !disabledCmdTypes.contains(e.name));
|
||||
return cmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight(); // Remove trailing divider
|
||||
}
|
||||
}
|
||||
|
||||
/// Unix shell script builder
|
||||
@@ -129,13 +138,19 @@ chmod 755 $scriptPath
|
||||
@override
|
||||
String getCustomCmdsString(ShellFunc func, Map<String, String>? customCmds) {
|
||||
if (func == ShellFunc.status && customCmds != null && customCmds.isNotEmpty) {
|
||||
return '$cmdDivider\n\t${customCmds.values.join(cmdDivider)}';
|
||||
final sb = StringBuffer();
|
||||
for (final e in customCmds.entries) {
|
||||
final cmdDivider = ScriptConstants.getCustomCmdSeparator(e.key);
|
||||
sb.writeln('echo "$cmdDivider"');
|
||||
sb.writeln(e.value);
|
||||
}
|
||||
return '\n$sb';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@override
|
||||
String buildScript(Map<String, String>? customCmds) {
|
||||
String buildScript(Map<String, String>? customCmds, [List<String>? disabledCmdTypes]) {
|
||||
final sb = StringBuffer();
|
||||
sb.write(scriptHeader);
|
||||
// Write each function
|
||||
@@ -143,7 +158,7 @@ chmod 755 $scriptPath
|
||||
final customCmdsStr = getCustomCmdsString(func, customCmds);
|
||||
sb.write('''
|
||||
${func.name}() {
|
||||
${_getUnixCommand(func).split('\n').map((e) => '\t$e').join('\n')}
|
||||
${_getUnixCommand(func, disabledCmdTypes).split('\n').map((e) => '\t$e').join('\n')}
|
||||
$customCmdsStr
|
||||
}
|
||||
|
||||
@@ -168,27 +183,24 @@ esac''');
|
||||
}
|
||||
|
||||
/// Get Unix-specific command for a shell function
|
||||
String _getUnixCommand(ShellFunc func) {
|
||||
switch (func) {
|
||||
case ShellFunc.status:
|
||||
return _getUnixStatusCommand();
|
||||
case ShellFunc.process:
|
||||
return _getUnixProcessCommand();
|
||||
case ShellFunc.shutdown:
|
||||
return _getUnixShutdownCommand();
|
||||
case ShellFunc.reboot:
|
||||
return _getUnixRebootCommand();
|
||||
case ShellFunc.suspend:
|
||||
return _getUnixSuspendCommand();
|
||||
}
|
||||
String _getUnixCommand(ShellFunc func, [List<String>? disabledCmdTypes]) {
|
||||
return switch (func) {
|
||||
ShellFunc.status => _getUnixStatusCommand(disabledCmdTypes: disabledCmdTypes ?? []),
|
||||
ShellFunc.process => _getUnixProcessCommand(),
|
||||
ShellFunc.shutdown => _getUnixShutdownCommand(),
|
||||
ShellFunc.reboot => _getUnixRebootCommand(),
|
||||
ShellFunc.suspend => _getUnixSuspendCommand(),
|
||||
};
|
||||
}
|
||||
|
||||
/// Get Unix status command with OS detection
|
||||
String _getUnixStatusCommand() {
|
||||
// Generate command lists for better readability
|
||||
final linuxCommands = StatusCmdType.values.map((e) => e.cmd).join(cmdDivider);
|
||||
String _getUnixStatusCommand({required List<String> disabledCmdTypes}) {
|
||||
// Generate command lists with command-specific separators, filtering disabled commands
|
||||
final filteredLinuxCmdTypes = StatusCmdType.values.where((e) => !disabledCmdTypes.contains(e.name));
|
||||
final linuxCommands = filteredLinuxCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
||||
|
||||
final bsdCommands = BSDStatusCmdType.values.map((e) => e.cmd).join(cmdDivider);
|
||||
final filteredBsdCmdTypes = BSDStatusCmdType.values.where((e) => !disabledCmdTypes.contains(e.name));
|
||||
final bsdCommands = filteredBsdCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
||||
|
||||
return '''
|
||||
if [ "\$macSign" = "" ] && [ "\$bsdSign" = "" ]; then
|
||||
|
||||
Reference in New Issue
Block a user