github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/variables_reserved.go (about)

     1  package lang
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/lmorg/murex/lang/types"
     7  	"github.com/lmorg/murex/utils/envvars"
     8  	"github.com/lmorg/murex/utils/path"
     9  	"github.com/lmorg/murex/utils/readline"
    10  )
    11  
    12  var envDataTypes = map[string][]string{
    13  	types.Path:  {"HOME", "PWD", "OLDPWD", "SHELL", "HOMEBREW_CELLAR", "HOMEBREW_PREFIX", "HOMEBREW_REPOSITORY", "GOPATH", "GOROOT", "GOBIN"},
    14  	types.Paths: {"PATH", "LD_LIBRARY_PATH", "MANPATH", "INFOPATH"},
    15  }
    16  
    17  func getVarSelf(p *Process) interface{} {
    18  	bg := p.Scope.Background.Get()
    19  	return map[string]interface{}{
    20  		"Parent":      int(p.Scope.Parent.Id),
    21  		"Scope":       int(p.Scope.Id),
    22  		"TTY":         p.Scope.Stdout.IsTTY(),
    23  		"Method":      p.Scope.IsMethod,
    24  		"Interactive": Interactive && !bg,
    25  		"Not":         p.Scope.IsNot,
    26  		"Background":  bg,
    27  		"Module":      p.Scope.FileRef.Source.Module,
    28  	}
    29  }
    30  
    31  func getVarArgs(p *Process) interface{} {
    32  	return append([]string{p.Scope.Name.String()}, p.Scope.Parameters.StringArray()...)
    33  }
    34  
    35  func getVarMurexArgs() interface{} {
    36  	return os.Args
    37  }
    38  
    39  func getVarMurexExeValue() (interface{}, error) {
    40  	pwd, err := os.Executable()
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return path.Unmarshal([]byte(pwd))
    46  }
    47  
    48  func getHostname() string {
    49  	name, _ := os.Hostname()
    50  	return name
    51  }
    52  
    53  func getPwdValue() (interface{}, error) {
    54  	pwd, err := os.Getwd()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return path.Unmarshal([]byte(pwd))
    60  }
    61  
    62  func getEnvVarValue(v *Variables) interface{} {
    63  	var err error
    64  	evTable := make(map[string]interface{})
    65  	envvars.All(evTable)
    66  	for env, val := range evTable {
    67  		val, err = v.getEnvValueValue(env, val.(string))
    68  		if err == nil {
    69  			evTable[env] = val
    70  		}
    71  	}
    72  	return evTable
    73  }
    74  
    75  func getEnvVarString() interface{} {
    76  	evTable := make(map[string]interface{})
    77  	envvars.All(evTable)
    78  	return evTable
    79  }
    80  
    81  func getGlobalValues() interface{} {
    82  	m := make(map[string]interface{})
    83  
    84  	GlobalVariables.mutex.Lock()
    85  	for name, v := range GlobalVariables.vars {
    86  		m[name] = v.Value
    87  	}
    88  	GlobalVariables.mutex.Unlock()
    89  
    90  	return m
    91  }
    92  
    93  func getVarColumnsValue() int {
    94  	return readline.GetTermWidth()
    95  }