File: config.go

package info (click to toggle)
wuzz 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 372 kB
  • sloc: makefile: 6
file content (174 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package config

import (
	"errors"
	"os"
	"path/filepath"
	"runtime"
	"time"

	"github.com/BurntSushi/toml"
	"github.com/mitchellh/go-homedir"
)

var ContentTypes = map[string]string{
	"json":      "application/json",
	"form":      "application/x-www-form-urlencoded",
	"multipart": "multipart/form-data",
}

// Duration is used to automatically unmarshal timeout strings to
// time.Duration values
type Duration struct {
	time.Duration
}

func (d *Duration) UnmarshalText(text []byte) error {
	var err error
	d.Duration, err = time.ParseDuration(string(text))
	return err
}

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

type GeneralOptions struct {
	ContextSpecificSearch  bool
	DefaultURLScheme       string
	Editor                 string
	FollowRedirects        bool
	FormatJSON             bool
	Insecure               bool
	PreserveScrollPosition bool
	StatusLine             string
	TLSVersionMax          uint16
	TLSVersionMin          uint16
	Timeout                Duration
}

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

var DefaultKeys = map[string]map[string]string{
	"global": {
		"CtrlR": "submit",
		"CtrlC": "quit",
		"CtrlS": "saveResponse",
		"CtrlF": "loadRequest",
		"CtrlE": "saveRequest",
		"CtrlD": "deleteLine",
		"CtrlW": "deleteWord",
		"CtrlO": "openEditor",
		"CtrlT": "toggleContextSpecificSearch",
		"CtrlX": "clearHistory",
		"Tab":   "nextView",
		"CtrlJ": "nextView",
		"CtrlK": "prevView",
		"AltH":  "history",
		"F2":    "focus url",
		"F3":    "focus get",
		"F4":    "focus method",
		"F5":    "focus data",
		"F6":    "focus headers",
		"F7":    "focus search",
		"F8":    "focus response-headers",
		"F9":    "focus response-body",
	},
	"url": {
		"Enter": "submit",
	},
	"response-headers": {
		"ArrowUp":   "scrollUp",
		"ArrowDown": "scrollDown",
		"PageUp":    "pageUp",
		"PageDown":  "pageDown",
	},
	"response-body": {
		"ArrowUp":   "scrollUp",
		"ArrowDown": "scrollDown",
		"PageUp":    "pageUp",
		"PageDown":  "pageDown",
	},
	"help": {
		"ArrowUp":   "scrollUp",
		"ArrowDown": "scrollDown",
		"PageUp":    "pageUp",
		"PageDown":  "pageDown",
	},
}

var DefaultConfig = Config{
	General: GeneralOptions{
		DefaultURLScheme:       "https",
		Editor:                 "vim",
		FollowRedirects:        true,
		FormatJSON:             true,
		Insecure:               false,
		PreserveScrollPosition: true,
		StatusLine:             "[wuzz {{.Version}}]{{if .Duration}} [Response time: {{.Duration}}]{{end}} [Request no.: {{.RequestNumber}}/{{.HistorySize}}] [Search type: {{.SearchType}}]",
		Timeout: Duration{
			defaultTimeoutDuration,
		},
	},
}

func init() {
	if os.Getenv("EDITOR") != "" {
		DefaultConfig.General.Editor = os.Getenv("EDITOR")
	}
}

func LoadConfig(configFile string) (*Config, error) {
	if _, err := os.Stat(configFile); os.IsNotExist(err) {
		return nil, errors.New("Config file does not exist.")
	} else if err != nil {
		return nil, err
	}

	conf := DefaultConfig
	if _, err := toml.DecodeFile(configFile, &conf); err != nil {
		return nil, err
	}

	if conf.Keys == nil {
		conf.Keys = DefaultKeys
	} else {
		// copy default keys
		for keyCategory, keys := range DefaultKeys {
			confKeys, found := conf.Keys[keyCategory]
			if found {
				for key, action := range keys {
					if _, found := confKeys[key]; !found {
						conf.Keys[keyCategory][key] = action
					}
				}
			} else {
				conf.Keys[keyCategory] = keys
			}
		}
	}

	return &conf, nil
}

func GetDefaultConfigLocation() string {
	var configFolderLocation string
	switch runtime.GOOS {
	case "linux":
		// Use the XDG_CONFIG_HOME variable if it is set, otherwise
		// $HOME/.config/wuzz/config.toml
		xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
		if xdgConfigHome != "" {
			configFolderLocation = xdgConfigHome
		} else {
			configFolderLocation, _ = homedir.Expand("~/.config/wuzz/")
		}

	default:
		// On other platforms we just use $HOME/.wuzz
		configFolderLocation, _ = homedir.Expand("~/.wuzz/")
	}

	return filepath.Join(configFolderLocation, "config.toml")
}