You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
package com.example.datacollect.command;
|
|
|
|
import com.example.datacollect.repository.ArticleRepository;
|
|
import com.example.datacollect.persist.JsonExporter;
|
|
import com.example.datacollect.view.ConsoleView;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class ExportCommand implements Command {
|
|
private static final Logger logger = LoggerFactory.getLogger(ExportCommand.class);
|
|
private final ConsoleView view;
|
|
private final JsonExporter jsonExporter;
|
|
|
|
public ExportCommand(ConsoleView view) {
|
|
this.view = view;
|
|
this.jsonExporter = new JsonExporter();
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "export";
|
|
}
|
|
|
|
@Override
|
|
public void execute(String[] args, ArticleRepository repository) {
|
|
// 参数校验:export <filePath>
|
|
if (args.length < 2) {
|
|
String errorMsg = "用法: export <文件路径> (示例: export articles.json)";
|
|
logger.error(errorMsg);
|
|
view.printError(errorMsg);
|
|
return;
|
|
}
|
|
|
|
String filePath = args[1];
|
|
try {
|
|
jsonExporter.export(repository, filePath);
|
|
view.printSuccess("✅ 成功导出文章到: " + filePath);
|
|
} catch (IOException e) {
|
|
view.printError("❌ 导出失败: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|