mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-06-08 12:32:06 +08:00
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.
This commit is contained in:
parent
5ee2265958
commit
ab08227350
90
main.go
90
main.go
@ -168,21 +168,27 @@ func (e *AppError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Configuration Functions / 配置函数
|
// Configuration Functions / 配置函数
|
||||||
func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig { // Modified to take old config
|
func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig {
|
||||||
|
// Use different ID generation functions for different fields
|
||||||
|
// 为不同字段使用不同的ID生成函数
|
||||||
|
// Reason: machineId needs new format while others keep old format
|
||||||
|
// 原因:machineId需要使用新格式,而其他ID保持旧格式
|
||||||
newConfig := &StorageConfig{
|
newConfig := &StorageConfig{
|
||||||
TelemetryMacMachineId: generateMachineId(),
|
TelemetryMacMachineId: generateMacMachineId(), // Use old format / 使用旧格式
|
||||||
TelemetryMachineId: generateMachineId(),
|
TelemetryMachineId: generateMachineId(), // Use new format / 使用新格式
|
||||||
TelemetryDevDeviceId: generateDevDeviceId(),
|
TelemetryDevDeviceId: generateDevDeviceId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep sqmId from old config or generate new one using old format
|
||||||
|
// 保留旧配置的sqmId或使用旧格式生成新的
|
||||||
if oldConfig != nil {
|
if oldConfig != nil {
|
||||||
newConfig.TelemetrySqmId = oldConfig.TelemetrySqmId
|
newConfig.TelemetrySqmId = oldConfig.TelemetrySqmId
|
||||||
} else {
|
} else {
|
||||||
newConfig.TelemetrySqmId = generateMachineId()
|
newConfig.TelemetrySqmId = generateMacMachineId()
|
||||||
}
|
}
|
||||||
|
|
||||||
if newConfig.TelemetrySqmId == "" {
|
if newConfig.TelemetrySqmId == "" {
|
||||||
newConfig.TelemetrySqmId = generateMachineId()
|
newConfig.TelemetrySqmId = generateMacMachineId()
|
||||||
}
|
}
|
||||||
|
|
||||||
return newConfig
|
return newConfig
|
||||||
@ -190,6 +196,16 @@ func NewStorageConfig(oldConfig *StorageConfig) *StorageConfig { // Modified to
|
|||||||
|
|
||||||
// ID Generation Functions / ID生成函数
|
// ID Generation Functions / ID生成函数
|
||||||
func generateMachineId() string {
|
func generateMachineId() string {
|
||||||
|
prefix := "auth0|user_"
|
||||||
|
remainingLength := 74 - len(prefix)
|
||||||
|
bytes := make([]byte, remainingLength/2)
|
||||||
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
|
panic(fmt.Errorf("failed to generate random data: %v", err))
|
||||||
|
}
|
||||||
|
return prefix + hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateMacMachineId() string {
|
||||||
data := make([]byte, 32)
|
data := make([]byte, 32)
|
||||||
if _, err := rand.Read(data); err != nil {
|
if _, err := rand.Read(data); err != nil {
|
||||||
panic(fmt.Errorf("failed to generate random data: %v", err))
|
panic(fmt.Errorf("failed to generate random data: %v", err))
|
||||||
@ -225,7 +241,7 @@ func getConfigPath(username string) (string, error) {
|
|||||||
return filepath.Join(configDir, "storage.json"), nil
|
return filepath.Join(configDir, "storage.json"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveConfig(config *StorageConfig, username string) error { // Modified to take username
|
func saveConfig(config *StorageConfig, username string) error {
|
||||||
configPath, err := getConfigPath(username)
|
configPath, err := getConfigPath(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -252,40 +268,52 @@ func saveConfig(config *StorageConfig, username string) error { // Modified to t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
originalFileStat, err := os.Stat(configPath)
|
// Read the original file to preserve all fields
|
||||||
if err != nil {
|
var originalFile map[string]interface{}
|
||||||
return &AppError{
|
|
||||||
Type: ErrSystem,
|
|
||||||
Op: "get file mode",
|
|
||||||
Path: configPath,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
originalFileMode := originalFileStat.Mode()
|
|
||||||
|
|
||||||
originalFileContent, err := os.ReadFile(configPath)
|
originalFileContent, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &AppError{
|
if !os.IsNotExist(err) {
|
||||||
Type: ErrSystem,
|
return &AppError{
|
||||||
Op: "read original file",
|
Type: ErrSystem,
|
||||||
Path: configPath,
|
Op: "read original file",
|
||||||
Err: err,
|
Path: configPath,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If file doesn't exist, create a new map
|
||||||
|
originalFile = make(map[string]interface{})
|
||||||
|
} else {
|
||||||
|
if err := json.Unmarshal(originalFileContent, &originalFile); err != nil {
|
||||||
|
return &AppError{
|
||||||
|
Type: ErrSystem,
|
||||||
|
Op: "unmarshal original file",
|
||||||
|
Path: configPath,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var originalFile map[string]any
|
// Get original file mode
|
||||||
if err := json.Unmarshal(originalFileContent, &originalFile); err != nil {
|
var originalFileMode os.FileMode = 0666
|
||||||
return &AppError{
|
if stat, err := os.Stat(configPath); err == nil {
|
||||||
Type: ErrSystem,
|
originalFileMode = stat.Mode()
|
||||||
Op: "unmarshal original file",
|
|
||||||
Path: configPath,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update only the telemetry fields while preserving all other fields
|
||||||
originalFile["telemetry.sqmId"] = config.TelemetrySqmId
|
originalFile["telemetry.sqmId"] = config.TelemetrySqmId
|
||||||
originalFile["telemetry.macMachineId"] = config.TelemetryMacMachineId
|
originalFile["telemetry.macMachineId"] = config.TelemetryMacMachineId
|
||||||
originalFile["telemetry.machineId"] = config.TelemetryMachineId
|
originalFile["telemetry.machineId"] = config.TelemetryMachineId
|
||||||
originalFile["telemetry.devDeviceId"] = config.TelemetryDevDeviceId
|
originalFile["telemetry.devDeviceId"] = config.TelemetryDevDeviceId
|
||||||
|
|
||||||
|
// Add lastModified and version fields if they don't exist
|
||||||
|
if _, exists := originalFile["lastModified"]; !exists {
|
||||||
|
originalFile["lastModified"] = time.Now().UTC().Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
if _, exists := originalFile["version"]; !exists {
|
||||||
|
originalFile["version"] = "1.0.1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal with indentation
|
||||||
newFileContent, err := json.MarshalIndent(originalFile, "", " ")
|
newFileContent, err := json.MarshalIndent(originalFile, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &AppError{
|
return &AppError{
|
||||||
@ -513,7 +541,7 @@ func showSuccess() {
|
|||||||
successColor.Printf("%s\n", text.SuccessMessage)
|
successColor.Printf("%s\n", text.SuccessMessage)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
warningColor.Printf("%s\n", text.RestartMessage)
|
warningColor.Printf("%s\n", text.RestartMessage)
|
||||||
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<EFBFBD><EFBFBD>━━")
|
||||||
} else {
|
} else {
|
||||||
// Chinese messages with extra spacing
|
// Chinese messages with extra spacing
|
||||||
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user