-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.go
164 lines (147 loc) · 3.6 KB
/
shell.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
package fssh
import (
"errors"
"flag"
"fmt"
"io"
"log"
"path"
"path/filepath"
"github.com/chzyer/readline"
"github.com/jarxorg/wfs"
)
// ShellName is "fssh".
const ShellName = "fssh"
// ErrExit represents an exit error. If this error is detected then the shell will terminate.
var ErrExit = errors.New("exit")
// Shell reads stdin, interprets lines, and executes commands.
type Shell struct {
rl *readline.Instance
Stdout io.Writer
Stderr io.Writer
FS wfs.WriteFileFS
Protocol string
Host string
Dir string
PrefixMatcher PrefixMatcher
}
// NewShell creates a new Shell.
func NewShell(dirUrl string) (*Shell, error) {
homeDir, err := osUserHomeDir()
if err != nil {
return nil, err
}
fsys, protocol, host, dir, err := NewFS(dirUrl)
if err != nil {
return nil, err
}
sh := &Shell{
FS: fsys,
Protocol: protocol,
Host: host,
Dir: dir,
PrefixMatcher: &GlobPrefixMatcher{},
}
rl, err := readline.NewEx(&readline.Config{
HistoryFile: filepath.Join(homeDir, fmt.Sprintf(".%s_history", ShellName)),
AutoComplete: newReadlineAutoCompleter(sh),
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
})
if err != nil {
return nil, err
}
sh.rl = rl
sh.Stdout = rl.Stdout()
sh.Stderr = rl.Stderr()
sh.UpdatePrompt()
return sh, nil
}
// DirWithProtocol returns the current directory held by the shell.
func (sh *Shell) DirWithProtocol() string {
return sh.Protocol + path.Join(sh.Host, sh.Dir)
}
// UpdatePrompt updates the command line prompt.
func (sh *Shell) UpdatePrompt() {
sh.PrefixMatcher.Reset()
sh.rl.SetPrompt("\033[36m" + sh.DirWithProtocol() + ">\033[0m ")
}
// Close closes the shell.
func (sh *Shell) Close() error {
return sh.rl.Close()
}
// Run runs the shell.
func (sh *Shell) Run() error {
sh.rl.CaptureExitSignal()
log.SetOutput(sh.Stderr)
for {
line, err := sh.rl.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
if err := sh.ExecCommand(ParseArgs(line)); err != nil {
if errors.Is(err, ErrExit) {
return nil
}
fmt.Fprintf(sh.Stderr, "%s: %v\n", ShellName, err)
}
}
return nil
}
// ExecCommand executes a command.
func (sh *Shell) ExecCommand(args []string) error {
if len(args) == 0 {
return nil
}
cmd := AquireCommand(args[0])
if cmd == nil {
return fmt.Errorf("command not found: %s", args[0])
}
defer ReleaseCommand(cmd)
if err := cmd.FlagSet().Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
cmd.Usage(sh.Stdout)
return nil
}
return err
}
return cmd.Exec(sh)
}
// Usage prints the usage to the specified writer..
func (sh *Shell) Usage(w io.Writer) {
w.Write([]byte("Commands:\n"))
for _, name := range SortedCommandNames() {
w.Write([]byte(" "))
w.Write([]byte(name))
w.Write([]byte("\n"))
}
}
// SubFS returns the FS and related path. If the dirUrl has protocol then this creates a new FS.
func (sh *Shell) SubFS(filenameUrl string) (FS, string, error) {
if IsCurrentPath(filenameUrl) {
return sh.FS, path.Join(sh.Dir, filenameUrl), nil
}
fsys, _, _, filename, err := NewFS(filenameUrl)
if err != nil {
return nil, "", err
}
return fsys, filename, nil
}
// SubFS returns the FS and related path. If the dirUrl has protocol then this creates a new FS.
func (sh *Shell) SubDirFS(dirUrl string) (FS, string, error) {
if IsCurrentPath(dirUrl) {
return sh.FS, path.Join(sh.Dir, dirUrl), nil
}
fsys, _, _, dir, err := NewDirFS(dirUrl)
if err != nil {
return nil, "", err
}
return fsys, dir, nil
}