Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/device/genericcli/genericcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,16 @@ func WithDevLogger(logger *zap.Logger) GenericDeviceOption {
}
}

// WithDevAdditionalLoginCallbacks adds given callbacks to device's login callbacks.
func WithDevAdditionalLoginCallbacks(cb []cmd.ExprCallback) GenericDeviceOption {
return func(h *GenericDevice) {
h.cli.loginCB = append(h.cli.loginCB, cb...)
}
}

// WithDevLoginCallbacks replaces device's login callbacks.
// Replacing behaviour is error-prone due to possible overwrites, so it should only be used in /pkg/device/... basic configs.
// If reusing device configs - use [WithDevAdditionalLoginCallbacks] instead.
func WithDevLoginCallbacks(cb []cmd.ExprCallback) GenericDeviceOption {
return func(h *GenericDevice) {
h.cli.loginCB = cb
Expand Down Expand Up @@ -339,9 +343,14 @@ func (m *GenericDevice) connectCLI(ctx context.Context) (err error) {
return err
}
case cbExprName:
pos := match.GetUnderlyingRes().GetPatternNo()
f := m.cli.loginCB[pos]
err := m.connector.Write(f.GetAns())
var ans []byte
for _, v := range m.cli.loginCB {
if _, ok := v.GetExpr().Match(match.GetMatched()); ok {
ans = v.GetAns()
break
}
}
err := m.connector.Write(ans)
if err != nil {
return fmt.Errorf("write error %w", err)
}
Expand Down Expand Up @@ -528,9 +537,14 @@ func genericLogin(ctx context.Context, connector streamer.Connector, cli Generic
case promptExprName:
return nil
case cbExprName:
pos := readResLogin.GetUnderlyingRes().GetPatternNo()
f := cli.loginCB[pos]
err := connector.Write(f.GetAns())
var ans []byte
for _, v := range cli.loginCB {
if _, ok := v.GetExpr().Match(readResLogin.GetMatched()); ok {
ans = v.GetAns()
break
}
}
err := connector.Write(ans)
if err != nil {
return fmt.Errorf("write error %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/device/genericcli/genericcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ func TestLoginCallback(t *testing.T) {
cli := MakeGenericCLI(
expr.NewSimpleExprLast200().FromPattern(promptExpression),
expr.NewSimpleExprLast200().FromPattern(``),
WithLoginCallbacks([]cmd.ExprCallback{cmd.NewExprCallback(`/\*.+Login: Someone logged in/`, "\n")}),
WithLoginCallbacks([]cmd.ExprCallback{
// both not valid regex * added here to check that we make callback for an expected regex
cmd.NewExprCallback("/not valid regex 1/", "\r\n"),
cmd.NewExprCallback(`/\*.+Login: Someone logged in/`, "\n"),
cmd.NewExprCallback("/not valid regex 2/", "\r"),
}),
)
dev := MakeGenericDevice(cli, connector, WithDevLogger(logger))
return &dev
Expand Down
Loading