Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/src/commands/get_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +52,10 @@ class GetCommand extends Command<int> {
abbr: 'c',
help: 'Path to the config file',
defaultsTo: './pepper-sprite.yaml',
)
..addOption(
'only',
help: 'Process only the entry with this name',
);
}

Expand All @@ -61,6 +73,7 @@ class GetCommand extends Command<int> {
Future<int> 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);
Expand All @@ -70,12 +83,21 @@ class GetCommand extends Command<int> {
}

// 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;
Expand Down Expand Up @@ -119,10 +141,13 @@ class GetCommand extends Command<int> {

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),
);
}
}

Expand Down
68 changes: 68 additions & 0 deletions test/src/commands/get_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}
Loading