Skip to content

Commit 12684a2

Browse files
committed
reorganize, modularize, add spinner
1 parent 2f86f24 commit 12684a2

8 files changed

Lines changed: 214 additions & 77 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# https://dart.dev/guides/libraries/private-files
22
# Created by `dart pub`
33
.dart_tool/
4-
test
4+
test
5+
bin/s5_deploy.exe

bin/s5_deploy.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:s5_deploy/s5_deploy.dart';
2+
3+
void main(List<String> args) async {
4+
s5Deploy(args);
5+
}

lib/functions/cli.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:args/args.dart';
2+
import 'package:dcli/dcli.dart';
3+
4+
ArgParser defineArguments() {
5+
// Define args
6+
var topParser = ArgParser();
7+
8+
// -V || --version
9+
topParser.addFlag('version',
10+
abbr: 'V',
11+
defaultsTo: false,
12+
negatable: false,
13+
help: 'Gets the version number of package.');
14+
15+
// -h || --help
16+
topParser.addFlag('help',
17+
abbr: 'h',
18+
defaultsTo: false,
19+
negatable: false,
20+
help: 'Print help dialoge.');
21+
22+
// --reset
23+
topParser.addFlag('reset',
24+
defaultsTo: false,
25+
negatable: false,
26+
help: 'Resets local node ${red("BE CAREFUL")}');
27+
28+
// -s || --server
29+
topParser.addOption('node',
30+
abbr: 'n',
31+
defaultsTo: 'https://s5.ninja',
32+
help: 'Which S5 node to deploy to.');
33+
34+
return topParser;
35+
}
36+
37+
void printUsage(ArgParser parser) {
38+
print(('Usage:'));
39+
print(('s5_deploy ./file/or/folder'));
40+
print((parser.usage));
41+
}

lib/functions/s5.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:dcli/dcli.dart';
2+
import 'package:hive/hive.dart';
3+
import 'package:path/path.dart';
4+
import 'package:s5_deploy/definitons/logging.dart';
5+
import 'package:s5/s5.dart';
6+
7+
void initS5(String nodeURL, String dbPath, String logPath) async {
8+
final nowInMilliseconds = DateTime.now().millisecondsSinceEpoch;
9+
final lastEightDigits = nowInMilliseconds
10+
.toString()
11+
.substring(nowInMilliseconds.toString().length - 8);
12+
Hive.init(dbPath);
13+
final s5 = await S5.create(
14+
logger: FileLogger(file: join(logPath, 'log-$lastEightDigits.txt')));
15+
if (!s5.hasIdentity) {
16+
final seed = s5.generateSeedPhrase();
17+
print("");
18+
print(
19+
"This is your ${green("seed")}. If you want to keep updating this registry entry in the future you ${green("MUST")} have this seed");
20+
print(green(seed));
21+
var confirmed = confirm('Have you written this down:', defaultValue: true);
22+
if (!confirmed) {
23+
print(
24+
"No like I'm ${red("serious")}, write this down. But to each their own.");
25+
}
26+
await s5.recoverIdentityFromSeedPhrase(seed);
27+
await s5.registerOnNewStorageService(
28+
nodeURL,
29+
);
30+
}
31+
}

lib/functions/spinner.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:cli_spin/cli_spin.dart';
2+
3+
CliSpin spinStart(String text) {
4+
return CliSpin(
5+
text: text,
6+
spinner: CliSpinners.dots,
7+
).start(); // Chaining methods
8+
}

lib/s5_deploy.dart

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
import 'dart:io';
22

33
import 'package:args/args.dart';
4+
import 'package:cli_spin/cli_spin.dart';
45
import 'package:dcli/dcli.dart';
5-
import 'package:hive/hive.dart';
66
import 'package:path/path.dart';
7-
import 'package:s5/s5.dart';
87
import 'package:s5_deploy/definitons/constants.dart';
8+
import 'package:s5_deploy/functions/cli.dart';
99
import 'package:s5_deploy/functions/filesystem.dart';
10-
import 'package:s5_deploy/definitons/logging.dart';
10+
import 'package:s5_deploy/functions/s5.dart';
11+
import 'package:s5_deploy/functions/spinner.dart';
1112
import 'package:xdg_directories/xdg_directories.dart';
1213

