Add support for specifying a configuration file

master
MendelGusmao 5 years ago
parent b8671b548c
commit 9d93ee47cd
  1. 23
      cmd/mop/main.go
  2. 28
      profile.go

@ -5,11 +5,18 @@
package main package main
import ( import (
`github.com/mop-tracker/mop` "flag"
`github.com/nsf/termbox-go` "os/user"
`time` "path"
"time"
"github.com/mop-tracker/mop"
"github.com/nsf/termbox-go"
) )
// File name in user's home directory where we store the settings.
const defaultProfile = `.moprc`
const help = `Mop v0.2.0 -- Copyright (c) 2013-2016 by Michael Dvorkin. All Rights Reserved. const help = `Mop v0.2.0 -- Copyright (c) 2013-2016 by Michael Dvorkin. All Rights Reserved.
NO WARRANTIES OF ANY KIND WHATSOEVER. SEE THE LICENSE FILE FOR DETAILS. NO WARRANTIES OF ANY KIND WHATSOEVER. SEE THE LICENSE FILE FOR DETAILS.
@ -119,6 +126,14 @@ func main() {
screen := mop.NewScreen() screen := mop.NewScreen()
defer screen.Close() defer screen.Close()
profile := mop.NewProfile() usr, err := user.Current()
if err != nil {
panic(err)
}
profileName := flag.String("profile", path.Join(usr.HomeDir, defaultProfile), "path to profile")
flag.Parse()
profile := mop.NewProfile(*profileName)
mainLoop(screen, profile) mainLoop(screen, profile)
} }

@ -5,15 +5,11 @@
package mop package mop
import ( import (
`encoding/json` "encoding/json"
`io/ioutil` "io/ioutil"
`os/user` "sort"
`sort`
) )
// File name in user's home directory where we store the settings.
const moprc = `/.moprc`
// Profile manages Mop program settings as defined by user (ex. list of // Profile manages Mop program settings as defined by user (ex. list of
// stock tickers). The settings are serialized using JSON and saved in // stock tickers). The settings are serialized using JSON and saved in
// the ~/.moprc file. // the ~/.moprc file.
@ -25,13 +21,14 @@ type Profile struct {
Ascending bool // True when sort order is ascending. Ascending bool // True when sort order is ascending.
Grouped bool // True when stocks are grouped by advancing/declining. Grouped bool // True when stocks are grouped by advancing/declining.
selectedColumn int // Stores selected column number when the column editor is active. selectedColumn int // Stores selected column number when the column editor is active.
filename string // Path to the file in which the configuration is stored
} }
// Creates the profile and attempts to load the settings from ~/.moprc file. // Creates the profile and attempts to load the settings from ~/.moprc file.
// If the file is not there it gets created with default values. // If the file is not there it gets created with default values.
func NewProfile() *Profile { func NewProfile(filename string) *Profile {
profile := &Profile{} profile := &Profile{filename: filename}
data, err := ioutil.ReadFile(profile.defaultFileName()) data, err := ioutil.ReadFile(filename)
if err != nil { // Set default values: if err != nil { // Set default values:
profile.MarketRefresh = 12 // Market data gets fetched every 12s (5 times per minute). profile.MarketRefresh = 12 // Market data gets fetched every 12s (5 times per minute).
profile.QuotesRefresh = 5 // Stock quotes get updated every 5s (12 times per minute). profile.QuotesRefresh = 5 // Stock quotes get updated every 5s (12 times per minute).
@ -55,7 +52,7 @@ func (profile *Profile) Save() error {
return err return err
} }
return ioutil.WriteFile(profile.defaultFileName(), data, 0644) return ioutil.WriteFile(profile.filename, data, 0644)
} }
// AddTickers updates the list of existing tikers to add the new ones making // AddTickers updates the list of existing tikers to add the new ones making
@ -123,12 +120,3 @@ func (profile *Profile) Regroup() error {
profile.Grouped = !profile.Grouped profile.Grouped = !profile.Grouped
return profile.Save() return profile.Save()
} }
//-----------------------------------------------------------------------------
func (profile *Profile) defaultFileName() string {
usr, err := user.Current()
if err != nil {
panic(err)
}
return usr.HomeDir + moprc
}

Loading…
Cancel
Save