Skip to content

Commit

Permalink
refactor(report-format): now use enum insteadof strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Guckes authored and Justin Guckes committed Sep 13, 2023
1 parent 924f28b commit 91d2f63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/src/cli/commands/diff_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:args/command_runner.dart';
import 'package:dart_apitool/api_tool.dart';
import 'package:dart_apitool/src/diff/report/diff_reporter.dart';
import 'package:dart_apitool/src/diff/report/json_diff_reporter.dart';
import 'package:dart_apitool/src/diff/report/report_format.dart';

import '../../diff/report/console_diff_reporter.dart';
import '../../diff/report/markdown_diff_reporter.dart';
Expand Down Expand Up @@ -104,8 +105,8 @@ You may want to do this if you want to make sure
argParser.addOption(
_optionReportFormat,
help: 'Which output format should be used',
defaultsTo: 'cli',
allowed: ['cli', 'markdown', 'json'],
defaultsTo: ReportFormat.cli.name,
allowed: ReportFormat.values.map((e) => e.name),
mandatory: false,
);
argParser.addOption(
Expand All @@ -119,14 +120,15 @@ You may want to do this if you want to make sure
Future<int> run() async {
final oldPackageRef = PackageRef(argResults![_optionNameOld]);
final newPackageRef = PackageRef(argResults![_optionNameNew]);
final outputFormatter = argResults![_optionReportFormat];
final outputFormatter = ReportFormat.values.firstWhere(
(element) => element.name == argResults![_optionReportFormat]);
final outputFile = argResults![_optionReportPath];

if (outputFormatter != 'cli' && outputFile == null) {
if (outputFormatter != ReportFormat.cli && outputFile == null) {
throw 'You need to define an output file using the $_optionReportPath parameter when not using the cli option';
}

if (outputFormatter == 'cli' && outputFile != null) {
if (outputFormatter == ReportFormat.cli && outputFile != null) {
stdout.writeln(
'WARNING: $_optionReportPath has no effect because $_optionReportFormat is set to cli');
}
Expand Down Expand Up @@ -183,14 +185,14 @@ You may want to do this if you want to make sure

DiffReporter reporter = (() {
switch (outputFormatter) {
case 'cli':
case ReportFormat.cli:
return ConsoleDiffReporter();
case 'markdown':
case ReportFormat.markdown:
return MarkdownDiffReporter(
oldPackageRef: oldPackageRef,
newPackageRef: newPackageRef,
outputFile: File(outputFile));
case 'json':
case ReportFormat.json:
return JsonDiffReporter(
oldPackageRef: oldPackageRef,
newPackageRef: newPackageRef,
Expand Down
1 change: 1 addition & 0 deletions lib/src/diff/report/report_format.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum ReportFormat { cli, markdown, json }

0 comments on commit 91d2f63

Please sign in to comment.