mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
yeah i should have done this before, i know
This commit is contained in:
commit
0091ffdbb3
33 changed files with 25678 additions and 0 deletions
82
session/environment.go
Normal file
82
session/environment.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Environment struct {
|
||||
Padding int
|
||||
storage map[string]string
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func NewEnvironment() *Environment {
|
||||
env := &Environment{
|
||||
Padding: 0,
|
||||
storage: make(map[string]string),
|
||||
lock: &sync.Mutex{},
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
func (env *Environment) Has(name string) bool {
|
||||
env.lock.Lock()
|
||||
defer env.lock.Unlock()
|
||||
|
||||
_, found := env.storage[name]
|
||||
|
||||
return found
|
||||
}
|
||||
|
||||
func (env *Environment) Set(name, value string) string {
|
||||
env.lock.Lock()
|
||||
defer env.lock.Unlock()
|
||||
|
||||
old, _ := env.storage[name]
|
||||
env.storage[name] = value
|
||||
|
||||
if len(name) > env.Padding {
|
||||
env.Padding = len(name)
|
||||
}
|
||||
|
||||
return old
|
||||
}
|
||||
|
||||
func (env *Environment) Get(name string) (bool, string) {
|
||||
env.lock.Lock()
|
||||
defer env.lock.Unlock()
|
||||
|
||||
if value, found := env.storage[name]; found == true {
|
||||
return true, value
|
||||
}
|
||||
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (env *Environment) GetInt(name string) (error, int) {
|
||||
if found, value := env.Get(name); found == true {
|
||||
if i, err := strconv.Atoi(value); err == nil {
|
||||
return nil, i
|
||||
} else {
|
||||
return err, 0
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Not found."), 0
|
||||
}
|
||||
|
||||
func (env *Environment) Sorted() []string {
|
||||
env.lock.Lock()
|
||||
defer env.lock.Unlock()
|
||||
|
||||
var keys []string
|
||||
for k := range env.storage {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue