Skip to content

Commit

Permalink
First pass at storing user settings separately #2896
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Oct 19, 2015
1 parent 151a104 commit 6b7b35a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/github.com/getlantern/flashlight/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
package settings

import (
"io/ioutil"
"net/http"
"path/filepath"
"sync"

"github.com/getlantern/appdir"
"github.com/getlantern/flashlight/config"
"github.com/getlantern/launcher"
"github.com/getlantern/yaml"

"github.com/getlantern/flashlight/ui"
"github.com/getlantern/golog"
Expand All @@ -22,7 +26,10 @@ var (
cfgMutex sync.RWMutex
settingsMutex sync.RWMutex
baseSettings *Settings
settings *Settings
httpClient *http.Client
name = "settings.yaml"
dir = appdir.General("Lantern")
)

type Settings struct {
Expand All @@ -34,6 +41,26 @@ type Settings struct {
ProxyAll bool
}

func Load(version, revisionDate, buildDate string) {
// Create default settings that may or may not be overridden from an existing file
// on disk.
settings = &Settings{
Version: version,
BuildDate: buildDate,
RevisionDate: revisionDate,
AutoReport: true,
AutoLaunch: true,
ProxyAll: false,
}
path := filepath.Join(dir, name)
if bytes, err := ioutil.ReadFile(path); err != nil {
return
} else if err := yaml.Unmarshal(bytes, settings); err != nil {
log.Errorf("Could not load yaml %v", err)
return
}
}

func Configure(cfg *config.Config, version, revisionDate string, buildDate string) {

cfgMutex.Lock()
Expand Down
40 changes: 40 additions & 0 deletions src/github.com/getlantern/flashlight/settings/settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package settings

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBootstrapSettings(t *testing.T) {
version := "test"
revisionDate := "test"
buildDate := "test"
Load(version, revisionDate, buildDate)

assert.Equal(t, settings.AutoLaunch, true, "Should be set to auto launch")

// Reset the variables for loading the yaml.
name = "test.yaml"
dir = "."
//path := filepath.Join(dir, "test.yaml")

Load(version, revisionDate, buildDate)

assert.Equal(t, settings.AutoLaunch, false, "Should not be set to auto launch")
/*
set := &Settings{
Version: version,
BuildDate: buildDate,
RevisionDate: revisionDate,
AutoReport: true,
AutoLaunch: true,
ProxyAll: false,
}
if bytes, err := yaml.Marshal(set); err != nil {
t.Fail()
} else if err := ioutil.WriteFile(path, bytes, 0644); err != nil {
t.Fail()
}
*/
}

0 comments on commit 6b7b35a

Please sign in to comment.