mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
refactors (#539)
This commit is contained in:
@@ -7,8 +7,8 @@ import 'package:logging/logging.dart';
|
||||
import 'package:server_box/data/model/server/private_key_info.dart';
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/data/model/server/snippet.dart';
|
||||
import 'package:server_box/data/provider/base.dart';
|
||||
import 'package:server_box/data/res/misc.dart';
|
||||
import 'package:server_box/data/res/provider.dart';
|
||||
import 'package:server_box/data/res/rebuild.dart';
|
||||
import 'package:server_box/data/res/store.dart';
|
||||
|
||||
@@ -23,7 +23,7 @@ class Backup {
|
||||
// backup format version
|
||||
final int version;
|
||||
final String date;
|
||||
final List<ServerPrivateInfo> spis;
|
||||
final List<Spi> spis;
|
||||
final List<Snippet> snippets;
|
||||
final List<PrivateKeyInfo> keys;
|
||||
final Map<String, dynamic> container;
|
||||
@@ -177,7 +177,7 @@ class Backup {
|
||||
}
|
||||
}
|
||||
|
||||
Pros.reload();
|
||||
Provider.reload();
|
||||
RNodes.app.notify();
|
||||
|
||||
_logger.info('Restore success');
|
||||
|
||||
@@ -10,7 +10,7 @@ Backup _$BackupFromJson(Map<String, dynamic> json) => Backup(
|
||||
version: (json['version'] as num).toInt(),
|
||||
date: json['date'] as String,
|
||||
spis: (json['spis'] as List<dynamic>)
|
||||
.map((e) => ServerPrivateInfo.fromJson(e as Map<String, dynamic>))
|
||||
.map((e) => Spi.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
snippets: (json['snippets'] as List<dynamic>)
|
||||
.map((e) => Snippet.fromJson(e as Map<String, dynamic>))
|
||||
|
||||
@@ -15,7 +15,7 @@ import 'package:server_box/data/model/server/system.dart';
|
||||
import 'package:server_box/data/model/server/temp.dart';
|
||||
|
||||
class Server implements TagPickable {
|
||||
ServerPrivateInfo spi;
|
||||
Spi spi;
|
||||
ServerStatus status;
|
||||
SSHClient? client;
|
||||
ServerConn conn;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:server_box/data/model/server/custom.dart';
|
||||
import 'package:server_box/data/model/server/server.dart';
|
||||
import 'package:server_box/data/model/server/wol_cfg.dart';
|
||||
import 'package:server_box/data/res/provider.dart';
|
||||
import 'package:server_box/data/provider/server.dart';
|
||||
|
||||
import 'package:server_box/data/model/app/error.dart';
|
||||
|
||||
@@ -13,13 +14,13 @@ part 'server_private_info.g.dart';
|
||||
|
||||
/// In the first version, it's called `ServerPrivateInfo` which was designed to
|
||||
/// store the private information of a server.
|
||||
///
|
||||
///
|
||||
/// Some params named as `spi` in the codebase which is the abbreviation of `ServerPrivateInfo`.
|
||||
///
|
||||
/// Nowaday, more fields are added to this class, but the name is still the same.
|
||||
/// Nowaday, more fields are added to this class, and it's renamed to `Spi`.
|
||||
@JsonSerializable()
|
||||
@HiveType(typeId: 3)
|
||||
class ServerPrivateInfo {
|
||||
class Spi {
|
||||
@HiveField(0)
|
||||
final String name;
|
||||
@HiveField(1)
|
||||
@@ -58,7 +59,7 @@ class ServerPrivateInfo {
|
||||
|
||||
final String id;
|
||||
|
||||
const ServerPrivateInfo({
|
||||
const Spi({
|
||||
required this.name,
|
||||
required this.ip,
|
||||
required this.port,
|
||||
@@ -74,17 +75,22 @@ class ServerPrivateInfo {
|
||||
this.envs,
|
||||
}) : id = '$user@$ip:$port';
|
||||
|
||||
factory ServerPrivateInfo.fromJson(Map<String, dynamic> json) =>
|
||||
_$ServerPrivateInfoFromJson(json);
|
||||
factory Spi.fromJson(Map<String, dynamic> json) =>
|
||||
_$SpiFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$ServerPrivateInfoToJson(this);
|
||||
Map<String, dynamic> toJson() => _$SpiToJson(this);
|
||||
|
||||
@override
|
||||
String toString() => id;
|
||||
}
|
||||
|
||||
extension Spix on Spi {
|
||||
String toJsonString() => json.encode(toJson());
|
||||
|
||||
Server? get server => Pros.server.pick(spi: this);
|
||||
Server? get jumpServer => Pros.server.pick(id: jumpId);
|
||||
VNode<Server>? get server => ServerProvider.pick(spi: this);
|
||||
VNode<Server>? get jumpServer => ServerProvider.pick(id: jumpId);
|
||||
|
||||
bool shouldReconnect(ServerPrivateInfo old) {
|
||||
bool shouldReconnect(Spi old) {
|
||||
return id != old.id ||
|
||||
pwd != old.pwd ||
|
||||
keyId != old.keyId ||
|
||||
@@ -113,12 +119,7 @@ class ServerPrivateInfo {
|
||||
return (ip_, port_);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return id;
|
||||
}
|
||||
|
||||
static const example = ServerPrivateInfo(
|
||||
static const example = Spi(
|
||||
name: 'name',
|
||||
ip: 'ip',
|
||||
port: 22,
|
||||
|
||||
@@ -6,17 +6,17 @@ part of 'server_private_info.dart';
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
|
||||
class SpiAdapter extends TypeAdapter<Spi> {
|
||||
@override
|
||||
final int typeId = 3;
|
||||
|
||||
@override
|
||||
ServerPrivateInfo read(BinaryReader reader) {
|
||||
Spi read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return ServerPrivateInfo(
|
||||
return Spi(
|
||||
name: fields[0] as String,
|
||||
ip: fields[1] as String,
|
||||
port: fields[2] as int,
|
||||
@@ -34,7 +34,7 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, ServerPrivateInfo obj) {
|
||||
void write(BinaryWriter writer, Spi obj) {
|
||||
writer
|
||||
..writeByte(13)
|
||||
..writeByte(0)
|
||||
@@ -71,7 +71,7 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is ServerPrivateInfoAdapter &&
|
||||
other is SpiAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
@@ -80,8 +80,7 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
ServerPrivateInfo _$ServerPrivateInfoFromJson(Map<String, dynamic> json) =>
|
||||
ServerPrivateInfo(
|
||||
Spi _$SpiFromJson(Map<String, dynamic> json) => Spi(
|
||||
name: json['name'] as String,
|
||||
ip: json['ip'] as String,
|
||||
port: (json['port'] as num).toInt(),
|
||||
@@ -103,8 +102,7 @@ ServerPrivateInfo _$ServerPrivateInfoFromJson(Map<String, dynamic> json) =>
|
||||
),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$ServerPrivateInfoToJson(ServerPrivateInfo instance) =>
|
||||
<String, dynamic>{
|
||||
Map<String, dynamic> _$SpiToJson(Spi instance) => <String, dynamic>{
|
||||
'name': instance.name,
|
||||
'ip': instance.ip,
|
||||
'port': instance.port,
|
||||
|
||||
@@ -48,7 +48,7 @@ class Snippet implements TagPickable {
|
||||
|
||||
static final fmtFinder = RegExp(r'\$\{[^{}]+\}');
|
||||
|
||||
String fmtWithSpi(ServerPrivateInfo spi) {
|
||||
String fmtWithSpi(Spi spi) {
|
||||
return script.replaceAllMapped(
|
||||
fmtFinder,
|
||||
(match) {
|
||||
@@ -63,7 +63,7 @@ class Snippet implements TagPickable {
|
||||
|
||||
Future<void> runInTerm(
|
||||
Terminal terminal,
|
||||
ServerPrivateInfo spi, {
|
||||
Spi spi, {
|
||||
bool autoEnter = false,
|
||||
}) async {
|
||||
final argsFmted = fmtWithSpi(spi);
|
||||
@@ -159,12 +159,12 @@ class Snippet implements TagPickable {
|
||||
}
|
||||
|
||||
static final fmtArgs = {
|
||||
r'${host}': (ServerPrivateInfo spi) => spi.ip,
|
||||
r'${port}': (ServerPrivateInfo spi) => spi.port.toString(),
|
||||
r'${user}': (ServerPrivateInfo spi) => spi.user,
|
||||
r'${pwd}': (ServerPrivateInfo spi) => spi.pwd ?? '',
|
||||
r'${id}': (ServerPrivateInfo spi) => spi.id,
|
||||
r'${name}': (ServerPrivateInfo spi) => spi.name,
|
||||
r'${host}': (Spi spi) => spi.ip,
|
||||
r'${port}': (Spi spi) => spi.port.toString(),
|
||||
r'${user}': (Spi spi) => spi.user,
|
||||
r'${pwd}': (Spi spi) => spi.pwd ?? '',
|
||||
r'${id}': (Spi spi) => spi.id,
|
||||
r'${name}': (Spi spi) => spi.name,
|
||||
};
|
||||
|
||||
/// r'${ctrl+ad}' -> TerminalKey.control, a, d
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
part of 'worker.dart';
|
||||
|
||||
class SftpReq {
|
||||
final ServerPrivateInfo spi;
|
||||
final Spi spi;
|
||||
final String remotePath;
|
||||
final String localPath;
|
||||
final SftpReqType type;
|
||||
String? privateKey;
|
||||
ServerPrivateInfo? jumpSpi;
|
||||
Spi? jumpSpi;
|
||||
String? jumpPrivateKey;
|
||||
|
||||
SftpReq(
|
||||
|
||||
Reference in New Issue
Block a user