parent
cb21c3a498
commit
5822152537
@ -1,3 +1,4 @@
|
||||
.bin
|
||||
.vscode
|
||||
riffraff
|
||||
riffraff
|
||||
data.json
|
@ -1,58 +0,0 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ListenAddress string `json:"listenAddress"`
|
||||
DataPath string `json:"dataPath"`
|
||||
Shortcuts Shortcuts `json:"shortcuts"`
|
||||
}
|
||||
|
||||
func LoadConfig(configPath string) (Config, error) {
|
||||
var configuration Config
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return configuration, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
|
||||
if err := decoder.Decode(&configuration); err != nil {
|
||||
return configuration, err
|
||||
}
|
||||
|
||||
if configuration.Shortcuts == nil {
|
||||
configuration.Shortcuts = make(map[string]string)
|
||||
}
|
||||
|
||||
|
||||
if _, ok := configuration.Shortcuts["*"]; !ok {
|
||||
configuration.Shortcuts["*"] = DefaultSearchProvider
|
||||
}
|
||||
|
||||
configuration.Shortcuts["help"] = "/"
|
||||
|
||||
if configuration.ListenAddress == "" {
|
||||
configuration.ListenAddress = "127.0.0.1"
|
||||
}
|
||||
|
||||
if configuration.DataPath == "" {
|
||||
configuration.DataPath = "."
|
||||
}
|
||||
|
||||
return configuration, nil
|
||||
}
|
||||
|
||||
func SaveShortcuts(path string, shorts Shortcuts) error {
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func LoadShortcuts(path string) (Shortcuts, error) {
|
||||
return nil, nil
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
"log"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type ShortcutStore struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
type ShortcutData struct {
|
||||
Shortcuts Shortcuts `json:"shortcuts"`
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) Init() error {
|
||||
defaultSS := NewDefaultShortcuts()
|
||||
if _, err := os.Stat(ss.Path); os.IsNotExist(err) {
|
||||
log.Printf("file doesn't exist %s: %v", ss.Path, err)
|
||||
if err := ss.SaveShortcuts(defaultSS); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
if _, err := ss.LoadShortcuts(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) SaveShortcuts(shorts Shortcuts) error {
|
||||
// remove help shortcut
|
||||
|
||||
file, err := os.Create(ss.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
temp := Shortcuts{}
|
||||
|
||||
for k, v := range shorts {
|
||||
if k != "help" {
|
||||
temp[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
sc := ShortcutData{ Shortcuts: temp }
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", "\t")
|
||||
if err := encoder.Encode(&sc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) LoadShortcuts() (Shortcuts, error) {
|
||||
var sc ShortcutData
|
||||
file, err := os.Open(ss.Path)
|
||||
if err != nil {
|
||||
if !os.IsExist(err) {
|
||||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
return sc.Shortcuts, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
finfo, err := file.Stat()
|
||||
if err != nil {
|
||||
return sc.Shortcuts, err
|
||||
}
|
||||
|
||||
if finfo.Size() == 0 {
|
||||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
|
||||
if err := decoder.Decode(&sc); err != nil {
|
||||
return sc.Shortcuts, err
|
||||
}
|
||||
|
||||
if sc.Shortcuts == nil {
|
||||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
|
||||
sc.Shortcuts["help"] = "/"
|
||||
|
||||
if v, ok := sc.Shortcuts["*"]; !ok || v == "" {
|
||||
sc.Shortcuts["*"] = DefaultSearchProvider
|
||||
}
|
||||
|
||||
return sc.Shortcuts, nil
|
||||
}
|
Loading…
Reference in new issue