Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(multiple-cli): support multiple clis with BaseUI #38

Merged
merged 1 commit into from
Jul 21, 2023
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
20 changes: 10 additions & 10 deletions pkg/ui/flat/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (u UI) registerEvents() {
u.Arco.Component(pathState.AsComponent())

u.Runtime.Handle("UpdateSubcommand", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
p := ui.ToStruct[UpdateSubcommandParams[ui.Path]](m.Params)

form := p.Path.TraverseFormWithCallback(s.Form, func(s string, f *config.Form) {
Expand All @@ -42,7 +42,7 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("UpdateOptionValue", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)

p := ui.ToStruct[ui.UpdateOptionValueParams](m.Params)
form := p.Path.TraverseForm(s.Form)
Expand All @@ -58,15 +58,15 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("Heartbeat", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s.HeartbeatCh <- struct{}{}
return nil
})

u.Runtime.Handle("Run", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(*u.FormTemplate, connId)
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)

script, f := u.CLI.Script(*sess.Form)
script, f := u.CLIs[0].Script(*sess.Form)
formatState.SetState(f, &connId)

finishedCh, err := sess.Exec.Run(script)
Expand Down Expand Up @@ -123,19 +123,19 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("Stop", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s.Exec.StopCh <- struct{}{}
return nil
})

u.Runtime.Handle("DryRun", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(*u.FormTemplate, connId)
s, _ := u.CLI.Script(*sess.Form)
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s, _ := u.CLIs[0].Script(*sess.Form)
return dryRunState.SetState(s, &connId)
})

u.Runtime.Handle("UpdateCheckedOptions", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
p := ui.ToStruct[ui.UpdateCheckedOptionsParams[[]string]](m.Params)
f := p.Path.TraverseForm(s.Form)
ui.UpdateCheckedOptions(&f.Flags, p.CheckedValues)
Expand All @@ -144,7 +144,7 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("EstablishedConnection", func(m *runtime.Message, connId int) error {
ui.GetOrCreateSession(*u.FormTemplate, connId)
ui.GetOrCreateSession(0, u.FormTemplates, connId)
return nil
})
}
8 changes: 4 additions & 4 deletions pkg/ui/flat/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (u UI) optionSection() sunmao.BaseComponentBuilder {
`).
Children(map[string][]sunmao.BaseComponentBuilder{
"content": {
u.commandStack(Path{}, u.CLI.Command),
u.checkbox(Path{}, u.CLI.Command),
u.commandStack(Path{}, u.CLIs[0].Command),
u.checkbox(Path{}, u.CLIs[0].Command),
u.Arco.NewStack(),
u.buttons(),
},
Expand Down Expand Up @@ -440,7 +440,7 @@ func (u UI) outputSection() sunmao.BaseComponentBuilder {

func (u UI) sidebar() sunmao.BaseComponentBuilder {
title := u.Arco.NewText().
Content(u.CLI.Name).
Content(u.CLIs[0].Name).
Style("content", `
font-size: 1.25rem;
font-weight: bold;
Expand Down Expand Up @@ -497,7 +497,7 @@ func (u UI) commandMenu() sunmao.BaseComponentBuilder {
}

func (u UI) menuItems() []ui.TreeNodeProperties {
return menuItems(u.CLI.Command, []ui.TreeNodeProperties{})
return menuItems(u.CLIs[0].Command, []ui.TreeNodeProperties{})
}

func menuItems(c config.Command, i []ui.TreeNodeProperties) []ui.TreeNodeProperties {
Expand Down
20 changes: 10 additions & 10 deletions pkg/ui/naive/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (u UI) registerEvents() {
u.Arco.Component(formatState.AsComponent())

u.Runtime.Handle("UpdateSubcommand", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)

p := ui.ToStruct[UpdateSubcommandParams[int]](m.Params)
form := p.Path.TraverseForm(s.Form)
Expand All @@ -35,7 +35,7 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("UpdateOptionValue", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)

p := ui.ToStruct[ui.UpdateOptionValueParams](m.Params)
form := p.Path.TraverseForm(s.Form)
Expand All @@ -51,15 +51,15 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("Heartbeat", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s.HeartbeatCh <- struct{}{}
return nil
})

u.Runtime.Handle("Run", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(*u.FormTemplate, connId)
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)

script, f := u.CLI.Script(*sess.Form)
script, f := u.CLIs[0].Script(*sess.Form)
formatState.SetState(f, &connId)

finishedCh, err := sess.Exec.Run(script)
Expand Down Expand Up @@ -116,24 +116,24 @@ func (u UI) registerEvents() {
})

u.Runtime.Handle("Stop", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s.Exec.StopCh <- struct{}{}
return nil
})

u.Runtime.Handle("EstablishedConnection", func(m *runtime.Message, connId int) error {
ui.GetOrCreateSession(*u.FormTemplate, connId)
ui.GetOrCreateSession(0, u.FormTemplates, connId)
return nil
})

u.Runtime.Handle("DryRun", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(*u.FormTemplate, connId)
s, _ := u.CLI.Script(*sess.Form)
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)
s, _ := u.CLIs[0].Script(*sess.Form)
return dryRunState.SetState(s, &connId)
})

