run method

  1. @override
Future<void> run()
override

Runs this command.

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

Implementation

@override
Future<void> run() async {
  final rest = argResults!.rest;
  if (rest.length > 1) {
    printErrorAndExit('--- Too many arguments specified ---');
  }
  final apiKey = Platform.environment['KENALL_API_KEY'];
  if (apiKey == null) {
    printErrorAndExit('--- KENALL_API_KEY is not found ---');
  }
  final prefectureCode = argResults!['code'] ?? '';
  if (prefectureCode.isEmpty) {
    printErrorAndExit('--- arguments prefecture-code is not found ---');
  }
  final config = Config(apiKey: apiKey!);
  final kenallClient = KenallClient(config, http.Client());
  try {
    final response = await kenallClient.getCities(
      GetCitiesRequest(prefectureCode: prefectureCode),
    );
    printInfo('version: ${response.version}');
    printInfo('city count: ${response.cities.length}');
    response.cities.forEach((city) {
      printInfo('city: ${city.toJson()}');
    });
    exit(0);
  } catch (error) {
    printErrorAndExit('--- $error ---');
  }
}