-
Notifications
You must be signed in to change notification settings - Fork 318
/
main.go
1050 lines (910 loc) · 27.7 KB
/
main.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
// Core imports / 核心导入
import (
"bufio"
"context"
cryptorand "crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/fatih/color"
)
// Version information
var version = "dev" // This will be overwritten by goreleaser
// Types and Constants / 类型和常量
type Language string
const (
// Language options / 语言选项
CN Language = "cn"
EN Language = "en"
// Error types / 错误类型
ErrPermission = "permission_error"
ErrConfig = "config_error"
ErrProcess = "process_error"
ErrSystem = "system_error"
)
// Configuration Structures / 配置结构
type (
// TextResource stores multilingual text / 存储多语言文本
TextResource struct {
SuccessMessage string
RestartMessage string
ReadingConfig string
GeneratingIds string
PressEnterToExit string
ErrorPrefix string
PrivilegeError string
RunAsAdmin string
RunWithSudo string
SudoExample string
ConfigLocation string
CheckingProcesses string
ClosingProcesses string
ProcessesClosed string
PleaseWait string
SetReadOnlyMessage string
}
// StorageConfig optimized storage configuration / 优化的存储配置
StorageConfig struct {
TelemetryMacMachineId string `json:"telemetry.macMachineId"`
TelemetryMachineId string `json:"telemetry.machineId"`
TelemetryDevDeviceId string `json:"telemetry.devDeviceId"`
TelemetrySqmId string `json:"telemetry.sqmId"`
}
// AppError defines error types / 定义错误类型
AppError struct {
Type string
Op string
Path string
Err error
Context map[string]interface{}
}
// Config structures / 配置结构
Config struct {
Storage StorageConfig
UI UIConfig
System SystemConfig
}
UIConfig struct {
Language Language
Theme string
Spinner SpinnerConfig
}
SystemConfig struct {
RetryAttempts int
RetryDelay time.Duration
Timeout time.Duration
}
// SpinnerConfig defines spinner configuration / 定义进度条配置
SpinnerConfig struct {
Frames []string
Delay time.Duration
}
// ProgressSpinner for showing progress animation / 用于显示进度动画
ProgressSpinner struct {
frames []string
current int
message string
}
)
// Global Variables / 全局变量
var (
currentLanguage = CN // Default to Chinese / 默认为中文
texts = map[Language]TextResource{
CN: {
SuccessMessage: "[√] 配置文件已成功更新!",
RestartMessage: "[!] 请手动重启 Cursor 以使更新生效",
ReadingConfig: "正在读取配置文件...",
GeneratingIds: "正在生成新的标识符...",
PressEnterToExit: "按回车键退出程序...",
ErrorPrefix: "程序发生严重错误: %v",
PrivilegeError: "\n[!] 错误:需要管理员权限",
RunAsAdmin: "请右键点击程序,选择「以管理员身份运行」",
RunWithSudo: "请使用 sudo 命令运行此程序",
SudoExample: "示例: sudo %s",
ConfigLocation: "配置文件位置:",
CheckingProcesses: "正在检查运行中的 Cursor 实例...",
ClosingProcesses: "正在关闭 Cursor 实例...",
ProcessesClosed: "所有 Cursor 实例已关闭",
PleaseWait: "请稍候...",
SetReadOnlyMessage: "设置 storage.json 为只读模式, 这将导致 workspace 记录信息丢失等问题",
},
EN: {
SuccessMessage: "[√] Configuration file updated successfully!",
RestartMessage: "[!] Please restart Cursor manually for changes to take effect",
ReadingConfig: "Reading configuration file...",
GeneratingIds: "Generating new identifiers...",
PressEnterToExit: "Press Enter to exit...",
ErrorPrefix: "Program encountered a serious error: %v",
PrivilegeError: "\n[!] Error: Administrator privileges required",
RunAsAdmin: "Please right-click and select 'Run as Administrator'",
RunWithSudo: "Please run this program with sudo",
SudoExample: "Example: sudo %s",
ConfigLocation: "Config file location:",
CheckingProcesses: "Checking for running Cursor instances...",
ClosingProcesses: "Closing Cursor instances...",
ProcessesClosed: "All Cursor instances have been closed",
PleaseWait: "Please wait...",
SetReadOnlyMessage: "Set storage.json to read-only mode, which will cause issues such as lost workspace records",
},
}
)
var setReadOnly *bool = flag.Bool("r", false, "set storage.json to read-only mode")
// Error Implementation / 错误实现
func (e *AppError) Error() string {
if e.Context != nil {
return fmt.Sprintf("[%s] %s: %v (context: %v)", e.Type, e.Op, e.Err, e.Context)
}
return fmt.Sprintf("[%s] %s: %v", e.Type, e.Op, e.Err)
}
// Configuration Functions / 配置函数
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{
TelemetryMacMachineId: generateMacMachineId(), // Use old format / 使用旧格式
TelemetryMachineId: generateMachineId(), // Use new format / 使用新格式
TelemetryDevDeviceId: generateDevDeviceId(),
}
// Keep sqmId from old config or generate new one using old format
// 保留旧配置的sqmId或使用旧格式生成新的
if oldConfig != nil {
newConfig.TelemetrySqmId = oldConfig.TelemetrySqmId
} else {
newConfig.TelemetrySqmId = generateMacMachineId()
}
if newConfig.TelemetrySqmId == "" {
newConfig.TelemetrySqmId = generateMacMachineId()
}
return newConfig
}
func generateMachineId() string {
// 基础结构:auth0|user_XX[unique_id]
prefix := "auth0|user_"
// 生成两位数字序列 (00-99)
sequence := fmt.Sprintf("%02d", rand.Intn(100))
// 生成唯一标识部分 (23字符)
uniqueId := generateUniqueId(23)
// 组合完整ID
fullId := prefix + sequence + uniqueId
// 转换为十六进制
return hex.EncodeToString([]byte(fullId))
}
func generateUniqueId(length int) string {
// 字符集:使用类似 Crockford's Base32 的字符集
const charset = "0123456789ABCDEFGHJKLMNPQRSTVWXYZ"
// 生成随机字符串
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func generateMacMachineId() string {
data := make([]byte, 32)
if _, err := cryptorand.Read(data); err != nil {
panic(fmt.Errorf("failed to generate random data: %v", err))
}
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}
func generateDevDeviceId() string {
uuid := make([]byte, 16)
if _, err := cryptorand.Read(uuid); err != nil {
panic(fmt.Errorf("failed to generate UUID: %v", err))
}
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant
return fmt.Sprintf("%x-%x-%x-%x-%x",
uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:16])
}
// File Operations / 文件操作
func getConfigPath(username string) (string, error) {
var configDir string
switch runtime.GOOS {
case "windows":
configDir = filepath.Join(os.Getenv("APPDATA"), "Cursor", "User", "globalStorage")
case "darwin":
configDir = filepath.Join("/Users", username, "Library", "Application Support", "Cursor", "User", "globalStorage")
case "linux":
configDir = filepath.Join("/home", username, ".config", "Cursor", "User", "globalStorage")
default:
return "", fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
return filepath.Join(configDir, "storage.json"), nil
}
func saveConfig(config *StorageConfig, username string) error {
configPath, err := getConfigPath(username)
if err != nil {
return err
}
// Create parent directories with proper permissions
dir := filepath.Dir(configPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return &AppError{
Type: ErrSystem,
Op: "create directory",
Path: dir,
Err: err,
}
}
// First ensure we can write to the file
if err := os.Chmod(configPath, 0666); err != nil && !os.IsNotExist(err) {
return &AppError{
Type: ErrSystem,
Op: "modify file permissions",
Path: configPath,
Err: err,
}
}
// Read the original file to preserve all fields
var originalFile map[string]interface{}
originalFileContent, err := os.ReadFile(configPath)
if err != nil {
if !os.IsNotExist(err) {
return &AppError{
Type: ErrSystem,
Op: "read original file",
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,
}
}
}
// Get original file mode
var originalFileMode os.FileMode = 0666
if stat, err := os.Stat(configPath); err == nil {
originalFileMode = stat.Mode()
}
// Update only the telemetry fields while preserving all other fields
originalFile["telemetry.sqmId"] = config.TelemetrySqmId
originalFile["telemetry.macMachineId"] = config.TelemetryMacMachineId
originalFile["telemetry.machineId"] = config.TelemetryMachineId
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, "", " ")
if err != nil {
return &AppError{
Type: ErrSystem,
Op: "marshal new file",
Path: configPath,
Err: err,
}
}
// Write to temporary file first
tmpPath := configPath + ".tmp"
if err := os.WriteFile(tmpPath, newFileContent, 0666); err != nil {
return &AppError{
Type: ErrSystem,
Op: "write temporary file",
Path: tmpPath,
Err: err,
}
}
if *setReadOnly {
originalFileMode = 0444
}
// Ensure proper permissions on temporary file
if err := os.Chmod(tmpPath, originalFileMode); err != nil {
os.Remove(tmpPath)
return &AppError{
Type: ErrSystem,
Op: "set temporary file permissions",
Path: tmpPath,
Err: err,
}
}
// Atomic rename
if err := os.Rename(tmpPath, configPath); err != nil {
os.Remove(tmpPath)
return &AppError{
Type: ErrSystem,
Op: "rename file",
Path: configPath,
Err: err,
}
}
// Sync the directory to ensure changes are written to disk
if dir, err := os.Open(filepath.Dir(configPath)); err == nil {
dir.Sync()
dir.Close()
}
return nil
}
func readExistingConfig(username string) (*StorageConfig, error) { // Modified to take username
configPath, err := getConfigPath(username)
if err != nil {
return nil, err
}
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
var config StorageConfig
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}
// Process Management / 进程管理
type ProcessManager struct {
config *SystemConfig
}
func (pm *ProcessManager) killCursorProcesses() error {
ctx, cancel := context.WithTimeout(context.Background(), pm.config.Timeout)
defer cancel()
for attempt := 0; attempt < pm.config.RetryAttempts; attempt++ {
if err := pm.killProcess(ctx); err != nil {
time.Sleep(pm.config.RetryDelay)
continue
}
return nil
}
return &AppError{
Type: ErrProcess,
Op: "kill_processes",
Err: errors.New("failed to kill all Cursor processes after retries"),
}
}
func (pm *ProcessManager) killProcess(ctx context.Context) error {
if runtime.GOOS == "windows" {
return pm.killWindowsProcess(ctx)
}
return pm.killUnixProcess(ctx)
}
func (pm *ProcessManager) killWindowsProcess(ctx context.Context) error {
exec.CommandContext(ctx, "taskkill", "/IM", "Cursor.exe").Run()
time.Sleep(pm.config.RetryDelay)
exec.CommandContext(ctx, "taskkill", "/F", "/IM", "Cursor.exe").Run()
return nil
}
func (pm *ProcessManager) killUnixProcess(ctx context.Context) error {
// Search for the process by it's executable name (AppRun) in ps output
cmd := exec.CommandContext(ctx, "ps", "aux")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to execute ps command: %w", err)
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "AppRun") {
parts := strings.Fields(line)
if len(parts) > 1 {
pid := parts[1]
if err := pm.forceKillProcess(ctx, pid); err != nil {
return err
}
}
}
// handle lowercase
if strings.Contains(line, "apprun") {
parts := strings.Fields(line)
if len(parts) > 1 {
pid := parts[1]
if err := pm.forceKillProcess(ctx, pid); err != nil {
return err
}
}
}
}
return nil
}
// helper function to kill process by pid
func (pm *ProcessManager) forceKillProcess(ctx context.Context, pid string) error {
// First try graceful termination
if err := exec.CommandContext(ctx, "kill", pid).Run(); err == nil {
// Wait for processes to terminate gracefully
time.Sleep(2 * time.Second)
}
// Force kill if still running
if err := exec.CommandContext(ctx, "kill", "-9", pid).Run(); err != nil {
return fmt.Errorf("failed to force kill process %s: %w", pid, err)
}
return nil
}
func checkCursorRunning() bool {
cmd := exec.Command("ps", "aux")
output, err := cmd.Output()
if err != nil {
return false
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "AppRun") || strings.Contains(line, "apprun") {
return true
}
}
return false
}
// UI Components / UI组件
type UI struct {
config *UIConfig
spinner *ProgressSpinner
}
func NewUI(config *UIConfig) *UI {
return &UI{
config: config,
spinner: NewProgressSpinner(""),
}
}
func (ui *UI) showProgress(message string) {
ui.spinner.message = message
ui.spinner.Start()
defer ui.spinner.Stop()
ticker := time.NewTicker(ui.config.Spinner.Delay)
defer ticker.Stop()
for i := 0; i < 15; i++ {
<-ticker.C
ui.spinner.Spin()
}
}
// Display Functions / 显示函数
func showSuccess() {
text := texts[currentLanguage]
successColor := color.New(color.FgGreen, color.Bold)
warningColor := color.New(color.FgYellow, color.Bold)
pathColor := color.New(color.FgCyan)
// Clear any previous output
fmt.Println()
if currentLanguage == EN {
// English messages with extra spacing
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
successColor.Printf("%s\n", text.SuccessMessage)
fmt.Println()
warningColor.Printf("%s\n", text.RestartMessage)
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
} else {
// Chinese messages with extra spacing
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
successColor.Printf("%s\n", text.SuccessMessage)
fmt.Println()
warningColor.Printf("%s\n", text.RestartMessage)
successColor.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
}
// Add spacing before config location
fmt.Println()
username := os.Getenv("SUDO_USER")
if username == "" {
user, err := user.Current()
if err != nil {
panic(err)
}
username = user.Username
}
if configPath, err := getConfigPath(username); err == nil {
pathColor.Printf("%s\n%s\n", text.ConfigLocation, configPath)
}
}
func showReadOnlyMessage() {
if *setReadOnly {
warningColor := color.New(color.FgYellow, color.Bold)
warningColor.Printf("%s\n", texts[currentLanguage].SetReadOnlyMessage)
fmt.Println("Press Enter to continue...")
bufio.NewReader(os.Stdin).ReadString('\n')
}
}
func showPrivilegeError() {
text := texts[currentLanguage]
red := color.New(color.FgRed, color.Bold)
yellow := color.New(color.FgYellow)
if currentLanguage == EN {
red.Println(text.PrivilegeError)
if runtime.GOOS == "windows" {
yellow.Println(text.RunAsAdmin)
} else {
yellow.Printf("%s\n%s\n", text.RunWithSudo, fmt.Sprintf(text.SudoExample, os.Args[0]))
}
} else {
red.Printf("\n%s\n", text.PrivilegeError)
if runtime.GOOS == "windows" {
yellow.Printf("%s\n", text.RunAsAdmin)
} else {
yellow.Printf("%s\n%s\n", text.RunWithSudo, fmt.Sprintf(text.SudoExample, os.Args[0]))
}
}
}
// System Functions / 系统函数
func checkAdminPrivileges() (bool, error) {
switch runtime.GOOS {
case "windows":
// 使用更可靠的方法检查Windows管理员权限
cmd := exec.Command("net", "session")
err := cmd.Run()
if err == nil {
return true, nil
}
// 如果命令执行失败,说明没有管理员权限
return false, nil
case "darwin", "linux":
currentUser, err := user.Current()
if err != nil {
return false, fmt.Errorf("failed to get current user: %v", err)
}
return currentUser.Uid == "0", nil
default:
return false, fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
}
func detectLanguage() Language {
// Check common environment variables
for _, envVar := range []string{"LANG", "LANGUAGE", "LC_ALL"} {
if lang := os.Getenv(envVar); lang != "" {
if strings.Contains(strings.ToLower(lang), "zh") {
return CN
}
}
}
// Windows-specific language check
if runtime.GOOS == "windows" {
cmd := exec.Command("powershell", "-Command",
"[System.Globalization.CultureInfo]::CurrentUICulture.Name")
output, err := cmd.Output()
if err == nil {
lang := strings.ToLower(strings.TrimSpace(string(output)))
if strings.HasPrefix(lang, "zh") {
return CN
}
}
// Check Windows locale
cmd = exec.Command("wmic", "os", "get", "locale")
output, err = cmd.Output()
if err == nil && strings.Contains(string(output), "2052") {
return CN
}
}
// Check Unix locale
if runtime.GOOS != "windows" {
cmd := exec.Command("locale")
output, err := cmd.Output()
if err == nil && strings.Contains(strings.ToLower(string(output)), "zh_cn") {
return CN
}
}
return EN
}
func selfElevate() error {
switch runtime.GOOS {
case "windows":
// Set automated mode for the elevated process
os.Setenv("AUTOMATED_MODE", "1")
verb := "runas"
exe, _ := os.Executable()
cwd, _ := os.Getwd()
args := strings.Join(os.Args[1:], " ")
cmd := exec.Command("cmd", "/C", "start", verb, exe, args)
cmd.Dir = cwd
return cmd.Run()
case "darwin", "linux":
// Set automated mode for the elevated process
os.Setenv("AUTOMATED_MODE", "1")
exe, err := os.Executable()
if err != nil {
return err
}
cmd := exec.Command("sudo", append([]string{exe}, os.Args[1:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
default:
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
}
// Utility Functions / 实用函数
func handleError(err error) {
if err == nil {
return
}
logger := log.New(os.Stderr, "", log.LstdFlags)
switch e := err.(type) {
case *AppError:
logger.Printf("[ERROR] %v\n", e)
if e.Type == ErrPermission {
showPrivilegeError()
}
default:
logger.Printf("[ERROR] Unexpected error: %v\n", err)
}
}
func waitExit() {
// Skip waiting in automated mode
if os.Getenv("AUTOMATED_MODE") == "1" {
return
}
if currentLanguage == EN {
fmt.Println("\nPress Enter to exit...")
} else {
fmt.Println("\n按��车键退出程序...")
}
os.Stdout.Sync()
bufio.NewReader(os.Stdin).ReadString('\n')
}
// Add this new function near the other process management functions
func ensureCursorClosed() error {
maxAttempts := 3
text := texts[currentLanguage]
showProcessStatus(text.CheckingProcesses)
for attempt := 1; attempt <= maxAttempts; attempt++ {
if !checkCursorRunning() {
showProcessStatus(text.ProcessesClosed)
fmt.Println() // New line after status
return nil
}
if currentLanguage == EN {
showProcessStatus(fmt.Sprintf("Please close Cursor before continuing. Attempt %d/%d\n%s",
attempt, maxAttempts, text.PleaseWait))
} else {
showProcessStatus(fmt.Sprintf("请在继续之前关闭 Cursor。尝试 %d/%d\n%s",
attempt, maxAttempts, text.PleaseWait))
}
time.Sleep(5 * time.Second)
}
return errors.New("cursor is still running")
}
func main() {
// Initialize error recovery
defer func() {
if r := recover(); r != nil {
log.Printf("Panic recovered: %v\n", r)
debug.PrintStack()
waitExit()
}
}()
flag.Parse()
showReadOnlyMessage()
var username string
if username = os.Getenv("SUDO_USER"); username == "" {
user, err := user.Current()
if err != nil {
panic(err)
}
username = user.Username
}
log.Println("Current user: ", username)
// Initialize configuration
ui := NewUI(&UIConfig{
Language: detectLanguage(),
Theme: "default",
Spinner: SpinnerConfig{
Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
Delay: 100 * time.Millisecond,
},
})
// Check privileges
os.Stdout.Sync()
currentLanguage = detectLanguage()
log.Println("Current language: ", currentLanguage)
isAdmin, err := checkAdminPrivileges()
if err != nil {
handleError(err)
waitExit()
return
}
if !isAdmin && runtime.GOOS == "windows" {
if currentLanguage == EN {
fmt.Println("\nRequesting administrator privileges...")
} else {
fmt.Println("\n请求管理员权限...")
}
if err := selfElevate(); err != nil {
handleError(err)
showPrivilegeError()
waitExit()
return
}
return
} else if !isAdmin {
showPrivilegeError()
waitExit()
return
}
// Ensure all Cursor instances are closed
if err := ensureCursorClosed(); err != nil {
if currentLanguage == EN {
fmt.Println("\nError: Please close Cursor manually before running this program.")
} else {
fmt.Println("\n错误:请在运���此程序之前手动关闭 Cursor。")
}
waitExit()
return
}
// Process management
pm := &ProcessManager{
config: &SystemConfig{
RetryAttempts: 3,
RetryDelay: time.Second,
Timeout: 30 * time.Second,
},
}
if checkCursorRunning() {
text := texts[currentLanguage]
showProcessStatus(text.ClosingProcesses)
if err := pm.killCursorProcesses(); err != nil {
fmt.Println() // New line after status
if currentLanguage == EN {
fmt.Println("Warning: Could not close all Cursor instances. Please close them manually.")
} else {
fmt.Println("警告:无法关闭所有 Cursor 实例,请手动关闭。")
}
waitExit()
return
}
time.Sleep(2 * time.Second)
if checkCursorRunning() {
fmt.Println() // New line after status
if currentLanguage == EN {
fmt.Println("\nWarning: Cursor is still running. Please close it manually.")
} else {
fmt.Println("\n警告:Cursor 仍在运行,请手动关闭。")
}
waitExit()
return
}
showProcessStatus(text.ProcessesClosed)
fmt.Println() // New line after status
}
// Clear screen and show banner
clearScreen()
printCyberpunkBanner()
// Read and update configuration
oldConfig, err := readExistingConfig(username) // add username parameter
if err != nil {
oldConfig = nil
}
storageConfig, err := loadAndUpdateConfig(ui, username) // add username parameter
if err != nil {
handleError(err)
waitExit()
return
}
// Show changes and save
showIdComparison(oldConfig, storageConfig)
if err := saveConfig(storageConfig, username); err != nil { // add username parameter
handleError(err)
waitExit()
return
}
// Show success and exit
showSuccess()
if currentLanguage == EN {
fmt.Println("\nOperation completed!")
} else {
fmt.Println("\n操作完成!")
}
// Check if running in automated mode
if os.Getenv("AUTOMATED_MODE") == "1" {
return
}
waitExit()
}
// Progress spinner functions / 进度条函数
func NewProgressSpinner(message string) *ProgressSpinner {
return &ProgressSpinner{
frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
message: message,
}
}
func (s *ProgressSpinner) Spin() {
frame := s.frames[s.current%len(s.frames)]
s.current++
fmt.Printf("\r%s %s", color.CyanString(frame), s.message)
}
func (s *ProgressSpinner) Stop() {
fmt.Println()
}
func (s *ProgressSpinner) Start() {
s.current = 0
}
// Display utility functions / 显示工具函数
func clearScreen() {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "cls")
} else {
cmd = exec.Command("clear")
}
cmd.Stdout = os.Stdout
cmd.Run()
}
func printCyberpunkBanner() {
cyan := color.New(color.FgCyan, color.Bold)
yellow := color.New(color.FgYellow, color.Bold)
magenta := color.New(color.FgMagenta, color.Bold)
green := color.New(color.FgGreen, color.Bold)
banner := `
██████╗██╗ ██╗██████╗ ███████╗ ██████╗ ██████╗
██╔════╝██║ ██║██╔══██╗██╔════╝██╔═══██╗██╔══██╗
██║ ██║ ██║██████╔╝███████╗██║ ██║█████╔╝
██║ ██║ ██║██╔══██╗╚════██║██║ ██║██╔══██╗
╚██████╗╚██████╔╝██║ ██║███████║╚██████╔╝██║ ██║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝
`
cyan.Println(banner)
yellow.Printf("\t\t>> Cursor ID Modifier %s <<\n", version)
magenta.Println("\t\t [ By Pancake Fruit Rolled Shark Chili ]")
langText := "当前语言/Language: "
if currentLanguage == CN {
langText += "简体中文"
} else {
langText += "English"
}
green.Printf("\n\t\t %s\n\n", langText)
}
func showIdComparison(oldConfig *StorageConfig, newConfig *StorageConfig) {