Make DefaultConfig safer

Instead of having a global mutable DefaultConfig which might be
changed by anything during run/test time turn DefaultConfig into a
function which returns a fresh DefaultConfig. This is safer and more
convenient.
This commit is contained in:
Menno Finlay-Smits 2019-03-12 14:20:33 +13:00
parent 44233f384e
commit 93bf686b9d
3 changed files with 46 additions and 48 deletions

View File

@ -53,7 +53,7 @@ func getConfig() *config.Config {
var conf *config.Config var conf *config.Config
if ignoreConfig { if ignoreConfig {
conf = &config.DefaultConfig conf = config.DefaultConfig()
} else { } else {
conf = loadConfigFile() conf = loadConfigFile()
} }
@ -83,12 +83,12 @@ func loadConfigFile() *config.Config {
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
fmt.Printf("Failed to get current user information: %s\n", err) fmt.Printf("Failed to get current user information: %s\n", err)
return &config.DefaultConfig return config.DefaultConfig()
} }
home := usr.HomeDir home := usr.HomeDir
if home == "" { if home == "" {
return &config.DefaultConfig return config.DefaultConfig()
} }
places := []string{} places := []string{}
@ -111,7 +111,7 @@ func loadConfigFile() *config.Config {
} }
} }
if b, err := config.DefaultConfig.Encode(); err != nil { if b, err := config.DefaultConfig().Encode(); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err) fmt.Printf("Failed to encode config file: %s\n", err)
} else { } else {
err = os.MkdirAll(filepath.Dir(places[0]), 0744) err = os.MkdirAll(filepath.Dir(places[0]), 0744)
@ -124,5 +124,5 @@ func loadConfigFile() *config.Config {
} }
} }
return &config.DefaultConfig return config.DefaultConfig()
} }

View File

@ -24,12 +24,12 @@ type Config struct {
type KeyMappingConfig map[string]string type KeyMappingConfig map[string]string
func Parse(data []byte) (*Config, error) { func Parse(data []byte) (*Config, error) {
c := DefaultConfig c := DefaultConfig()
err := toml.Unmarshal(data, &c) err := toml.Unmarshal(data, c)
if c.KeyMapping == nil { if c.KeyMapping == nil {
c.KeyMapping = KeyMappingConfig(map[string]string{}) c.KeyMapping = KeyMappingConfig(map[string]string{})
} }
return &c, err return c, err
} }
func (c *Config) Encode() ([]byte, error) { func (c *Config) Encode() ([]byte, error) {

View File

@ -2,7 +2,8 @@ package config
import "runtime" import "runtime"
var DefaultConfig = Config{ func DefaultConfig() *Config {
return &Config{
DebugMode: false, DebugMode: false,
ColourScheme: ColourScheme{ ColourScheme: ColourScheme{
Cursor: strToColourNoErr("#e8dfd6"), Cursor: strToColourNoErr("#e8dfd6"),
@ -26,28 +27,25 @@ var DefaultConfig = Config{
White: strToColourNoErr("#ffffff"), White: strToColourNoErr("#ffffff"),
Selection: strToColourNoErr("#333366"), Selection: strToColourNoErr("#333366"),
}, },
KeyMapping: KeyMappingConfig(map[string]string{}), KeyMapping: KeyMappingConfig(map[string]string{
string(ActionCopy): addMod("c"),
string(ActionPaste): addMod("v"),
string(ActionSearch): addMod("g"),
string(ActionToggleDebug): addMod("d"),
string(ActionToggleSlomo): addMod(";"),
string(ActionReportBug): addMod("r"),
string(ActionBufferClear): addMod("k"),
}),
SearchURL: "https://www.google.com/search?q=$QUERY", SearchURL: "https://www.google.com/search?q=$QUERY",
MaxLines: 1000, MaxLines: 1000,
CopyAndPasteWithMouse: true, CopyAndPasteWithMouse: true,
} }
func init() {
DefaultConfig.KeyMapping[string(ActionCopy)] = addMod("c")
DefaultConfig.KeyMapping[string(ActionPaste)] = addMod("v")
DefaultConfig.KeyMapping[string(ActionSearch)] = addMod("g")
DefaultConfig.KeyMapping[string(ActionToggleDebug)] = addMod("d")
DefaultConfig.KeyMapping[string(ActionToggleSlomo)] = addMod(";")
DefaultConfig.KeyMapping[string(ActionReportBug)] = addMod("r")
DefaultConfig.KeyMapping[string(ActionBufferClear)] = addMod("k")
} }
func addMod(keys string) string { func addMod(keys string) string {
standardMod := "ctrl + shift + " standardMod := "ctrl + shift + "
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
standardMod = "super + " standardMod = "super + "
} }
return standardMod + keys return standardMod + keys
} }