u.Runtime.Handle("UpdateCheckedOptions", func(m *runtime.Message, connId int) error {
s := ui.GetOrCreateSession(*u.FormTemplate, connId)
s := ui.GetOrCreateSession(0, u.FormTemplates, connId)
p := ui.ToStruct[ui.UpdateCheckedOptionsParams[[]string]](m.Params)
f := p.Path.TraverseForm(s.Form)
ui.UpdateCheckedOptions(&f.Flags, p.CheckedValues)
Expand Down
8 changes: 4 additions & 4 deletions pkg/ui/naive/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (u UI) layout() sunmao.BaseComponentBuilder {
Children(map[string][]sunmao.BaseComponentBuilder{
"header": u.headerElements(),
"content": {
u.commandStack(p, u.CLI.Command),
u.commandStack(p, u.CLIs[0].Command),
u.runButton(),
u.dryRunButton(),
u.terminal(),
Expand Down Expand Up @@ -265,7 +265,7 @@ func (u UI) subcommandsTab(p Path, c config.Command) sunmao.BaseComponentBuilder
values = append(values, c.Name)
}

form := p.TraverseForm(u.FormTemplate)
form := p.TraverseForm(u.FormTemplates[0])
form.Choice = values[0]

activeTab := fmt.Sprintf("{{ %s.activeTab }}", p.subcommandTabsId())
Expand Down Expand Up @@ -415,7 +415,7 @@ func (u UI) optionInput(p Path, o config.Option) sunmao.BaseComponentBuilder {

func (u UI) headerElements() []sunmao.BaseComponentBuilder {
title := u.Arco.NewText().
Content(u.CLI.Name).Style("content",
Content(u.CLIs[0].Name).Style("content",
`
font-size: 1.25rem;
font-weight: bold;
Expand Down Expand Up @@ -447,7 +447,7 @@ func (u UI) helpModal() sunmao.BaseComponentBuilder {
overflow: scroll;
`).
Content(ui.TextDisplayProperties{
Text: u.CLI.Help,
Text: u.CLIs[0].Help,
Format: "code",
})

Expand Down
12 changes: 10 additions & 2 deletions pkg/ui/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ type session struct {
Form *config.Form
Exec *executor.Executor
HeartbeatCh chan struct{}
cliIndex int
}

func GetOrCreateSession(template config.Form, connId int) *session {
func GetOrCreateSession(cliIndex int, templates []*config.Form, connId int) *session {
s, ok := sessions[connId]
if !ok {
f := template.Clone()
f := templates[cliIndex].Clone()
hbCh := make(chan struct{})
exec := executor.NewExecutor()

s = &session{
Form: f,
Exec: &exec,
HeartbeatCh: hbCh,
cliIndex: cliIndex,
}
sessions[connId] = s
}

if cliIndex != s.cliIndex {
s.Form = templates[cliIndex].Clone()
s.cliIndex = cliIndex
}

return s
}
30 changes: 18 additions & 12 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UI interface {
Run() error
}

func NewUI(c config.CLI) (*BaseUI, error) {
func NewUI(c ...config.CLI) (*BaseUI, error) {
if client.Error != nil {
return nil, errors.New("failed to load prebuilt UI")
}
Expand All @@ -23,23 +23,29 @@ func NewUI(c config.CLI) (*BaseUI, error) {
app := sunmao.NewApp()
arco := arco.NewArcoApp(app)
c2u := NewCLI2UIApp(app)
fTpl := c.Form()

tpls := []*config.Form{}

for _, cli := range c {
f := cli.Form()
tpls = append(tpls, &f)
}

return &BaseUI{
Runtime: r,
Arco: arco,
C2U: c2u,
CLI: &c,
FormTemplate: &fTpl,
Runtime: r,
Arco: arco,
C2U: c2u,
CLIs: c,
FormTemplates: tpls,
}, nil
}

type BaseUI struct {
Runtime *runtime.Runtime
Arco *arco.ArcoAppBuilder
C2U *CLI2UIAppBuilder
CLI *config.CLI
FormTemplate *config.Form
Runtime *runtime.Runtime
Arco *arco.ArcoAppBuilder
C2U *CLI2UIAppBuilder
CLIs []config.CLI
FormTemplates []*config.Form
}

func (BaseUI) Run() error {
Expand Down