diff --git a/lib/src/commands/get_command.dart b/lib/src/commands/get_command.dart index 5e14306..6f37269 100644 --- a/lib/src/commands/get_command.dart +++ b/lib/src/commands/get_command.dart @@ -10,7 +10,15 @@ import 'package:yaml/yaml.dart'; /// Configuration for a single file to download and export class FileConfig { /// Creates a file config - FileConfig({required this.fileId, required this.output, this.scale}); + FileConfig({ + required this.fileId, + required this.output, + this.name, + this.scale, + }); + + /// Optional human-readable name for this entry + final String? name; /// The file ID to download final String fileId; @@ -44,6 +52,10 @@ class GetCommand extends Command { abbr: 'c', help: 'Path to the config file', defaultsTo: './pepper-sprite.yaml', + ) + ..addOption( + 'only', + help: 'Process only the entry with this name', ); } @@ -61,6 +73,7 @@ class GetCommand extends Command { Future run() async { final apiKey = argResults!['api-key'] as String; final configPath = argResults!['config'] as String; + final only = argResults!['only'] as String?; // Check if config file exists final configFile = File(configPath); @@ -70,12 +83,21 @@ class GetCommand extends Command { } // Parse config file - final configs = _parseConfig(configFile); + var configs = _parseConfig(configFile); if (configs.isEmpty) { _logger.err('No files configured in $configPath'); return ExitCode.usage.code; } + // Filter by name if --only is provided + if (only != null) { + configs = configs.where((c) => c.name == only).toList(); + if (configs.isEmpty) { + _logger.err('No entry with name "$only" found in $configPath'); + return ExitCode.usage.code; + } + } + _logger.info('Found ${configs.length} file(s) to process'); var successCount = 0; @@ -119,10 +141,13 @@ class GetCommand extends Command { final fileId = fileMap['fileId'] as String?; final output = fileMap['output'] as String?; + final name = fileMap['name'] as String?; final scale = fileMap['scale'] as String?; if (fileId != null && output != null) { - configs.add(FileConfig(fileId: fileId, output: output, scale: scale)); + configs.add( + FileConfig(fileId: fileId, output: output, name: name, scale: scale), + ); } } diff --git a/test/src/commands/get_command_test.dart b/test/src/commands/get_command_test.dart index f9f5b00..edf3bd7 100644 --- a/test/src/commands/get_command_test.dart +++ b/test/src/commands/get_command_test.dart @@ -151,5 +151,73 @@ files: ).called(1); }); }); + + group('--only', () { + late Directory tempDir; + late File configFile; + + setUp(() { + tempDir = Directory.systemTemp.createTempSync('pepper_sprite_test_'); + configFile = File('${tempDir.path}/pepper-sprite.yaml'); + }); + + tearDown(() { + tempDir.deleteSync(recursive: true); + }); + + test('no entry with matching name', () async { + configFile.writeAsStringSync(''' +files: + - name: buildings + fileId: "Z8y1MP7R7z2u31aJq01N" + output: "/tmp/buildings.png" + - name: tiles + fileId: "w7Oy7gJXkBWUJgW7Y1jj" + output: "/tmp/tiles.png" +'''); + + final exitCode = await commandRunner.run([ + 'get', + '-k', + 'test-api-key', + '-c', + configFile.path, + '--only', + 'unknown', + ]); + + expect(exitCode, ExitCode.usage.code); + verify( + () => logger.err( + 'No entry with name "unknown" found in ${configFile.path}', + ), + ).called(1); + }); + + test('entries without name are excluded when --only is used', () async { + configFile.writeAsStringSync(''' +files: + - fileId: "no-name-id" + output: "/tmp/no-name.png" +'''); + + final exitCode = await commandRunner.run([ + 'get', + '-k', + 'test-api-key', + '-c', + configFile.path, + '--only', + 'buildings', + ]); + + expect(exitCode, ExitCode.usage.code); + verify( + () => logger.err( + 'No entry with name "buildings" found in ${configFile.path}', + ), + ).called(1); + }); + }); }); }