Skip to content

Commit ab08227

Browse files
committed
feat: Update StorageConfig and ID generation logic
- Modified NewStorageConfig to use different ID generation functions for TelemetryMacMachineId and TelemetryMachineId, ensuring machineId uses a new format while others retain the old format. - Enhanced saveConfig to read the original configuration file, preserving all fields while updating telemetry-related fields. - Added lastModified and version fields to the configuration if they do not exist, improving configuration management. - Introduced a new generateMacMachineId function for consistent ID generation. These changes improve the flexibility and maintainability of the storage configuration handling.
1 parent 5ee2265 commit ab08227

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

main.go

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,44 @@ func (e *AppError) Error() string {
168168
}
169169

170170
// Configuration Functions / 配置函数
171-
func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig { // Modified to take old config
171+
func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig {
172+
// Use different ID generation functions for different fields
173+
// 为不同字段使用不同的ID生成函数
174+
// Reason: machineId needs new format while others keep old format
175+
// 原因:machineId需要使用新格式,而其他ID保持旧格式
172176
newConfig := &StorageConfig{
173-
TelemetryMacMachineId: generateMachineId(),
174-
TelemetryMachineId: generateMachineId(),
177+
TelemetryMacMachineId: generateMacMachineId(), // Use old format / 使用旧格式
178+
TelemetryMachineId: generateMachineId(), // Use new format / 使用新格式
175179
TelemetryDevDeviceId: generateDevDeviceId(),
176180
}
177181

182+
// Keep sqmId from old config or generate new one using old format
183+
// 保留旧配置的sqmId或使用旧格式生成新的
178184
if oldConfig != nil {
179185
newConfig.TelemetrySqmId = oldConfig.TelemetrySqmId
180186
} else {
181-
newConfig.TelemetrySqmId = generateMachineId()
187+
newConfig.TelemetrySqmId = generateMacMachineId()
182188
}
183189

184190
if newConfig.TelemetrySqmId == "" {
185-
newConfig.TelemetrySqmId = generateMachineId()
191+
newConfig.TelemetrySqmId = generateMacMachineId()
186192
}
187193

188194
return newConfig
189195
}
190196

191197
// ID Generation Functions / ID生成函数
192198
func generateMachineId() string {
199+
prefix := "auth0|user_"
200+
remainingLength := 74 - len(prefix)
201+
bytes := make([]byte, remainingLength/2)
202+
if _, err := rand.Read(bytes); err != nil {
203+
panic(fmt.Errorf("failed to generate random data: %v", err))
204+
}
205+
return prefix + hex.EncodeToString(bytes)
206+
}
207+
208+
func generateMacMachineId() string {
193209
data := make([]byte, 32)
194210
if _, err := rand.Read(data); err != nil {
195211
panic(fmt.Errorf("failed to generate random data: %v", err))
@@ -225,7 +241,7 @@ func getConfigPath(username string) (string, error) {
225241
return filepath.Join(configDir, "storage.json"), nil
226242
}
227243

228-
func saveConfig(config *StorageConfig, username string) error { // Modified to take username
244+
func saveConfig(config *StorageConfig, username string) error {
229245
configPath, err := getConfigPath(username)
230246
if err != nil {
231247
return err
@@ -252,40 +268,52 @@ func saveConfig(config *StorageConfig, username string) error { // Modified to t
252268
}
253269
}
254270

255-
originalFileStat, err := os.Stat(configPath)
256-
if err != nil {
257-
return &AppError{
258-
Type: ErrSystem,
259-
Op: "get file mode",
260-
Path: configPath,
261-
Err: err,
262-
}
263-
}
264-
originalFileMode := originalFileStat.Mode()
265-
271+
// Read the original file to preserve all fields
272+
var originalFile map[string]interface{}
266273
originalFileContent, err := os.ReadFile(configPath)
267274
if err != nil {
268-
return &AppError{
269-
Type: ErrSystem,
270-
Op: "read original file",
271-
Path: configPath,
272-
Err: err,
275+
if !os.IsNotExist(err) {
276+
return &AppError{
277+
Type: ErrSystem,
278+
Op: "read original file",
279+
Path: configPath,
280+
Err: err,
281+
}
282+
}
283+
// If file doesn't exist, create a new map
284+
originalFile = make(map[string]interface{})
285+
} else {
286+
if err := json.Unmarshal(originalFileContent, &originalFile); err != nil {
287+
return &AppError{
288+
Type: ErrSystem,
289+
Op: "unmarshal original file",
290+
Path: configPath,
291+
Err: err,
292+
}
273293
}
274294
}
275295

276-
var originalFile map[string]any
277-
if err := json.Unmarshal(originalFileContent, &originalFile); err != nil {
278-
return &AppError{
279-
Type: ErrSystem,
280-
Op: "unmarshal original file",
281-
Path: configPath,
282-
Err: err,
283-
}
296+
// Get original file mode
297+
var originalFileMode os.FileMode = 0666
298+
if stat, err := os.Stat(configPath); err == nil {
299+
originalFileMode = stat.Mode()
284300
}
301+
302+
// Update only the telemetry fields while preserving all other fields
285303
originalFile["telemetry.sqmId"] = config.TelemetrySqmId
286304
originalFile["telemetry.macMachineId"] = config.TelemetryMacMachineId
287305
originalFile["telemetry.machineId"] = config.TelemetryMachineId
288306
originalFile["telemetry.devDeviceId"] = config.TelemetryDevDeviceId
307+
308+
// Add lastModified and version fields if they don't exist
309+
if _, exists := originalFile["lastModified"]; !exists {
310+
originalFile["lastModified"] = time.Now().UTC().Format(time.RFC3339)
311+
}
312+
if _, exists := originalFile["version"]; !exists {
313+
originalFile["version"] = "1.0.1"
314+
}
315+
316+
// Marshal with indentation
289317
newFileContent, err := json.MarshalIndent(originalFile, "", " ")
290318
if err != nil {
291319
return &AppError{
@@ -513,7 +541,7 @@ func showSuccess() {
513541
successColor.Printf("%s\n", text.SuccessMessage)
514542
fmt.Println()
515543
warningColor.Printf("%s\n", text.RestartMessage)
516-
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
544+
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━��━━")
517545
} else {
518546
// Chinese messages with extra spacing
519547
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")

0 commit comments

Comments
 (0)