integrate environment variables with vartable
This commit is contained in:
parent
9301c77573
commit
7c04e3de95
4 changed files with 57 additions and 4 deletions
|
|
@ -17,17 +17,42 @@
|
|||
|
||||
package ast
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
)
|
||||
|
||||
type VarTable *map[string]*Token
|
||||
|
||||
func GetVar(arg string, vt VarTable) *Token {
|
||||
val, ok := (*vt)[arg]
|
||||
if !ok {
|
||||
e := os.Getenv(arg)
|
||||
if e != "" {
|
||||
t := &Token{Inner: e}
|
||||
if StrIsNumber(e) {
|
||||
t.Tag = NUMBER
|
||||
} else {
|
||||
t.Tag = STRING
|
||||
}
|
||||
|
||||
SetVar(arg, t, vt)
|
||||
return t
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// TODO: Document me :)
|
||||
func SetVar(variable string, value *Token, vt VarTable) {
|
||||
(*vt)[variable] = value
|
||||
if value.Tag == NUMBER || value.Tag == STRING {
|
||||
os.Setenv(variable, value.Inner.(string))
|
||||
}
|
||||
}
|
||||
|
||||
// Library represents variables defined in inner scope
|
||||
// It is assumed library is ordered from innermost scope to outermost scope
|
||||
func GetVarFromTables(arg string, library []VarTable) *Token {
|
||||
|
|
@ -43,3 +68,29 @@ func GetVarFromTables(arg string, library []VarTable) *Token {
|
|||
|
||||
return res
|
||||
}
|
||||
|
||||
func InitVarTable(table VarTable) {
|
||||
for _, val := range os.Environ() {
|
||||
variable := strings.Split(val, "=")
|
||||
t := &Token{Inner: variable[1]}
|
||||
if StrIsNumber(variable[1]) {
|
||||
t.Tag = NUMBER
|
||||
} else {
|
||||
t.Tag = STRING
|
||||
}
|
||||
|
||||
SetVar(variable[0], t, table)
|
||||
}
|
||||
}
|
||||
|
||||
// Dont do this on var tables youve already called InitVarTable on.... please....
|
||||
// This will pull currently contained variables OUT of the operating env
|
||||
func DeleteVarTable(table VarTable) {
|
||||
for key, _ := range (*table) {
|
||||
// from what I can tell, theres not much we can do if this fails
|
||||
err := os.Unsetenv(key)
|
||||
if err != nil {
|
||||
log.Log(log.DEBUG, "Failed to remove " + key + " from env: " + err.Error(), "vartable")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue