Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (62 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
76 lines (62 loc) · 1.6 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/PerformLine/friendscript"
"github.com/PerformLine/friendscript/commands/core"
)
type CoreCommands struct {
*core.Commands
}
func NewCoreCommands(env *friendscript.Environment) *CoreCommands {
cmd := &CoreCommands{
Commands: core.New(env),
}
cmd.SetInstance(cmd)
return cmd
}
// Exit the program with the given status code.
//
// This will be available within Friendscript as the "exit" command.
//
func (self *CoreCommands) Exit(status int) error {
os.Exit(status)
return nil
}
// Return the list of files and subdirectories in the given directory path.
//
// This will be available within Friendscript as the "ls" command.
//
func (self *CoreCommands) Ls(path string) ([]string, error) {
if entries, err := ioutil.ReadDir(path); err == nil {
paths := make([]string, 0)
for _, entry := range entries {
paths = append(paths, entry.Name())
}
return paths, nil
} else {
return nil, err
}
}
func main() {
// log.SetLevelString(`debug`)
// create a new Friendscript scripting environment
environment := friendscript.NewEnvironment()
// add in our commands module, which extends the default "core" commands module
// by adding two new commands: "exit" and "ls".
environment.RegisterModule(``, NewCoreCommands(environment))
if len(os.Args) > 1 {
for _, scriptPath := range os.Args[1:] {
if _, err := environment.EvaluateFile(scriptPath); err == nil {
os.Exit(0)
} else {
fmt.Printf("script error: %v\n", err)
os.Exit(1)
}
}
} else {
fmt.Printf("usage: %v SCRIPT [SCRIPT ..]\n", os.Args[0])
os.Exit(127)
}
}