13-
void main(List<String> args) async {
14+
void s5Deploy(List<String> args) async {
1415
// define paths
1516
final dbPath = join(configHome.path, 's5_deploy', 'db');
1617
final logPath = join(configHome.path, 's5_deploy', 'logs');
1718
await ensureDirExistence(Directory(dbPath));
1819
await ensureDirExistence(Directory(logPath));
1920

20-
// Define args
21-
var topParser = ArgParser();
22-
23-
// -V || --version
24-
topParser.addFlag('version',
25-
abbr: 'V',
26-
defaultsTo: false,
27-
negatable: false,
28-
help: 'Gets the version number of package.');
29-
30-
// -h || --help
31-
topParser.addFlag('help',
32-
abbr: 'h',
33-
defaultsTo: false,
34-
negatable: false,
35-
help: 'Print help dialoge.');
36-
37-
// --reset
38-
topParser.addFlag('reset',
39-
defaultsTo: false,
40-
negatable: false,
41-
help: 'Resets local node ${red("BE CAREFUL")}');
21+
// Define arguments
22+
ArgParser topParser = defineArguments();
4223

4324
// Process args
44-
var results = topParser.parse(args);
25+
ArgResults results;
26+
try {
27+
results = topParser.parse(args);
28+
} catch (e) {
29+
print(red(e.toString()));
30+
printUsage(topParser);
31+
exit(1);
32+
}
4533

4634
// -V || --version
4735
var versionCheck = results['version'] as bool;
@@ -51,46 +39,31 @@ void main(List<String> args) async {
5139
}
5240

5341
// -h || --help || malformed path
54-
var helpCheck = results['help'] as bool;
42+
bool helpCheck = results['help'] as bool;
5543
if (results.rest.length != 1 || helpCheck) {
5644
if (!helpCheck) {
5745
print(red('You must pass the name of the file/directory to upload.'));
5846
}
59-
print(green('Usage:'));
60-
print(green('s5_deploy ./file/or/folder'));
61-
print(green(topParser.usage));
47+
printUsage(topParser);
6248
exit(1);
6349
}
6450

51+
// begin spinner
52+
CliSpin spinner = spinStart("Setting things up...");
53+
6554
// --reset
6655
var resetCheck = results['reset'] as bool;
6756
if (resetCheck) {
6857
await Directory(dbPath).delete(recursive: true);
6958
}
7059

60+
spinner.success();
61+
7162
// init s5
72-
final nowInMilliseconds = DateTime.now().millisecondsSinceEpoch;
73-
final lastEightDigits = nowInMilliseconds
74-
.toString()
75-
.substring(nowInMilliseconds.toString().length - 8);
76-
Hive.init(dbPath);
77-
final s5 = await S5.create(
78-
logger: FileLogger(file: join(logPath, 'log-$lastEightDigits.txt')));
79-
if (!s5.hasIdentity) {
80-
final seed = s5.generateSeedPhrase();
81-
print(
82-
"This is your ${green("seed")}. If you want to keep updating this registry entry in the future you ${green("MUST")} have this seed");
83-
print(green(seed));
84-
var confirmed = confirm('Have you written this down:', defaultValue: true);
85-
if (!confirmed) {
86-
print(
87-
"No like I'm ${red("serious")}, write this down. But to each their own.");
88-
}
89-
await s5.recoverIdentityFromSeedPhrase(seed);
90-
await s5.registerOnNewStorageService(
91-
'http://s5.ninja',
92-
inviteCode: 'TODO',
93-
);
94-
}
63+
spinner = spinStart("Initializing S5...");
64+
String nodeURL = results['node'] as String;
65+
initS5(nodeURL, dbPath, logPath);
66+
spinner.success();
67+
9568
exit(1);
9669
}

0 commit comments

Comments
 (0)