forked from madlambda/nash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nash.go
199 lines (161 loc) · 5.27 KB
/
nash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Package nash provides a library to embed the `nash` scripting language
// within your program or create your own nash cli.
package nash
import (
"bytes"
"fmt"
"io"
"github.com/NeowayLabs/nash/ast"
shell "github.com/NeowayLabs/nash/internal/sh"
"github.com/NeowayLabs/nash/sh"
)
type (
// Shell is the execution engine of the scripting language.
Shell struct {
interp *shell.Shell
}
)
// New creates a new `nash.Shell` instance.
func New(nashpath string, nashroot string) (*Shell, error) {
interp, err := shell.NewShell(nashpath, nashroot)
if err != nil {
return nil, err
}
nash := Shell{
interp: interp,
}
return &nash, nil
}
// SetDebug enable some logging for debug purposes.
func (nash *Shell) SetDebug(b bool) {
nash.interp.SetDebug(b)
}
// SetInteractive enables interactive (shell) mode.
func (nash *Shell) SetInteractive(b bool) {
nash.interp.SetInteractive(b)
}
func (nash *Shell) NashPath() string {
return nash.interp.NashPath()
}
// Environ returns the set of environment variables in the shell
func (nash *Shell) Environ() shell.Env {
return nash.interp.Environ()
}
// GetFn gets the function object.
func (nash *Shell) GetFn(name string) (sh.FnDef, error) {
fnObj, err := nash.interp.GetFn(name)
if err != nil {
return nil, err
}
return fnObj.Fn(), nil
}
// Prompt returns the environment prompt or the default one
func (nash *Shell) Prompt() string {
value, ok := nash.interp.Getenv("PROMPT")
if ok {
return value.String()
}
return "<no prompt> "
}
// SetNashdPath sets an alternativa path to nashd
func (nash *Shell) SetNashdPath(path string) {
nash.interp.SetNashdPath(path)
}
// Exec executes the code specified by string content.
// By default, nash uses os.Stdin, os.Stdout and os.Stderr as input, output
// and error file descriptors. You can change it with SetStdin, SetStdout and Stderr,
// respectively.
// The path is only used for error line reporting. If content represents a file, then
// setting path to this filename should improve debugging (or no).
func (nash *Shell) Exec(path, content string) error {
return nash.interp.Exec(path, content)
}
// ExecOutput executes the code specified by string content.
//
// It behaves like **Exec** with the exception that it will ignore any
// stdout parameter (and the default os.Stdout) and will return the
// whole stdout output in memory.
//
// This method has no side effects, it will preserve any previously
// setted stdout, it will only ignore the configured stdout to run
// the provided script content;
func (nash *Shell) ExecOutput(path, content string) ([]byte, error) {
oldstdout := nash.Stdout()
defer nash.SetStdout(oldstdout)
var output bytes.Buffer
nash.SetStdout(&output)
err := nash.interp.Exec(path, content)
return output.Bytes(), err
}
// ExecuteString executes the script content.
// Deprecated: Use Exec instead.
func (nash *Shell) ExecuteString(path, content string) error {
return nash.interp.Exec(path, content)
}
// ExecFile executes the script content of the file specified by path
// and passes as arguments to the script the given args slice.
func (nash *Shell) ExecFile(path string, args ...string) error {
if len(args) > 0 {
err := nash.ExecuteString("setting args", `var ARGS = `+args2Nash(args))
if err != nil {
return fmt.Errorf("Failed to set nash arguments: %s", err.Error())
}
}
return nash.interp.ExecFile(path)
}
// ExecuteFile executes the given file.
// Deprecated: Use ExecFile instead.
func (nash *Shell) ExecuteFile(path string) error {
return nash.interp.ExecFile(path)
}
// ExecuteTree executes the given tree.
// Deprecated: Use ExecTree instead.
func (nash *Shell) ExecuteTree(tr *ast.Tree) ([]sh.Obj, error) {
return nash.interp.ExecuteTree(tr)
}
// ExecTree evaluates the given abstract syntax tree.
// it returns the object result of eval or nil when not applied and error.
func (nash *Shell) ExecTree(tree *ast.Tree) ([]sh.Obj, error) {
return nash.interp.ExecuteTree(tree)
}
// SetStdout set the stdout of the nash engine.
func (nash *Shell) SetStdout(out io.Writer) {
nash.interp.SetStdout(out)
}
// SetStderr set the stderr of nash engine
func (nash *Shell) SetStderr(err io.Writer) {
nash.interp.SetStderr(err)
}
// SetStdin set the stdin of the nash engine
func (nash *Shell) SetStdin(in io.Reader) {
nash.interp.SetStdin(in)
}
// Stdin is the interpreter standard input
func (nash *Shell) Stdin() io.Reader { return nash.interp.Stdin() }
// Stdout is the interpreter standard output
func (nash *Shell) Stdout() io.Writer { return nash.interp.Stdout() }
// Stderr is the interpreter standard error
func (nash *Shell) Stderr() io.Writer { return nash.interp.Stderr() }
// Setvar sets or updates the variable in the nash session. It
// returns true if variable was found and properly updated.
func (nash *Shell) Setvar(name string, value sh.Obj) bool {
return nash.interp.Setvar(name, value)
}
// Newvar creates a new variable in the interpreter scope
func (nash *Shell) Newvar(name string, value sh.Obj) {
nash.interp.Newvar(name, value)
}
// Getvar retrieves a variable from nash session
func (nash *Shell) Getvar(name string) (sh.Obj, bool) {
return nash.interp.Getvar(name)
}
func args2Nash(args []string) string {
ret := "("
for i := 0; i < len(args); i++ {
ret += `"` + args[i] + `"`
if i < (len(args) - 1) {
ret += " "
}
}
return ret + ")"
}