fix: use a rwmutex for aliases map

windows-fixes
TorchedSammy 2021-12-19 22:37:44 -04:00
parent f81babe973
commit 40578d9d5f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"strings" "strings"
"sync"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
@ -10,16 +11,21 @@ var aliases *hilbishAliases
type hilbishAliases struct { type hilbishAliases struct {
aliases map[string]string aliases map[string]string
mu *sync.RWMutex
} }
// initialize aliases map // initialize aliases map
func NewAliases() *hilbishAliases { func NewAliases() *hilbishAliases {
return &hilbishAliases{ return &hilbishAliases{
aliases: make(map[string]string), aliases: make(map[string]string),
mu: &sync.RWMutex{},
} }
} }
func (h *hilbishAliases) Add(alias, cmd string) { func (h *hilbishAliases) Add(alias, cmd string) {
h.mu.Lock()
defer h.mu.Unlock()
h.aliases[alias] = cmd h.aliases[alias] = cmd
} }
@ -28,10 +34,16 @@ func (h *hilbishAliases) All() map[string]string {
} }
func (h *hilbishAliases) Delete(alias string) { func (h *hilbishAliases) Delete(alias string) {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.aliases, alias) delete(h.aliases, alias)
} }
func (h *hilbishAliases) Resolve(cmdstr string) string { func (h *hilbishAliases) Resolve(cmdstr string) string {
h.mu.RLock()
defer h.mu.RUnlock()
args := strings.Split(cmdstr, " ") args := strings.Split(cmdstr, " ")
for h.aliases[args[0]] != "" { for h.aliases[args[0]] != "" {
alias := h.aliases[args[0]] alias := h.aliases[args[0]]