Skip to content

Commit

Permalink
fix connect token, parse room name correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
flowbehappy committed Jul 29, 2023
1 parent 7e4ceea commit 6381825
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 45 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ app.*.map.json
ios/build

# fvm
.fvm/flutter_sdk
.fvm/flutter_sdk


./release

132 changes: 89 additions & 43 deletions lib/utils/tidb.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import 'package:args/args.dart';

ArgParser tokenParser = createTokenParser();

ArgParser createTokenParser() {
var p = ArgParser();
p.addOption("host", abbr: "h");
p.addOption("port", abbr: "P");
p.addOption("user", abbr: "u");
p.addOption("password", abbr: "p");
p.addOption("room");
p.addOption("room_name");

// Options we don't use yet. But we has to define too. Otherwise parser will complain
p.addOption("connect-timeout");
p.addOption("database", abbr: "D");
p.addOption("ssl-mode");
p.addOption("ssl-ca");

p.allowsAnything;
return p;
}

String toConnectionToken(
String host,
int port,
Expand All @@ -6,62 +29,85 @@ String toConnectionToken(
String roomId,
String roomName,
) {
return "mysql -u '$userName' -h $host -P 4000 -p$password --room $roomId --room_name $roomName";
return "mysql -u '$userName' -h '$host' -P $port -p$password --room '$roomId' --room_name '$roomName'";
}

// Return (host, port, user, password, tableId).
// If port == 0, means "host" is the error message.
(String, int, String, String, String, String) parseTiDBConnectionToken(
String text) {
if (text.isEmpty) {
String token) {
if (token.isEmpty) {
return ("", 0, "", "", "", "");
}
// token =
// "mysql --connect-timeout 15 -u '3DQS6CX9AX9qY51.root' -h gateway01.eu-central-1.prod.aws.tidbcloud.com -P 4000 -D test --ssl-mode=VERIFY_IDENTITY --ssl-ca=/etc/ssl/cert.pem -piAaVmmI4dypKbEgY";
try {
final tokens = tokenize(token);
final r = tokenParser.parse(tokens);
final host = r["host"];
final port = int.parse(r["port"]);
final user = r["user"];
final password = r["password"];
final roomId = r["room"] ?? "";
final roomName = r["room_name"] ?? "";

text = text.replaceFirst(" -p", " -p ");
final options = text.split(" ");
var nextOpts = List.from(options);
nextOpts.removeAt(0);
nextOpts.add("");
String user = "";
String host = "";
int port = 0;
String password = "";
String msgRoom = "";
String msgRoomName = "";
return (host, port, user, password, roomId, roomName);
} catch (e) {
return (e.toString(), 0, "", "", "", "");
}
}

try {
for (int i = 0; i < options.length; i += 1) {
final opt = options[i];
final nextOpt = nextOpts[i];
switch (opt) {
case "-u":
case "--user":
user = nextOpt.replaceAll("'", "");
user = user.replaceAll('"', "");
user = user.replaceAll("'", "");
break;
case "-h":
case "--host":
host = nextOpt;
break;
case "-P":
case "--port":
port = int.parse(nextOpt);
break;
case "-p":
case "--password":
password = nextOpt;
List<String> tokenize(String input) {
if (input.isEmpty) {
//no command? no string
return [];
}

var result = List<String>.empty(growable: true);

var current = "";

String? inQuote;
bool lastTokenHasBeenQuoted = false;

for (int index = 0; index < input.length; index++) {
final token = input[index];

if (inQuote != null) {
if (token == inQuote) {
lastTokenHasBeenQuoted = true;
inQuote = null;
} else {
current += token;
}
} else {
switch (token) {
case "'": // '
case '"': // ""
inQuote = token;
continue;

case " ": // space
if (lastTokenHasBeenQuoted || current.isNotEmpty) {
result.add(current);
current = "";
}
break;
case "--room":
msgRoom = nextOpt;
case "--room_name":
msgRoomName = nextOpt;

default:
current += token;
lastTokenHasBeenQuoted = false;
}
}
}

return (host, port, user, password, msgRoom, msgRoomName);
} catch (e) {
return (e.toString(), 0, "", "", "", "");
if (lastTokenHasBeenQuoted || current.isNotEmpty) {
result.add(current);
}

if (inQuote != null) {
throw Exception("Unbalanced quote $inQuote in input: $input");
}

return result;
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ packages:
source: hosted
version: "3.3.7"
args:
dependency: transitive
dependency: "direct main"
description:
name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies:
timeago: ^3.5.0
stream_transform: ^2.1.0
json_annotation: ^4.8.1
args: ^2.4.2

dev_dependencies:
flutter_test:
Expand Down
Binary file removed release/Moyubie-Installer.dmg
Binary file not shown.

0 comments on commit 6381825

Please sign in to comment.