fix SSHClient.exec

This commit is contained in:
lollipopkit
2022-12-11 14:08:08 +08:00
parent d43d251d92
commit 78b46998d1
10 changed files with 57 additions and 45 deletions

View File

@@ -15,20 +15,34 @@ extension SSHClientX on SSHClient {
{OnStd? onStderr, OnStd? onStdout, OnStdin? stdin}) async { {OnStd? onStderr, OnStd? onStdout, OnStdin? stdin}) async {
final session = await execute(cmd); final session = await execute(cmd);
if (onStderr != null) { final stdoutDone = Completer<void>();
await for (final data in session.stderr) { final stderrDone = Completer<void>();
onStderr(data.string, session.stdin);
}
}
if (onStdout != null) { if (onStdout != null) {
await for (final data in session.stdout) { session.stdout.listen(
onStdout(data.string, session.stdin); (e) => onStdout(e.string, session.stdin),
} onDone: stdoutDone.complete,
);
} else {
stdoutDone.complete();
} }
if (onStderr != null) {
session.stderr.listen(
(e) => onStderr(e.string, session.stdin),
onDone: stderrDone.complete,
);
} else {
stderrDone.complete();
}
if (stdin != null) { if (stdin != null) {
stdin(session.stdin); stdin(session.stdin);
} }
await stdoutDone.future;
await stderrDone.future;
session.close(); session.close();
return session.exitCode; return session.exitCode;
} }

View File

