mirror of
https://github.com/yuaotian/go-cursor-help.git
synced 2025-06-08 12:32:06 +08:00
feat: add option to set storage.json as read-only.
1. Default does not set storage.json file to read-only mode 2. Default retains other configuration information in storage.json
This commit is contained in:
parent
5884f82d66
commit
791156055f
80
main.go
80
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -60,6 +61,7 @@ type (
|
|||||||
ClosingProcesses string
|
ClosingProcesses string
|
||||||
ProcessesClosed string
|
ProcessesClosed string
|
||||||
PleaseWait string
|
PleaseWait string
|
||||||
|
SetReadOnlyMessage string
|
||||||
}
|
}
|
||||||
|
|
||||||
// StorageConfig optimized storage configuration / 优化的存储配置
|
// StorageConfig optimized storage configuration / 优化的存储配置
|
||||||
@ -132,6 +134,7 @@ var (
|
|||||||
ClosingProcesses: "正在关闭 Cursor 实例...",
|
ClosingProcesses: "正在关闭 Cursor 实例...",
|
||||||
ProcessesClosed: "所有 Cursor 实例已关闭",
|
ProcessesClosed: "所有 Cursor 实例已关闭",
|
||||||
PleaseWait: "请稍候...",
|
PleaseWait: "请稍候...",
|
||||||
|
SetReadOnlyMessage: "设置 storage.json 为只读模式, 这将导致 workspace 记录信息丢失等问题",
|
||||||
},
|
},
|
||||||
EN: {
|
EN: {
|
||||||
SuccessMessage: "[√] Configuration file updated successfully!",
|
SuccessMessage: "[√] Configuration file updated successfully!",
|
||||||
@ -149,10 +152,13 @@ var (
|
|||||||
ClosingProcesses: "Closing Cursor instances...",
|
ClosingProcesses: "Closing Cursor instances...",
|
||||||
ProcessesClosed: "All Cursor instances have been closed",
|
ProcessesClosed: "All Cursor instances have been closed",
|
||||||
PleaseWait: "Please wait...",
|
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 / 错误实现
|
// Error Implementation / 错误实现
|
||||||
func (e *AppError) Error() string {
|
func (e *AppError) Error() string {
|
||||||
if e.Context != nil {
|
if e.Context != nil {
|
||||||
@ -225,16 +231,6 @@ func saveConfig(config *StorageConfig, username string) error { // Modified to t
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := json.MarshalIndent(config, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return &AppError{
|
|
||||||
Type: ErrSystem,
|
|
||||||
Op: "generate JSON",
|
|
||||||
Path: "",
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create parent directories with proper permissions
|
// Create parent directories with proper permissions
|
||||||
dir := filepath.Dir(configPath)
|
dir := filepath.Dir(configPath)
|
||||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
@ -256,9 +252,53 @@ func saveConfig(config *StorageConfig, username string) error { // Modified to t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
originalFileStat, err := os.Stat(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return &AppError{
|
||||||
|
Type: ErrSystem,
|
||||||
|
Op: "get file mode",
|
||||||
|
Path: configPath,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
originalFileMode := originalFileStat.Mode()
|
||||||
|
|
||||||
|
originalFileContent, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return &AppError{
|
||||||
|
Type: ErrSystem,
|
||||||
|
Op: "read original file",
|
||||||
|
Path: configPath,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var originalFile map[string]any
|
||||||
|
if err := json.Unmarshal(originalFileContent, &originalFile); err != nil {
|
||||||
|
return &AppError{
|
||||||
|
Type: ErrSystem,
|
||||||
|
Op: "unmarshal original file",
|
||||||
|
Path: configPath,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
originalFile["telemetry.sqmId"] = config.TelemetrySqmId
|
||||||
|
originalFile["telemetry.macMachineId"] = config.TelemetryMacMachineId
|
||||||
|
originalFile["telemetry.machineId"] = config.TelemetryMachineId
|
||||||
|
originalFile["telemetry.devDeviceId"] = config.TelemetryDevDeviceId
|
||||||
|
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
|
// Write to temporary file first
|
||||||
tmpPath := configPath + ".tmp"
|
tmpPath := configPath + ".tmp"
|
||||||
if err := os.WriteFile(tmpPath, content, 0666); err != nil {
|
if err := os.WriteFile(tmpPath, newFileContent, 0666); err != nil {
|
||||||
return &AppError{
|
return &AppError{
|
||||||
Type: ErrSystem,
|
Type: ErrSystem,
|
||||||
Op: "write temporary file",
|
Op: "write temporary file",
|
||||||
@ -267,8 +307,12 @@ func saveConfig(config *StorageConfig, username string) error { // Modified to t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *setReadOnly {
|
||||||
|
originalFileMode = 0444
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure proper permissions on temporary file
|
// Ensure proper permissions on temporary file
|
||||||
if err := os.Chmod(tmpPath, 0444); err != nil {
|
if err := os.Chmod(tmpPath, originalFileMode); err != nil {
|
||||||
os.Remove(tmpPath)
|
os.Remove(tmpPath)
|
||||||
return &AppError{
|
return &AppError{
|
||||||
Type: ErrSystem,
|
Type: ErrSystem,
|
||||||
@ -495,6 +539,15 @@ func showSuccess() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
func showPrivilegeError() {
|
||||||
text := texts[currentLanguage]
|
text := texts[currentLanguage]
|
||||||
red := color.New(color.FgRed, color.Bold)
|
red := color.New(color.FgRed, color.Bold)
|
||||||
@ -691,6 +744,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
showReadOnlyMessage()
|
||||||
|
|
||||||
var username string
|
var username string
|
||||||
if username = os.Getenv("SUDO_USER"); username == "" {
|
if username = os.Getenv("SUDO_USER"); username == "" {
|
||||||
user, err := user.Current()
|
user, err := user.Current()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user