run method

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() async {
  final credFile = File('${ObsUtil.userHome}/.obs/credentials.json');

  final hasCred = credFile.existsSync();

  String? approval;

  final credentials = <String, dynamic>{};

  if (hasCred) {
    print('Do you want to overwrite the default credentials [y/n]:');

    approval = stdin.readLineSync(encoding: Encoding.getByName('utf-8')!);

    //use existing credentials from filesystem
    if (approval != null && approval.startsWith('n')) {
      final content = credFile.readAsStringSync();

      try {
        credentials.addAll(json.decode(content));
      } catch (exception) {
        stderr.write('Error reading the credentials file.\n');

        exit(64);
      }
    }
  }

  print('Uri (ws://[host]:[port]):');

  credentials['uri'] =
      stdin.readLineSync(encoding: Encoding.getByName('utf-8')!);

  print('Password:');

  stdin.echoMode = false;

  credentials['password'] =
      stdin.readLineSync(encoding: Encoding.getByName('utf-8')!);

  if (credentials['uri'] == null || credentials['password'] == null) {
    throw Exception('Error: missing required data.');
  }

  credFile.createSync(recursive: true);

  credFile.writeAsString(json.encode(credentials));

  print('Authorization completed.');
}