add safe mode

parent 2df20d57
...@@ -29,6 +29,12 @@ Hidden debug command: ...@@ -29,6 +29,12 @@ Hidden debug command:
ximper-shell-panel generate ximper-shell-panel generate
``` ```
Safe mode ignores user config, user modules, and user styles:
```sh
XIMPER_SHELL_SAFEMODE=1 ximper-shell-panel restart
```
Supported positions: Supported positions:
```text ```text
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
const ( const (
...@@ -15,6 +16,7 @@ const ( ...@@ -15,6 +16,7 @@ const (
ModulesFile = SystemWaybarDir + "/modules.json" ModulesFile = SystemWaybarDir + "/modules.json"
StyleFile = SystemWaybarDir + "/style.css" StyleFile = SystemWaybarDir + "/style.css"
DarkStyleFile = SystemWaybarDir + "/style-dark.css" DarkStyleFile = SystemWaybarDir + "/style-dark.css"
SafeModeEnv = "XIMPER_SHELL_SAFEMODE"
) )
type Config struct { type Config struct {
...@@ -91,6 +93,16 @@ func UserDarkStylePath() string { ...@@ -91,6 +93,16 @@ func UserDarkStylePath() string {
return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "style-dark.css") return filepath.Join(os.Getenv("HOME"), ".config", "ximper-shell", "panel", "style-dark.css")
} }
func SafeMode() bool {
value := strings.ToLower(strings.TrimSpace(os.Getenv(SafeModeEnv)))
switch value {
case "", "0", "false", "no", "off":
return false
default:
return true
}
}
func RuntimeDir() string { func RuntimeDir() string {
return filepath.Join(os.TempDir(), "ximper-shell", "panel") return filepath.Join(os.TempDir(), "ximper-shell", "panel")
} }
......
...@@ -109,26 +109,31 @@ func WriteRuntimeStyles() error { ...@@ -109,26 +109,31 @@ func WriteRuntimeStyles() error {
if err := os.MkdirAll(RuntimeWaybarDir(), 0755); err != nil { if err := os.MkdirAll(RuntimeWaybarDir(), 0755); err != nil {
return err return err
} }
if err := writeRuntimeStyle(RuntimeStylePath("style.css"), StyleFile, UserStylePath()); err != nil { userStyle := UserStylePath()
return err
}
userDarkStyle := UserStylePath() userDarkStyle := UserStylePath()
if fileExists(UserDarkStylePath()) { if SafeMode() {
userStyle = ""
userDarkStyle = ""
} else if fileExists(UserDarkStylePath()) {
userDarkStyle = UserDarkStylePath() userDarkStyle = UserDarkStylePath()
} }
if err := writeRuntimeStyle(RuntimeStylePath("style.css"), StyleFile, userStyle); err != nil {
return err
}
return writeRuntimeStyle(RuntimeStylePath("style-dark.css"), DarkStyleFile, userDarkStyle) return writeRuntimeStyle(RuntimeStylePath("style-dark.css"), DarkStyleFile, userDarkStyle)
} }
func writeRuntimeStyle(runtimePath, systemPath, userPath string) error { func writeRuntimeStyle(runtimePath, systemPath, userPath string) error {
data := "@import url(\"" + systemPath + "\");\n" data := "@import url(\"" + systemPath + "\");\n"
if fileExists(userPath) { if userPath != "" && fileExists(userPath) {
data += "@import url(\"" + userPath + "\");\n" data += "@import url(\"" + userPath + "\");\n"
} }
return os.WriteFile(runtimePath, []byte(data), 0644) return os.WriteFile(runtimePath, []byte(data), 0644)
} }
func loadInputs() (Config, Registry, error) { func loadInputs() (Config, Registry, error) {
cfg, err := LoadConfig(UserConfigPath()) cfg, err := loadConfig()
if err != nil { if err != nil {
return Config{}, Registry{}, err return Config{}, Registry{}, err
} }
...@@ -140,7 +145,17 @@ func loadInputs() (Config, Registry, error) { ...@@ -140,7 +145,17 @@ func loadInputs() (Config, Registry, error) {
return cfg, registry, nil return cfg, registry, nil
} }
func loadConfig() (Config, error) {
if SafeMode() {
return DefaultConfig(), nil
}
return LoadConfig(UserConfigPath())
}
func loadRegistry() (Registry, error) { func loadRegistry() (Registry, error) {
if SafeMode() {
return LoadRegistry(ModulesFile)
}
return LoadMergedRegistry(ModulesFile, UserModulesPath()) return LoadMergedRegistry(ModulesFile, UserModulesPath())
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment