new: server tab process

This commit is contained in:
lollipopkit
2023-06-21 17:47:57 +08:00
parent 625bc280f0
commit 3a8e189dd7
14 changed files with 111 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ enum ServerTabMenuType {
snippet,
pkg,
docker,
process,
edit;
IconData get icon {
@@ -20,6 +21,8 @@ enum ServerTabMenuType {
return Icons.view_agenda;
case ServerTabMenuType.edit:
return Icons.edit;
case ServerTabMenuType.process:
return Icons.list_alt_outlined;
}
}
@@ -35,6 +38,8 @@ enum ServerTabMenuType {
return 'Docker';
case ServerTabMenuType.edit:
return s.edit;
case ServerTabMenuType.process:
return s.process;
}
}

View File

@@ -0,0 +1,41 @@
class AppShellFunc {
final String name;
final String cmd;
final String flag;
const AppShellFunc(this.name, this.cmd, this.flag);
}
typedef AppShellFuncs = List<AppShellFunc>;
extension AppShellFuncsExt on AppShellFuncs {
String get generate {
final sb = StringBuffer();
// Write each func
for (final func in this) {
sb.write('''
${func.name}() {
${func.cmd}
}
''');
}
// Write switch case
sb.write('case \$1 in\n');
for (final func in this) {
sb.write('''
'-${func.flag}')
${func.name}
;;
''');
}
sb.write('''
*)
echo "Invalid argument \$1"
;;
esac
''');
return sb.toString();
}
}

View File

@@ -240,7 +240,8 @@ class ServerProvider extends BusyProvider {
if (s.client == null) return;
// run script to get server status
raw = await s.client!.run("sh $shellPath").string;
raw =
await s.client!.run("sh $shellPath -${shellFuncStatus.flag}").string;
segments = raw.split(seperator).map((e) => e.trim()).toList();
if (raw.isEmpty || segments.length != CmdType.values.length) {
s.state = ServerState.failed;

View File

@@ -1,3 +1,4 @@
import '../model/app/shell_func.dart';
import 'build_data.dart';
const seperator = 'SrvBox';
@@ -36,11 +37,37 @@ const _cmdList = [
'cat /etc/redhat-release',
];
final shellFuncStatus = AppShellFunc(
'status',
_cmdList.join('\necho $seperator\n'),
's',
);
// Check if `htop` is installed.
// Then app open SSH term and use `htop` or `ps` to see process.
const shellFuncProcess = AppShellFunc(
'process',
'''
if command -v htop &> /dev/null
then
htop
else
top
fi
''',
'p',
);
final _generated = [
shellFuncStatus,
shellFuncProcess,
].generate;
final shellCmd = """
# Script for app `${BuildData.name} v${BuildData.build}`
# Script for app `${BuildData.name} v1.0.${BuildData.build}`
# Delete this file while app is running will cause app crash
${_cmdList.join('\necho $seperator\n')}
$_generated
""";
final installShellCmd = "mkdir -p $serverBoxDir && "