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.JsonImporter;
|
|
import com.example.datacollect.view.ConsoleView;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class ImportCommand implements Command {
|
|
private static final Logger logger = LoggerFactory.getLogger(ImportCommand.class);
|
|
private final ConsoleView view;
|
|
private final JsonImporter jsonImporter;
|
|
|
|
public ImportCommand(ConsoleView view) {
|
|
this.view = view;
|
|
this.jsonImporter = new JsonImporter();
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "import";
|
|
}
|
|
|
|
@Override
|
|
public void execute(String[] args, ArticleRepository repository) {
|
|
// 参数校验:import <filePath>
|
|
if (args.length < 2) {
|
|
String errorMsg = "用法: import <文件路径> (示例: import articles.json)";
|
|
logger.error(errorMsg);
|
|
view.printError(errorMsg);
|
|
return;
|
|
}
|
|
|
|
String filePath = args[1];
|
|
try {
|
|
jsonImporter.importArticles(repository, filePath);
|
|
view.printSuccess("✅ 成功从文件导入文章: " + filePath);
|
|
} catch (IOException e) {
|
|
view.printError("❌ 导入失败: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|