fix: macos fix can't open the app

This commit is contained in:
2024-11-16 05:36:54 +07:00
parent ad8b67ccd3
commit b88b04a235
3 changed files with 35 additions and 13 deletions
+22 -3
View File
@@ -3,13 +3,32 @@ package utils
import (
"fmt"
"os"
"path/filepath"
"rul.sh/vaulterm/server/lib"
)
func GetDataPath(resolveFile string) string {
// Resolve the app directory
execPath, err := os.Executable()
if err != nil {
return ""
}
appDir := filepath.Dir(execPath)
if resolveFile == "" {
return appDir
}
return filepath.Join(appDir, resolveFile)
}
func CheckAndCreateEnvFile() error {
// Skip if ENCRYPTION_KEY is set
if os.Getenv("ENCRYPTION_KEY") != "" {
return nil
}
// Check if .env file exists
if _, err := os.Stat(".env"); !os.IsNotExist(err) {
envFile := GetDataPath(".env")
if _, err := os.Stat(envFile); !os.IsNotExist(err) {
return nil
}
@@ -21,11 +40,11 @@ func CheckAndCreateEnvFile() error {
// Write the random key to the .env file
envContent := fmt.Sprintf("ENCRYPTION_KEY=%s\n", randomKey)
err = os.WriteFile(".env", []byte(envContent), 0644)
err = os.WriteFile(envFile, []byte(envContent), 0644)
if err != nil {
return err
}
fmt.Println(".env file created with ENCRYPTION_KEY.")
fmt.Println(".env file created with ENCRYPTION_KEY.")
return nil
}