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 {
  await initializeOnvif();

  final users = <User>[];

  final hasFile = argResults?.options.contains('users-file') ?? false;

  final hasUser = (argResults?.options.contains('username') ?? false) &&
      (argResults?.options.contains('user-level') ?? false);

  if (hasFile) {
    final userFile = File(argResults!['users-file']);

    if (!userFile.existsSync()) {
      throw UsageException(
          '${argResults!['users-file']} - file not found', usage);
    }

    users.addAll((loadYaml(userFile.readAsStringSync()) as YamlList)
        .map((userJson) => User.fromYamlMap(userJson))
        .toList());
  }

  if (hasUser) {
    users.add(User(
      username: argResults!['username'],
      password: argResults?['password'],
      userLevel: UserLevel.values.byName(argResults!['user-level']),
    ));
  }

  try {
    if (hasUser || hasFile) {
      await deviceManagement.createUsers(users);
    } else {
      throw UsageException('User information required.', usage);
    }
  } on DioException catch (err) {
    throw UsageException('API usage error:', err.usage);
  }
}