11import 'dart:io' ;
22
33import 'package:args/args.dart' ;
4+ import 'package:cli_spin/cli_spin.dart' ;
45import 'package:dcli/dcli.dart' ;
5- import 'package:hive/hive.dart' ;
66import 'package:path/path.dart' ;
7- import 'package:s5/s5.dart' ;
87import 'package:s5_deploy/definitons/constants.dart' ;
8+ import 'package:s5_deploy/functions/cli.dart' ;
99import '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' ;
1112import '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