mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 13:33:21 -07:00
new: new -env-file for env persistance (closes #81)
This commit is contained in:
parent
27457ac774
commit
5509a58e52
3 changed files with 50 additions and 3 deletions
|
@ -1,7 +1,9 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
@ -21,7 +23,7 @@ type Environment struct {
|
|||
sess *Session
|
||||
}
|
||||
|
||||
func NewEnvironment(s *Session) *Environment {
|
||||
func NewEnvironment(s *Session, envFile string) *Environment {
|
||||
env := &Environment{
|
||||
Padding: 0,
|
||||
Data: make(map[string]string),
|
||||
|
@ -29,9 +31,42 @@ func NewEnvironment(s *Session) *Environment {
|
|||
cbs: make(map[string]SetCallback),
|
||||
}
|
||||
|
||||
if envFile != "" {
|
||||
envFile, _ := core.ExpandPath(envFile)
|
||||
if core.Exists(envFile) {
|
||||
if err := env.Load(envFile); err != nil {
|
||||
fmt.Printf("Error while loading %s: %s\n", envFile, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
func (env *Environment) Load(fileName string) error {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
||||
raw, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(raw, &env.Data)
|
||||
}
|
||||
|
||||
func (env *Environment) Save(fileName string) error {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
||||
raw, err := json.Marshal(env.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(fileName, raw, 0644)
|
||||
}
|
||||
|
||||
func (env *Environment) Has(name string) bool {
|
||||
env.Lock()
|
||||
defer env.Unlock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue