-
Notifications
You must be signed in to change notification settings - Fork 0
/
glas.go
174 lines (143 loc) · 3.74 KB
/
glas.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
package glas
import (
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/glasware/glas-core/internal"
"github.com/glasware/glas-core/internal/actions"
"github.com/glasware/glas-core/internal/config"
"github.com/glasware/glas-core/internal/connection"
"github.com/glasware/glas-core/internal/connection/telnet"
"github.com/glasware/glas-core/internal/input"
)
var (
welcome = strings.Join([]string{
`\ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___ / ___| | __ _ ___`,
" \\ \\ /\\ / / _ \\ |/ __/ _ \\| '_ ` _ \\ / _ \\ | __/ _ \\ | | _| |/ _` / __|",
` \ V V / __/ | (_| (_) | | | | | | __/ | || (_) | | |_| | | (_| \__ \_`,
` \_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ \____|_|\__,_|___( )`,
` |/`,
` _ _ _`,
` __ _ _ __ _____ ___ __ ___ _ __(_)_ __ ___ ___ _ __ | |_ __ _| |`,
" / _` | '_ \\ / _ \\ \\/ / '_ \\ / _ \\ '__| | '_ ` _ \\ / _ \\ '_ \\| __/ _` | |",
`| (_| | | | | | __/> <| |_) | __/ | | | | | | | | __/ | | | || (_| | |`,
` \__,_|_| |_| \___/_/\_\ .__/ \___|_| |_|_| |_| |_|\___|_| |_|\__\__,_|_|`,
` |_|`,
` __ __ _ _ ____ _ _ _`,
`| \/ | | | | _ \ ___| (_) ___ _ __ | |_`,
`| |\/| | | | | | | | / __| | |/ _ \ '_ \| __|`,
`| | | | |_| | |_| | | (__| | | __/ | | | |_`,
`|_| |_|\___/|____/ \___|_|_|\___|_| |_|\__|`,
}, "\n")
)
type (
Glas interface {
Start(context.Context) error
SendInput(string)
}
glas struct {
in chan string
out io.Writer
conn *connection.Connection
errCh chan error
cfg *config.Config
inputHandler input.Handler
}
)
var (
_ Glas = new(glas)
_ internal.Surface = new(glas)
)
func New(in chan string, out io.Writer, cfgPath string, options ...Option) (Glas, error) {
g := glas{
in: in,
out: out,
}
g.inputHandler = input.New(&g)
var err error
g.cfg, err = config.Load(cfgPath, options...)
if err != nil {
return nil, fmt.Errorf("config.Load -- %w", err)
}
return &g, nil
}
func (g *glas) Start(ctx context.Context) error {
if err := g.WriteLn(welcome); err != nil {
return err
}
if err := g.WriteLn("The command prefix is set to " + g.cfg.Prefix()); err != nil {
return err
}
return g.eval(ctx)
}
func (g *glas) SendInput(str string) {
g.in <- str
}
func (g *glas) Echo() bool {
if conn := g.Connection(); conn != nil {
return conn.Echo() && g.cfg.Echo()
}
return g.cfg.Echo()
}
func (g *glas) CommandPrefix() string {
return g.cfg.Prefix()
}
func (g *glas) Connection() *connection.Connection {
return g.conn
}
func (g *glas) NewConnection(ctx context.Context, addr string) error {
var err error
g.conn, err = connection.New(addr)
if err != nil {
return err
}
go g.readSocket(ctx)
return nil
}
func (g *glas) Write(b []byte) (int, error) {
return g.out.Write(b)
}
func (g *glas) WriteLn(str string) error {
_, err := g.Write([]byte(str + "\n"))
return err
}
func (g *glas) WriteF(format string, v ...any) error {
str := fmt.Sprintf(format, v...)
_, err := g.Write([]byte(str))
return err
}
func (g *glas) Aliases() *actions.Aliases {
return g.cfg.Aliases()
}
func (g *glas) readSocket(ctx context.Context) {
if conn := g.Connection(); conn != nil {
for {
if !conn.Connected() {
return
}
select {
case <-ctx.Done():
return
default:
byt := make([]byte, 10)
n, err := conn.Read(byt)
if err != nil && !errors.Is(err, telnet.ErrClosed) {
g.errCh <- err
return
}
if n > 0 {
_, err = g.Write(byt)
if err != nil {
g.errCh <- err
return
}
}
if errors.Is(err, telnet.ErrClosed) {
return
}
}
}
}
}