@@ -99,7 +99,7 @@ class AptProvider extends BusyProvider {
onStdout: (data, sink) { onStdout: (data, sink) {
updateLog = (updateLog ?? '') + data; updateLog = (updateLog ?? '') + data;
notifyListeners(); notifyListeners();
onUpdate!(); if (onUpdate != null) onUpdate!();
}, },
); );
return await client return await client
@@ -120,13 +120,13 @@ class AptProvider extends BusyProvider {
await client!.exec( await client!.exec(
_wrap(upgradeCmd), _wrap(upgradeCmd),
onStderr: (data, sink) => _onPwd(data, sink), onStderr: _onPwd,
onStdout: (log, sink) { onStdout: (log, sink) {
if (lastLog == log.trim()) return; if (lastLog == log.trim()) return;
upgradeLog = (upgradeLog ?? '') + log; upgradeLog = (upgradeLog ?? '') + log;
lastLog = log.trim(); lastLog = log.trim();
notifyListeners(); notifyListeners();
onUpgrade!(); if (onUpgrade != null) onUpgrade!();
}, },
); );

View File

@@ -30,7 +30,6 @@ class DockerProvider extends BusyProvider {
DockerErr? error; DockerErr? error;
PwdRequestFunc? onPwdReq; PwdRequestFunc? onPwdReq;
String? hostId; String? hostId;
String? runLog;
bool isRequestingPwd = false; bool isRequestingPwd = false;
void init(SSHClient client, String userName, PwdRequestFunc onPwdReq, void init(SSHClient client, String userName, PwdRequestFunc onPwdReq,
@@ -44,7 +43,7 @@ class DockerProvider extends BusyProvider {
void clear() { void clear() {
client = userName = error = items = version = edition = onPwdReq = null; client = userName = error = items = version = edition = onPwdReq = null;
isRequestingPwd = false; isRequestingPwd = false;
hostId = runLog = null; hostId = null;
} }
Future<void> refresh() async { Future<void> refresh() async {
@@ -124,14 +123,8 @@ class DockerProvider extends BusyProvider {
final code = await client!.exec( final code = await client!.exec(
_wrapHost(cmd), _wrapHost(cmd),
onStderr: _onPwd, onStderr: _onPwd,
onStdout: (data, _) {
runLog = '$runLog$data';
notifyListeners();
},
); );
runLog = null;
if (code != 0) { if (code != 0) {
setBusyState(false); setBusyState(false);
return DockerErr(type: DockerErrType.unknown, message: errs.join('\n')); return DockerErr(type: DockerErrType.unknown, message: errs.join('\n'));

View File

@@ -2,9 +2,9 @@
class BuildData { class BuildData {
static const String name = "ServerBox"; static const String name = "ServerBox";
static const int build = 166; static const int build = 169;
static const String engine = static const String engine =
"Flutter 3.3.9 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision b8f7f1f986 (12 days ago) • 2022-11-23 06:43:51 +0900\nEngine • revision 8f2221fbef\nTools • Dart 2.18.5 • DevTools 2.15.0\n"; "Flutter 3.3.9 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision b8f7f1f986 (3 weeks ago) • 2022-11-23 06:43:51 +0900\nEngine • revision 8f2221fbef\nTools • Dart 2.18.5 • DevTools 2.15.0\n";
static const String buildAt = "2022-12-04 21:57:10.591121"; static const String buildAt = "2022-12-11 12:36:17.737879";
static const int modifications = 2; static const int modifications = 8;
} }

View File

@@ -142,6 +142,7 @@ class MessageLookup extends MessageLookupByLibrary {
"invalidVersion": "invalidVersion":
MessageLookupByLibrary.simpleMessage("Invalid version"), MessageLookupByLibrary.simpleMessage("Invalid version"),
"invalidVersionHelp": m6, "invalidVersionHelp": m6,
"isBusy": MessageLookupByLibrary.simpleMessage("Is busy now"),
"keepForeground": "keepForeground":
MessageLookupByLibrary.simpleMessage("Keep app foreground!"), MessageLookupByLibrary.simpleMessage("Keep app foreground!"),
"keyAuth": MessageLookupByLibrary.simpleMessage("Key Auth"), "keyAuth": MessageLookupByLibrary.simpleMessage("Key Auth"),

View File

@@ -127,6 +127,7 @@ class MessageLookup extends MessageLookupByLibrary {
"invalidJson": MessageLookupByLibrary.simpleMessage("无效的json存在格式问题"), "invalidJson": MessageLookupByLibrary.simpleMessage("无效的json存在格式问题"),
"invalidVersion": MessageLookupByLibrary.simpleMessage("不支持的版本"), "invalidVersion": MessageLookupByLibrary.simpleMessage("不支持的版本"),
"invalidVersionHelp": m6, "invalidVersionHelp": m6,
"isBusy": MessageLookupByLibrary.simpleMessage("当前正忙"),
"keepForeground": MessageLookupByLibrary.simpleMessage("请保持应用处于前台!"), "keepForeground": MessageLookupByLibrary.simpleMessage("请保持应用处于前台!"),
"keyAuth": MessageLookupByLibrary.simpleMessage("公钥认证"), "keyAuth": MessageLookupByLibrary.simpleMessage("公钥认证"),
"lastTry": MessageLookupByLibrary.simpleMessage("最后尝试"), "lastTry": MessageLookupByLibrary.simpleMessage("最后尝试"),

View File

@@ -1450,6 +1450,16 @@ class S {
args: [], args: [],
); );
} }
/// `Is busy now`
String get isBusy {
return Intl.message(
'Is busy now',
name: 'isBusy',
desc: '',
args: [],
);
}
} }
class AppLocalizationDelegate extends LocalizationsDelegate<S> { class AppLocalizationDelegate extends LocalizationsDelegate<S> {

View File

@@ -138,5 +138,6 @@
"dockerImage": "Image", "dockerImage": "Image",
"dockerContainerName": "Container name", "dockerContainerName": "Container name",
"extraArgs": "Extra args", "extraArgs": "Extra args",
"preview": "Preview" "preview": "Preview",
"isBusy": "Is busy now"
} }

View File

@@ -138,5 +138,6 @@
"dockerImage": "镜像", "dockerImage": "镜像",
"dockerContainerName": "容器名", "dockerContainerName": "容器名",
"extraArgs": "额外参数", "extraArgs": "额外参数",
"preview": "预览" "preview": "预览",
"isBusy": "当前正忙"
} }

View File

@@ -260,22 +260,10 @@ class _DockerManagePageState extends State<DockerManagePage> {
Widget _buildLoading(DockerProvider docker) { Widget _buildLoading(DockerProvider docker) {
if (docker.isBusy) { if (docker.isBusy) {
final runLog = return const Padding(
docker.runLog == null ? const SizedBox() : Text(docker.runLog!); padding: EdgeInsets.all(17),
return Padding( child: Center(
padding: const EdgeInsets.all(17), child: CircularProgressIndicator(),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
runLog,
SizedBox(
width: docker.runLog == null ? 0 : 17,
),
const Center(
child: CircularProgressIndicator(),
),
],
), ),
); );
} }
@@ -380,15 +368,14 @@ class _DockerManagePageState extends State<DockerManagePage> {
return ListTile( return ListTile(
title: Text(item.image), title: Text(item.image),
subtitle: Text(item.status), subtitle: Text(item.status),
trailing: docker.isBusy trailing:
? const CircularProgressIndicator() _buildMoreBtn(item.running, item.containerId, docker.isBusy),
: _buildMoreBtn(item.running, item.containerId),
); );
}).toList(), }).toList(),
); );
} }
Widget _buildMoreBtn(bool running, String containerId) { Widget _buildMoreBtn(bool running, String containerId, bool busy) {
final item = running ? DockerMenuItems.stop : DockerMenuItems.start; final item = running ? DockerMenuItems.stop : DockerMenuItems.start;
return buildPopuopMenu( return buildPopuopMenu(
items: [ items: [
@@ -402,6 +389,10 @@ class _DockerManagePageState extends State<DockerManagePage> {
), ),
], ],
onSelected: (value) { onSelected: (value) {
if (busy) {
showSnackBar(context, Text(s.isBusy));
return;
}
final item = value as DropdownBtnItem; final item = value as DropdownBtnItem;
switch (item) { switch (item) {
case DockerMenuItems.rm: case DockerMenuItems.rm: