Skip to content

Commit

Permalink
Read defaults from /etc if available (#107)
Browse files Browse the repository at this point in the history
* Read defaults from /etc if available

* Don't return there
  • Loading branch information
kivutar authored Jan 1, 2019
1 parent 300c86c commit 106255a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
7 changes: 0 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ func runLoop(vid *video.Video) {

func main() {
var GLVersion uint
var fullscreen bool
flag.StringVar(&state.Global.CorePath, "L", "", "Path to the libretro core")
flag.BoolVar(&state.Global.Verbose, "v", false, "Verbose logs")
flag.UintVar(&GLVersion, "glver", 32, "OpenGL version")
flag.BoolVar(&fullscreen, "fullscreen", false, "Force starting in full screen mode")
flag.Parse()
args := flag.Args()

Expand All @@ -71,11 +69,6 @@ func main() {
log.Println("[Settings]: Using default settings")
}

if fullscreen {
settings.Current.VideoFullscreen = fullscreen
settings.Save()
}

if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize glfw:", err)
}
Expand Down
15 changes: 10 additions & 5 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ var Defaults = defaultSettings()
// If the settings file doesn't exists, it will return an error and
// set all the settings to their default value.
func Load() error {
defer Save()

usr, _ := user.Current()

// Set default values for settings
Current = Defaults

usr, err := user.Current()
if err != nil {
return err
// If /etc/ludo.json exists, override the defaults
if _, err := os.Stat("/etc/ludo.json"); !os.IsNotExist(err) {
b, _ := ioutil.ReadFile("/etc/ludo.json")
json.Unmarshal(b, &Current)
}

b, err := ioutil.ReadFile(usr.HomeDir + "/.ludo/settings.json")
Expand All @@ -59,15 +64,15 @@ func Load() error {
}
err = json.Unmarshal(b, &Current)

Save()

return err
}

// Save saves the current configuration to the home directory
func Save() error {
usr, _ := user.Current()

os.MkdirAll(usr.HomeDir+"/.ludo", os.ModePerm)

b, _ := json.MarshalIndent(Current, "", " ")
f, err := os.Create(usr.HomeDir + "/.ludo/settings.json")
if err != nil {
Expand Down

0 comments on commit 106255a

Please sign in to comment.