new & opt.

- new: support suspend and WOL #172
- opt.: `execWithPwd` when cancel
- opt.: extentions
This commit is contained in:
lollipopkit
2023-09-25 18:51:14 +08:00
parent df84aeb8b2
commit 4d06a52e99
45 changed files with 251 additions and 176 deletions

View File

@@ -18,6 +18,7 @@ enum ShellFunc {
process,
shutdown,
reboot,
suspend,
;
String get flag {
@@ -32,6 +33,8 @@ enum ShellFunc {
return 'sd';
case ShellFunc.reboot:
return 'r';
case ShellFunc.suspend:
return 'sp';
}
}
@@ -51,10 +54,12 @@ enum ShellFunc {
return 'ShutDown';
case ShellFunc.reboot:
return 'Reboot';
case ShellFunc.suspend:
return 'Suspend';
}
}
String get cmd {
String get _cmd {
switch (this) {
case ShellFunc.status:
return '''
@@ -96,6 +101,13 @@ if [ "\$userId" = "0" ]; then
\treboot
else
\tsudo -S reboot
fi''';
case ShellFunc.suspend:
return '''
if [ "\$userId" = "0" ]; then
\tsystemctl suspend
else
\tsudo -S systemctl suspend
fi''';
}
}
@@ -123,7 +135,7 @@ userId=\$(id -u)
for (final func in values) {
sb.write('''
${func.name}() {
${func.cmd.split('\n').map((e) => '\t$e').join('\n')}
${func._cmd.split('\n').map((e) => '\t$e').join('\n')}
}
''');

View File

@@ -1,4 +1,3 @@
import '../../../core/extension/stringx.dart';
import '../../res/misc.dart';
class Conn {
@@ -22,10 +21,10 @@ Conn? parseConn(String raw) {
if (idx != '') {
final vals = idx.split(Miscs.numReg);
return Conn(
maxConn: vals[5].i,
active: vals[6].i,
passive: vals[7].i,
fail: vals[8].i,
maxConn: int.tryParse(vals[5]) ?? 0,
active: int.tryParse(vals[6]) ?? 0,
passive: int.tryParse(vals[7]) ?? 0,
fail: int.tryParse(vals[8]) ?? 0,
);
}
return null;

View File

@@ -70,7 +70,7 @@ class ServerPrivateInfo {
return data;
}
Server? get server => Providers.server.pick(spi: this);
Server? get server => Pros.server.pick(spi: this);
bool shouldReconnect(ServerPrivateInfo old) {
return id != old.id ||

View File

@@ -3,7 +3,6 @@ import 'dart:async';
import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/material.dart';
import 'package:toolbox/core/extension/ssh_client.dart';
import 'package:toolbox/core/extension/stringx.dart';
import 'package:toolbox/data/model/app/shell_func.dart';
import 'package:toolbox/data/model/docker/image.dart';
import 'package:toolbox/data/model/docker/ps.dart';
@@ -175,9 +174,10 @@ class DockerProvider extends ChangeNotifier {
// judge whether to use DOCKER_HOST
String _wrap(String cmd) {
final dockerHost = Stores.docker.fetch(hostId!);
cmd = 'export LANG=en_US.UTF-8 && $cmd';
if (dockerHost == null || dockerHost.isEmpty) {
return cmd.withLangExport;
return cmd;
}
return 'export DOCKER_HOST=$dockerHost && $cmd'.withLangExport;
return 'export DOCKER_HOST=$dockerHost && $cmd';
}
}

View File

@@ -291,7 +291,7 @@ class ServerProvider extends ChangeNotifier {
final localPath = joinPath(await Paths.doc, 'install.sh');
final file = File(localPath);
file.writeAsString(ShellFunc.allScript);
final sftp = Providers.sftp;
final sftp = Pros.sftp;
final completer = Completer();
sftp.add(
SftpReq(spi, installShellPath, localPath, SftpReqType.upload),

View File

@@ -31,12 +31,13 @@ class SnippetProvider extends ChangeNotifier {
}
void _addInternal() {
if (!Stores.setting.fTISBM.fetch() || _snippets.isNotEmpty) {
if (!Stores.first.iSSBM.fetch() ||
_snippets.any((e) => e.name == installSBM.name)) {
return;
}
_snippets.add(installSBM);
Stores.snippet.put(installSBM);
Stores.setting.fTISBM.put(false);
Stores.first.iSSBM.put(false);
}
void _updateTags() {

View File

@@ -7,8 +7,8 @@ import 'package:toolbox/data/provider/sftp.dart';
import 'package:toolbox/data/provider/snippet.dart';
import 'package:toolbox/locator.dart';
class Providers {
const Providers._();
class Pros {
const Pros._();
static final app = locator<AppProvider>();
static final debug = locator<DebugProvider>();

View File

@@ -1,4 +1,5 @@
import 'package:toolbox/data/store/docker.dart';
import 'package:toolbox/data/store/first.dart';
import 'package:toolbox/data/store/history.dart';
import 'package:toolbox/data/store/private_key.dart';
import 'package:toolbox/data/store/server.dart';
@@ -15,4 +16,5 @@ class Stores {
static final history = locator<HistoryStore>();
static final key = locator<PrivateKeyStore>();
static final snippet = locator<SnippetStore>();
static final first = locator<FirstStore>();
}

10
lib/data/store/first.dart Normal file
View File

@@ -0,0 +1,10 @@
import 'package:toolbox/core/persistant_store.dart';
/// It stores whether is the first time of some.
class FirstStore extends PersistentStore<bool> {
/// Add Snippet `Install ServerBoxMonitor`
late final iSSBM = StoreProperty(box, 'installMonitorSnippet', true);
/// Show tip of suspend
late final showSuspendTip = StoreProperty(box, 'showSuspendTip', true);
}

View File

@@ -205,7 +205,7 @@ class SettingStore extends PersistentStore {
false,
);
/// Only valid on iOS / Android
/// Only valid on iOS / Android / Windows
late final useBioAuth = StoreProperty(
box,
'useBioAuth',
@@ -213,15 +213,11 @@ class SettingStore extends PersistentStore {
);
// Never show these settings for users
// Guide for these settings:
// - key should start with `_` and be shorter as possible
//
// ------BEGIN------
/// Version of store db
late final storeVersion = StoreProperty(box, 'storeVersion', 0);
/// Whether is first time to add Snippet<Install ServerBoxMonitor>
late final fTISBM = StoreProperty(box, '_fTISBM', true);
// ------END------
}