Skip to content

Commit

Permalink
增加仅更新周数和周末日期的配置文件的命令
Browse files Browse the repository at this point in the history
  • Loading branch information
1-6 authored and 1-6 committed Dec 5, 2024
1 parent a0d6529 commit a49fd0f
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static void addNestedProperty(String parentKey, String childKey, String v
String fullKey = parentKey + "." + childKey;
LogUtil.log("添加嵌套键值对: " + fullKey + " = " + value);
config.setProperty(fullKey, value);
// todo
propertyCache.put(fullKey, value);
propertiesMapCache.clear();
saveConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class Constant {
public static final String CATEGORY_BROWSER = "browser";
public static final String CATEGORY_EDITOR = "editor";
public static final String CATEGORY_VPN = "vpn";
public static final String CATEGORY_OUTER_URL = "outer-url";
public static final String CATEGORY_OUTER_URL = "outer_url";

public static String LOG_MODE = YamlConfig.initializeProperty(LOG, MODE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ protected final boolean checkArgs(String[] argv, int expectedNum, Consumer<Strin
return true;
}

protected final boolean checkArgs(String[] argv, Consumer<String[]> errorHandler, int... expectedNums) {
int length = argv.length;
if (containInArray(expectedNums,length)) {
LogUtil.error("expected argument num is %d, but got %d", expectedNums, length);
errorHandler.accept(argv);
return false;
}
return true;
}

private boolean containInArray(int[] expected, int target) {
for (int i : expected) {
if (i == target) {
return true;
}
}
return false;
}

protected final boolean checkArgs(String[] argv, int expectedNum) {
return checkArgs(argv, expectedNum, this::hint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ protected List<String> loadCommandList() {
@Override
protected void process(String[] argv) {
String alias = argv[2];

String path = YamlConfig.getProperty(PATH, alias);
if (!YamlConfig.containProperty(PATH, alias)
&& !YamlConfig.containProperty(INNER_URL, alias)
&& !YamlConfig.containProperty(OUTER_URL, alias)) {
LogUtil.error("No such alias %s", alias);
return;
}

String path = YamlConfig.getProperty(PATH, alias);

String category = argv[3];
switch (category) {
case CATEGORY_BROWSER -> {
Expand All @@ -40,7 +40,9 @@ protected void process(String[] argv) {
LogUtil.info("Add alias %s to VPN successfully", alias);
}
case CATEGORY_OUTER_URL -> {
path = YamlConfig.getProperty(INNER_URL, alias);
YamlConfig.addNestedProperty(category, alias, path);
YamlConfig.removeNestedProperty(INNER_URL, alias);
LogUtil.info("Add alias %s to OUTER_URL successfully", alias);
}
case SCRIPT -> {
Expand Down
237 changes: 130 additions & 107 deletions src/main/java/com/lingoutil/workcopilot/handler/ReportCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,111 +22,134 @@
import static com.lingoutil.workcopilot.constant.Constant.*;

public class ReportCommandHandler extends CommandHandler {
@Override
protected List<String> loadCommandList() {
return reportCommands;
}

private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy.MM.dd");

private Instant parseDate(String dateStr) throws ParseException {
return LocalDate.parse(dateStr, DATE_FORMATTER).atStartOfDay(ZoneId.systemDefault()).toInstant();
}

private void updateWeekConfig(String reportPath, int weekNum, Date nextLastDayOfWeek) {
try {
String nextLastDayOfWeekStr = new SimpleDateFormat("yyyy.MM.dd").format(nextLastDayOfWeek);
YamlConfig.addNestedProperty(REPORT, WEEK_NUM, String.valueOf(weekNum));
YamlConfig.addNestedProperty(REPORT, LAST_DAY_OF_WEEK, nextLastDayOfWeekStr);
LogUtil.info("✅ 更新配置文件成功:周数 = %d, 周结束日期 = %s", weekNum, nextLastDayOfWeekStr);
}
catch (Exception e) {
LogUtil.error("❌ 更新配置文件时出错: %s", e.getMessage());
}
}

@Override
protected void process(String[] argv) {
if (argv.length < 3) {
LogUtil.error("❌ 缺少必要参数,请提供脚本名、命令和内容。");
return;
}

String content = argv[2].trim();

// 如果 content 被引号包围 "",去除引号
if (content.startsWith("\"") && content.endsWith("\"")) {
content = content.substring(1, content.length() - 1);
}

if (content.isEmpty()) {
LogUtil.error("⚠️ 内容为空,无法写入。");
return;
}

String reportPath = YamlConfig.getProperty(REPORT, WEEK_REPORT);
LogUtil.info("📂 从配置文件中读取到路径:%s", reportPath);

int weekNum = Integer.parseInt(YamlConfig.getProperty(REPORT, WEEK_NUM));
String lastDayOfWeekStr = YamlConfig.getProperty(REPORT, LAST_DAY_OF_WEEK);

File file = new File(reportPath);
if (!file.exists()) {
LogUtil.error("❌ 路径不存在:%s", reportPath);
return;
}

Date now = new Date();
try {
Date lastDayOfWeek = new SimpleDateFormat("yyyy.MM.dd").parse(lastDayOfWeekStr);

if (now.after(addOneDay(lastDayOfWeek))) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_MONTH, 6);
Date nextLastDayOfWeek = calendar.getTime();
String newWeekTitle = String.format("# Week%d[%s-%s]\n", weekNum,
DATE_FORMATTER.format(now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()),
DATE_FORMATTER.format(nextLastDayOfWeek.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
);
weekNum++;
updateWeekConfig(reportPath, weekNum, nextLastDayOfWeek);
appendToFile(reportPath, newWeekTitle);
}

String todayStr = new SimpleDateFormat("yyyy/MM/dd").format(now);
String logEntry = String.format("- 【%s】 %s\n", todayStr, content);
appendToFile(reportPath, logEntry);
LogUtil.info("✅ 成功将内容写入:%s", reportPath);
}
catch (Exception e) {
LogUtil.error("❌ 操作时发生错误: %s", e.getMessage(), e);
}
}

// 使用 UTF-8 编码的文件追加方法
private void appendToFile(String filePath, String content) throws IOException {
try (FileChannel channel = new FileOutputStream(filePath, true).getChannel()) {
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
channel.write(buffer);
}
}

private Date addOneDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}

@Override
protected boolean checkArgs(String[] argv) {
return checkArgs(argv, 3, this::hint);
}

@Override
protected void hint(String[] argv) {
LogUtil.usage("%s %s <content>", argv[0], argv[1]);
}
@Override
protected List<String> loadCommandList() {
return reportCommands;
}

private final static String NEW_WEEK_CONFIG_UPDATE = "new";

private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy.MM.dd");

private Instant parseDate(String dateStr) throws ParseException {
return LocalDate.parse(dateStr, DATE_FORMATTER).atStartOfDay(ZoneId.systemDefault()).toInstant();
}

private void updateWeekConfig(int weekNum, Date nextLastDayOfWeek) {
try {
String nextLastDayOfWeekStr = new SimpleDateFormat("yyyy.MM.dd").format(nextLastDayOfWeek);
YamlConfig.addNestedProperty(REPORT, WEEK_NUM, String.valueOf(weekNum));
YamlConfig.addNestedProperty(REPORT, LAST_DAY_OF_WEEK, nextLastDayOfWeekStr);
LogUtil.info("✅ 更新配置文件成功:周数 = %d, 周结束日期 = %s", weekNum, nextLastDayOfWeekStr);
}
catch (Exception e) {
LogUtil.error("❌ 更新配置文件时出错: %s", e.getMessage());
}
}

@Override
protected void process(String[] argv) {
if (argv.length < 3) {
LogUtil.error("❌ 缺少必要参数,请提供脚本名、命令和内容。");
return;
}

String content = argv[2].trim();

// 如果 content 被引号包围 "",去除引号
if (content.startsWith("\"") && content.endsWith("\"")) {
content = content.substring(1, content.length() - 1);
}

if (content.isEmpty()) {
LogUtil.error("⚠️ 内容为空,无法写入。");
return;
}

int weekNum = Integer.parseInt(YamlConfig.getProperty(REPORT, WEEK_NUM));
String lastDayOfWeekStr = YamlConfig.getProperty(REPORT, LAST_DAY_OF_WEEK);

if (content.equals(NEW_WEEK_CONFIG_UPDATE)) {
String dataStr = argv.length == 4 ? argv[3] : lastDayOfWeekStr;
Date lastDayOfWeek = null;
Date nextLastDayOfWeek = null;

try {
lastDayOfWeek = new SimpleDateFormat("yyyy.MM.dd").parse(dataStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(lastDayOfWeek);
calendar.add(Calendar.DAY_OF_MONTH, 6);
nextLastDayOfWeek = calendar.getTime();
}
catch (ParseException e) {
LogUtil.error("更新周数失败,请检查日期字符串是否有误");
return;
}
updateWeekConfig(weekNum + 1, nextLastDayOfWeek);
return;
}

String reportPath = YamlConfig.getProperty(REPORT, WEEK_REPORT);
LogUtil.info("📂 从配置文件中读取到路径:%s", reportPath);

File file = new File(reportPath);
if (!file.exists()) {
LogUtil.error("❌ 路径不存在:%s", reportPath);
return;
}

Date now = new Date();
try {
Date lastDayOfWeek = new SimpleDateFormat("yyyy.MM.dd").parse(lastDayOfWeekStr);

if (now.after(addOneDay(lastDayOfWeek))) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_MONTH, 6);
Date nextLastDayOfWeek = calendar.getTime();
String newWeekTitle = String.format("# Week%d[%s-%s]\n", weekNum,
DATE_FORMATTER.format(now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()),
DATE_FORMATTER.format(nextLastDayOfWeek.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
);
weekNum++;
updateWeekConfig(weekNum, nextLastDayOfWeek);
appendToFile(reportPath, newWeekTitle);
}

String todayStr = new SimpleDateFormat("yyyy/MM/dd").format(now);
String logEntry = String.format("- 【%s】 %s\n", todayStr, content);
appendToFile(reportPath, logEntry);
LogUtil.info("✅ 成功将内容写入:%s", reportPath);
}
catch (Exception e) {
LogUtil.error("❌ 操作时发生错误: %s", e.getMessage(), e);
}
}

// 使用 UTF-8 编码的文件追加方法
private void appendToFile(String filePath, String content) throws IOException {
try (FileChannel channel = new FileOutputStream(filePath, true).getChannel()) {
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
channel.write(buffer);
}
}

private Date addOneDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}

@Override
protected boolean checkArgs(String[] argv) {
return checkArgs(argv, this::hint, 3, 4);
}

@Override
protected void hint(String[] argv) {
LogUtil.usage("%s %s <content>", argv[0], argv[1]);
LogUtil.usage("%s new [<last_day_of_week> in pattern yyyy.MM.dd]", argv[0], argv[1]);
}
}
30 changes: 23 additions & 7 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
editor:
code: /Applications/work_copilot_java/link/code.app
wps: /Applications/work_copilot_java/link/wps
tp: /Applications/Typora.app
idea: /Applications/work_copilot_java/link/idea.app
color:
warn: null
error: null
log: null
info: null
log:
mode: concise
inner_url: null
inner_url:
suixainglu: https://www.programmercarl.com/
storage:
mode: config
config:
Expand All @@ -24,20 +28,32 @@ version:
email: lingojack@qq.com
script:
depot: /Applications/work_copilot_java/script/
hello: /Applications/work_copilot_java/script/hello.sh
leetcode: /Applications/work_copilot_java/script/leetcode.sh
download-bv: /Applications/work_copilot_java/script/download-bv.sh
setting:
search-engine: bing
path:
bs: /Applications/work_copilot_java/link/chrome.app
wx: /Applications/WeChat.app
code: /Applications/work_copilot_java/link/code.app
hello: /Applications/work_copilot_java/script/hello.sh
idea: /Applications/work_copilot_java/link/idea.app
ms: /Applications/foobar2000.app
leetcode: /Applications/work_copilot_java/script/leetcode.sh
landrop: /Applications/LANDrop.app
fs: /Applications/Lark.app
bs: /Applications/work_copilot_java/link/chrome.app
vpn: /Applications/SFM.app
download-bv: /Applications/work_copilot_java/script/download-bv.sh
vpn: null
snip: /Applications/Snipaste.app
wps: /Applications/work_copilot_java/link/wps
calc: /System/Applications/Calculator.app
tp: /Applications/Typora.app
vpn:
vpn: /Applications/SFM.app
browser:
bs: /Applications/work_copilot_java/link/chrome.app
report:
week_num: '58'
week_report: /Users/a1-6/Documents/markdown_notebook/week_report.md
week_report: /Applications/work_copilot_java/report/week_report.md
last_day: 2024.12.04
outer_url: null
outer_url:
gpt: https://chatgpt.com/

0 comments on commit a49fd0f

Please sign in to comment.