~adnano/astronaut

ref: c2cbc6b7e5ba6ccdb1e473261a134a4e897e6d46 astronaut/settings.go -rw-r--r-- 734 bytes
c2cbc6b7Adnan Maolood settings: Remove unused code 3 months ago
                                                                                
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
package main

import (
	"encoding/json"
	"io"
	"os"
	"sync"
)

type Settings struct {
	sync.RWMutex
	path    string
	Display struct {
		Graphics bool `json:"graphics"`
		AltText  bool `json:"alt-text"`
	} `json:"display"`
}

// Default applies the default settings.
func (s *Settings) Default() {
	s.Display.Graphics = true
	s.Display.AltText = false
}

// Load loads the settings from the provided path.
func (s *Settings) Load(path string) error {
	s.path = path
	f, err := os.OpenFile(path, os.O_RDWR, 0644)
	if err != nil {
		// Try to create the settings file
		s.Default()
		return s.Save()
	}
	b, err := io.ReadAll(f)
	if err != nil {
		return err
	}
	if err := json.Unmarshal(b, s); err != nil {
		return err
	}
	return nil
}