-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcurrenttheme.go
46 lines (39 loc) · 1.33 KB
/
currenttheme.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
package ffcss
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
// CurrentThemeByProfile returns a map mapping a profile path to its current theme's name.
func CurrentThemeByProfile() (map[string]string, error) {
currentThemesRaw, err := os.ReadFile(ConfigDir("currently.yaml"))
if os.IsNotExist(err) {
err = os.WriteFile(ConfigDir("currently.yaml"), []byte(""), 0777)
if err != nil {
return nil, fmt.Errorf("while creating current themes list file: %w", err)
}
} else if err != nil {
return nil, fmt.Errorf("while reading current themes list: %w", err)
}
currentThemes := make(map[string]string)
yaml.Unmarshal(currentThemesRaw, ¤tThemes)
return currentThemes, nil
}
// RegisterCurrentTheme writes the currently.yaml file in ffcss' configuration to update
// what ffcss considers to be the current theme for that profile.
func (ffp FirefoxProfile) RegisterCurrentTheme(themeName string) error {
currentThemes, err := CurrentThemeByProfile()
if err != nil {
return err
}
currentThemes[ffp.FullName()] = themeName
currentThemesNewContents, err := yaml.Marshal(currentThemes)
if err != nil {
return fmt.Errorf("while marshaling into YAML: %w", err)
}
err = os.WriteFile(ConfigDir("currently.yaml"), currentThemesNewContents, 0777)
if err != nil {
return fmt.Errorf("while writing new contents: %w", err)
}
return nil
}