commitremotes/origin/HEADaa1027292e
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Tue Nov 19 09:53:21 2019 -0600 tweak into text commit8b1945df7e
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Tue Nov 19 09:31:35 2019 -0600 make -accessLog flag actually work commit685a2896ab
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Tue Nov 19 09:31:07 2019 -0600 update formatting with gofmt, and fix lint errors commit6a74c5f9f0
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Tue Nov 19 08:24:58 2019 -0600 add data generation, version tagging vars to build process commit5822152537
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Tue Nov 19 07:49:22 2019 -0600 add saving shortcuts to local db file commitcb21c3a498
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Mon Nov 18 23:51:21 2019 -0600 added bind address flag commit547bec5eb7
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Mon Nov 18 22:37:06 2019 -0600 refactor shortcut code to be a little nicer commit15667ca704
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Mon Nov 18 00:48:06 2019 -0600 add some stubs for saving user updates to a data file commit4e74fd955b
Author: Adam Veldhousen <adam.veldhousen@liveauctioneers.com> Date: Mon Nov 18 00:30:29 2019 -0600 added config file loading, fixed bug with search shortcuts
parent
a57f276257
commit
7bb5ad1b5c
@ -1,2 +1,4 @@
|
||||
.bin
|
||||
.vscode
|
||||
.vscode
|
||||
riffraff
|
||||
data.json
|
@ -0,0 +1,120 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
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, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := ss.LoadShortcuts(nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *ShortcutStore) SaveShortcuts(shorts Shortcuts, copyTo io.Writer) error {
|
||||
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}
|
||||
|
||||
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 nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if finfo.Size() == 0 {
|
||||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
|
||||
var target io.Reader
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if sc.Shortcuts == nil {
|
||||
return NewDefaultShortcuts(), nil
|
||||
}
|
||||
|
||||
sc.Shortcuts["help"] = "/"
|
||||
|
||||
if v, ok := sc.Shortcuts["*"]; !ok || v == "" {
|
||||
sc.Shortcuts["*"] = DefaultSearchProvider
|
||||
}
|
||||
|
||||
return sc.Shortcuts, nil
|
||||
}
|
@ -1,32 +1,74 @@
|
||||
APP := riffraff
|
||||
PKGS := $(shell go list ./... | grep -v vendor)
|
||||
GOBIN := $(GOPATH)/bin
|
||||
LINTBIN := $(GOBIN)/golangci-lint
|
||||
OUTDIR := .bin
|
||||
BINARY := $(OUTDIR)/$(app)
|
||||
|
||||
dev: clean .bin/$(APP)-dev
|
||||
./.bin/$(APP)-dev -port 8080 -accesslog=true
|
||||
GIT_SHA := $$(git rev-parse HEAD)
|
||||
GIT_BRANCH := $$(git rev-parse --abbrev-ref HEAD)
|
||||
VERSION := $(git desribe)
|
||||
|
||||
|
||||
define SHORTCUT_DATA
|
||||
{
|
||||
"shortcuts": {
|
||||
"*": "https://duckduckgo.com/%s",
|
||||
"fb": "https://facebook.com",
|
||||
"gh": "https://github.com",
|
||||
"gitemoji": "https://www.webfx.com/tools/emoji-cheat-sheet/"
|
||||
}
|
||||
}
|
||||
endef
|
||||
export SHORTCUT_DATA
|
||||
|
||||
dev: clean $(BINARY)dev
|
||||
./$(BINARY)-dev -port 8080 -accesslog=true -data=data.json
|
||||
|
||||
test: lint
|
||||
go test -v -cover ./...
|
||||
|
||||
lint: $(LINTBIN)
|
||||
$(LINTBIN) run -p format -p unused -p bugs -p performance
|
||||
|
||||
$(LINTBIN):
|
||||
@GO111MODULE=off go get github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||
|
||||
package:
|
||||
docker build --build-arg VERSION=$${VERSION:-dev} -t vdhsn/$(APP):$${VERSION:-dev} .
|
||||
docker build --build-arg VERSION=$(VERSION) \
|
||||
--build-arg COMMIT=$(GIT_SHA) \
|
||||
-t vdhsn/$(APP):$(VERSION) .
|
||||
docker tag vdhsn/$(APP):$(VERSION) vdhsn/$(APP):$(GIT_SHA)
|
||||
docker tag vdhsn/$(APP):$(VERSION) vdhsn/$(APP):$(GIT_BRANCH)
|
||||
|
||||
package-run:
|
||||
docker run -it --rm --name riffraff -u 1000:1000 -p 8080:8080 vdhsn/$(APP):$${VERSION:-dev}
|
||||
docker run -it --rm --name riffraff -u 1000:1000 -p 8080:8080 vdhsn/$(APP):$(GIT_SHA)
|
||||
|
||||
publish: package
|
||||
docker push vdhsn/$(APP):$${VERSION:-dev}
|
||||
docker push vdhsn/$(APP):$(VERSION)
|
||||
docker push vdhsn/$(APP):$(GIT_BRANCH)
|
||||
|
||||
build: .bin/$(APP)
|
||||
build: $(BINARY)
|
||||
|
||||
.bin:
|
||||
mkdir .bin
|
||||
$(OUTDIR):
|
||||
mkdir $@
|
||||
|
||||
.bin/$(APP): packr .bin
|
||||
packr build -o .bin/$(APP) -v .
|
||||
$(BINARY): packr .bin
|
||||
packr build -o $@ -v .
|
||||
|
||||
.bin/$(APP)-dev: .bin
|
||||
go build -o .bin/$(APP)-dev -v .
|
||||
$(BINARY)-dev: .bin
|
||||
go build -o $(BINARY)-dev -v .
|
||||
|
||||
clean:
|
||||
rm -rf .bin
|
||||
|
||||
clobber: clean
|
||||
rm -rf data.json
|
||||
|
||||
data.json:
|
||||
@echo "$${SHORTCUT_DATA}" > data.json
|
||||
|
||||
packr:
|
||||
go get -u github.com/gobuffalo/packr/packr
|
||||
|
||||
.PHONY: build clean dev package package-run publish packr
|
||||
.PHONY: build clean clobber dev lint package package-run publish packr test
|
||||
|
Loading…
Reference in new issue