-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliState.java
More file actions
114 lines (103 loc) · 3.5 KB
/
Copy pathCliState.java
File metadata and controls
114 lines (103 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package io.aether.cli;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.aether.logger.Log;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Manages the persistent JSON state file (~/.aether-cli-state.json)
* for storing user preferences like aliases.
*/
public class CliState {
private static final String STATE_FILE_NAME = ".aether-cli-state.json";
private final Path stateFilePath;
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private StateData data = new StateData();
public CliState() {
this.stateFilePath = Paths.get(System.getProperty("user.home"), STATE_FILE_NAME);
}
/**
* Loads the state from the JSON file on disk.
*/
public void load() {
try {
if (Files.exists(stateFilePath)) {
String json = Files.readString(stateFilePath);
if (json.isBlank()) {
Log.warn("CLI state file is empty, initializing new one.");
save(); // Save empty default state
return;
}
// Parse JSON
StateData loadedData = gson.fromJson(json, StateData.class);
if (loadedData != null) {
this.data = loadedData;
if (this.data.aliases == null) {
this.data.aliases = new ConcurrentHashMap<>();
}
Log.info("Loaded " + this.data.aliases.size() + " user aliases from " + stateFilePath);
}
} else {
Log.info("No CLI state file found, creating a new one at " + stateFilePath);
save(); // Create the file with default (empty) content
}
} catch (Exception e) {
Log.error("Failed to load CLI state from " + stateFilePath + ". Error: " + e.getMessage());
}
}
/**
* Saves the current state (including all aliases) to the JSON file.
*/
public synchronized void save() {
try {
String json = gson.toJson(this.data);
Files.writeString(stateFilePath, json);
} catch (Exception e) {
Log.error("Failed to save CLI state to " + stateFilePath, e);
}
}
/**
* Adds a new alias and persists it to disk.
*
* @param alias The alias name.
* @param uuid The UUID string.
*/
public void addAlias(String alias, String uuid) {
if (alias == null || alias.isBlank() || uuid == null || uuid.isBlank()) {
return;
}
data.aliases.put(alias, uuid);
save();
}
/**
* @return The map of user-defined aliases.
*/
public Map<String, String> getAliases() {
return data.aliases;
}
/**
* Checks if a given alias exists.
*
* @param alias The alias name.
* @return true if the alias exists.
*/
public boolean hasAlias(String alias) {
return data.aliases.containsKey(alias);
}
/**
* Gets the UUID string for a given alias.
*
* @param alias The alias name.
* @return The UUID string or null if not found.
*/
public String getUuidForAlias(String alias) {
return data.aliases.get(alias);
}
// This structure is directly serialized to/from JSON
public static class StateData {
public Map<String, String> aliases = new ConcurrentHashMap<>();
}
}