update formatting with gofmt, and fix lint errors
parent
6a74c5f9f0
commit
685a2896ab
|
|
@ -13,9 +13,9 @@ type Shortcuts map[string]string
|
|||
func NewServer(tp TemplateRenderer, shortcutStore *ShortcutStore, accessLogging bool) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
shorts, _ := shortcutStore.LoadShortcuts()
|
||||
shorts, _ := shortcutStore.LoadShortcuts(nil)
|
||||
ss := &CommandHandler{
|
||||
Mutex: &sync.Mutex{},
|
||||
Mutex: &sync.Mutex{},
|
||||
Shortcuts: shorts,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,8 +84,9 @@ func (c *CommandHandler) updateShortcut(action, shortcut, location string) (Comm
|
|||
}
|
||||
|
||||
func (c *CommandHandler) getShortcut(key string, input ...string) Command {
|
||||
location, ok := c.Shortcuts[key]
|
||||
var parameter string
|
||||
|
||||
location, ok := c.Shortcuts[key]
|
||||
if !ok {
|
||||
location = DefaultSearchProvider
|
||||
key = "*"
|
||||
|
|
@ -105,9 +106,9 @@ func (c *CommandHandler) getShortcut(key string, input ...string) Command {
|
|||
}
|
||||
}
|
||||
|
||||
func NewDefaultShortcuts() (Shortcuts) {
|
||||
func NewDefaultShortcuts() Shortcuts {
|
||||
return Shortcuts{
|
||||
"*": DefaultSearchProvider,
|
||||
"*": DefaultSearchProvider,
|
||||
"help": "/",
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ package internal
|
|||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Handle(t *testing.T) {
|
||||
tests := []struct {
|
||||
testHandleCases := []struct {
|
||||
name string
|
||||
input string
|
||||
want Command
|
||||
|
|
@ -38,11 +38,11 @@ func Test_Handle(t *testing.T) {
|
|||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "add search shortcut: 'add so https://stackvoerflow.com?q=%s'",
|
||||
input: "add so https://stackoverflow.com?q=%s",
|
||||
name: "add search shortcut: 'add so https://stackvoerflow.com?q=%s'",
|
||||
input: "add so https://stackoverflow.com?q=%s",
|
||||
want: Command{
|
||||
Action: "add",
|
||||
Name: "so",
|
||||
Action: "add",
|
||||
Name: "so",
|
||||
Location: "https://stackoverflow.com?q=%s",
|
||||
},
|
||||
},
|
||||
|
|
@ -88,7 +88,8 @@ func Test_Handle(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
for _, testCase := range testHandleCases {
|
||||
tt := testCase
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cm := &CommandHandler{
|
||||
Mutex: &sync.Mutex{},
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"os"
|
||||
"log"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ShortcutStore struct {
|
||||
|
|
@ -16,25 +17,25 @@ type ShortcutData struct {
|
|||
|
||||
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 {
|
||||
|
||||
if err := ss.SaveShortcuts(defaultSS, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
if _, err := ss.LoadShortcuts(); err != nil {
|
||||
if _, err := ss.LoadShortcuts(nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) SaveShortcuts(shorts Shortcuts) error {
|
||||
// remove help shortcut
|
||||
|
||||
func (ss *ShortcutStore) SaveShortcuts(shorts Shortcuts, copyTo io.Writer) error {
|
||||
file, err := os.Create(ss.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -50,29 +51,39 @@ func (ss *ShortcutStore) SaveShortcuts(shorts Shortcuts) error {
|
|||
}
|
||||
}
|
||||
|
||||
sc := ShortcutData{ Shortcuts: temp }
|
||||
sc := ShortcutData{Shortcuts: temp}
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
var target io.Writer
|
||||
if copyTo != nil {
|
||||
target = io.MultiWriter(file, copyTo)
|
||||
} else {
|
||||
target = file
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(target)
|
||||
encoder.SetIndent("", "\t")
|
||||
|
||||
if err := encoder.Encode(&sc); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) LoadShortcuts() (Shortcuts, error) {
|
||||
func (ss *ShortcutStore) LoadShortcuts(copyTo io.Writer) (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
|
||||
|
|
@ -82,9 +93,15 @@ func (ss *ShortcutStore) LoadShortcuts() (Shortcuts, error) {
|
|||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
|
||||
var target io.Reader
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
if copyTo != nil {
|
||||
target = io.TeeReader(file, copyTo)
|
||||
} else {
|
||||
target = file
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(target)
|
||||
if err := decoder.Decode(&sc); err != nil {
|
||||
return sc.Shortcuts, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,9 +72,11 @@ func (tr TemplateRenderer) RenderHandler(filename string, ss *CommandHandler, he
|
|||
return func(res http.ResponseWriter, req *http.Request) {
|
||||
templVar := ToTemplateVars(req, ss.Shortcuts)
|
||||
h := res.Header()
|
||||
|
||||
for k, v := range headers {
|
||||
h[k] = v
|
||||
}
|
||||
|
||||
h.Set("Cache-Control", "max-age 0; no-cache; private")
|
||||
|
||||
if err := tr.Render(filename, templVar, res); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue