Allowing Commands to reconfigure app Configuration, this is will allow default ports to be currently added to remote presets

This commit is contained in:
NI
2020-02-23 17:23:24 +08:00
parent 697ee0b2a1
commit e7ca6d95ed
14 changed files with 212 additions and 79 deletions

View File

@@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"github.com/niruix/sshwifty/application/configuration"
"github.com/niruix/sshwifty/application/log"
)
@@ -42,20 +43,35 @@ type Command func(
cfg Configuration,
) FSMMachine
// Builder builds a command
type Builder struct {
command Command
configurator configuration.Reconfigurator
}
// Register builds a Builder for registration
func Register(c Command, p configuration.Reconfigurator) Builder {
return Builder{
command: c,
configurator: p,
}
}
// Commands contains data of all commands
type Commands [MaxCommandID + 1]Command
type Commands [MaxCommandID + 1]Builder
// Register registers a new command
func (c *Commands) Register(id byte, cb Command) {
func (c *Commands) Register(
id byte, cb Command, ps configuration.Reconfigurator) {
if id > MaxCommandID {
panic("Command ID must be not greater than MaxCommandID")
}
if (*c)[id] != nil {
if (*c)[id].command != nil {
panic(fmt.Sprintf("Command %d already been registered", id))
}
(*c)[id] = cb
(*c)[id] = Register(cb, ps)
}
// Run creates command executer
@@ -63,16 +79,32 @@ func (c Commands) Run(
id byte,
l log.Logger,
w StreamResponder,
cfg Configuration) (FSM, error) {
cfg Configuration,
) (FSM, error) {
if id > MaxCommandID {
return FSM{}, ErrCommandRunUndefinedCommand
}
cc := c[id]
if cc == nil {
if cc.command == nil {
return FSM{}, ErrCommandRunUndefinedCommand
}
return newFSM(cc(l, w, cfg)), nil
return newFSM(cc.command(l, w, cfg)), nil
}
// Reconfigure lets commands reset configuration
func (c Commands) Reconfigure(
p configuration.Configuration,
) configuration.Configuration {
for i := range c {
if c[i].configurator == nil {
continue
}
p = c[i].configurator(p)
}
return p
}