Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to make history persistent #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (d *Duration) UnmarshalText(text []byte) error {

type Config struct {
General GeneralOptions
Meta MetaOptions
Keys map[string]map[string]string
}

Expand All @@ -39,11 +40,16 @@ type GeneralOptions struct {
Insecure bool
PreserveScrollPosition bool
FollowRedirects bool
PersistHistory bool
DefaultURLScheme string
TLSVersionMin uint16
TLSVersionMax uint16
}

type MetaOptions struct {
ConfigLocation string
}

var defaultTimeoutDuration, _ = time.ParseDuration("1m")

var DefaultKeys = map[string]map[string]string{
Expand Down Expand Up @@ -98,8 +104,12 @@ var DefaultConfig = Config{
Insecure: false,
PreserveScrollPosition: true,
FollowRedirects: true,
PersistHistory: false,
DefaultURLScheme: "https",
},
Meta: MetaOptions{
ConfigLocation: "",
},
}

func LoadConfig(configFile string) (*Config, error) {
Expand All @@ -114,6 +124,8 @@ func LoadConfig(configFile string) (*Config, error) {
return nil, err
}

conf.Meta.ConfigLocation = configFile

if conf.Keys == nil {
conf.Keys = DefaultKeys
} else {
Expand Down
78 changes: 78 additions & 0 deletions wuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -12,6 +13,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
Expand Down Expand Up @@ -785,6 +787,7 @@ func (a *App) SubmitRequest(g *gocui.Gui, _ *gocui.View) error {
// add to history
a.history = append(a.history, r)
a.historyIndex = len(a.history) - 1
a.SaveHistory()

// render response
g.Execute(func(g *gocui.Gui) error {
Expand Down Expand Up @@ -1124,6 +1127,72 @@ func (a *App) CreatePopupView(name string, width, height int, g *gocui.Gui) (v *
return
}

func (a *App) SerializeHistory() ([]byte, error) {
return json.Marshal(a.history)
}

func (a *App) DeserializeHistory(input []byte) ([]*Request, error) {
history := []*Request{}
err := json.Unmarshal(input, &history)
return history, err
}

func (a *App) GetHistoryFile() (string, error) {
if a.config.Meta.ConfigLocation == "" {
return "", errors.New("There is no known config directory.")
}
dir := filepath.Dir(a.config.Meta.ConfigLocation)
histFile := filepath.Join(dir, "history.json")
return histFile, nil
}

func (a *App) SaveHistory() error {
if !a.config.General.PersistHistory {
return nil
}

histFile, err := a.GetHistoryFile()
if err != nil {
return err
}

serializedHistory, err := a.SerializeHistory()
if err != nil {
return err
}

err = ioutil.WriteFile(histFile, serializedHistory, 0644)
if err != nil {
return err
}

return nil
}

func (a *App) LoadHistory() error {
if !a.config.General.PersistHistory {
return nil
}

histFile, err := a.GetHistoryFile()
if err != nil {
return err
}

histBytes, err := ioutil.ReadFile(histFile)
if err != nil {
return err
}

history, err := a.DeserializeHistory(histBytes)
if err != nil {
return err
}

a.history = history
return nil
}

func (a *App) ToggleHistory(g *gocui.Gui, _ *gocui.View) (err error) {
// Destroy if present
if a.currentPopup == HISTORY_VIEW {
Expand Down Expand Up @@ -1604,6 +1673,15 @@ func main() {
os.Exit(1)
}

if app.config.General.PersistHistory {
err = app.LoadHistory()
if err != nil && !os.IsNotExist(err) {
g.Close()
fmt.Println("Error!", err)
os.Exit(1)
}
}

defer g.Close()

if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
Expand Down