Skip to content

Commit

Permalink
feat(sess): made goroutines die with connection optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyo5hi committed Aug 23, 2023
1 parent e02e4d7 commit 764b920
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import (
"CLI2UI/pkg/ui/flat"
"CLI2UI/pkg/ui/naive"
"encoding/json"
"fmt"
"log"
"os"
"runtime"
"time"

"github.com/invopop/jsonschema"
"github.com/urfave/cli/v2"
)

func main() {
go func() {
for {
fmt.Printf("Number of goroutines: %d\n", runtime.NumGoroutine())
time.Sleep(1 * time.Second)
}
}()

app := &cli.App{
Name: "CLI2UI",
Usage: "Usage: cli2ui <config>",
Expand Down
4 changes: 4 additions & 0 deletions pkg/ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ type UpdateCheckedOptionsParams[T []string | string] struct {
Path Path
CheckedValues T
}

type RunParams struct {
DieWithConn any
}
5 changes: 5 additions & 0 deletions pkg/ui/flat/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (u UI) registerEvents() {

u.Runtime.Handle("Run", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)
p := ui.ToStruct[ui.RunParams](m.Params)

script, f := u.CLIs[0].Script(*sess.Form)
formatState.SetState(f, &connId)
Expand Down Expand Up @@ -97,6 +98,10 @@ func (u UI) registerEvents() {
sess.Exec.StateCh <- struct{}{}

go func() {
if !p.DieWithConn.(bool) {
return
}

for sess.Exec.State.IsRunning {
// TODO(xinxi.guo): this can be extended to send more useful messages
err := u.Runtime.Ping(&connId, "Ping")
Expand Down
19 changes: 18 additions & 1 deletion pkg/ui/flat/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (u UI) runButton() sunmao.BaseComponentBuilder {
ComponentId: "$utils",
Method: sunmao.EventMethod{
Name: "binding/v1/Run",
Parameters: ui.RunParams{
DieWithConn: "{{ dieWithConnCheckbox.isCheckAll }}",
},
},
},
}).
Expand Down Expand Up @@ -192,7 +195,21 @@ func (u UI) checkbox(c config.Command) sunmao.BaseComponentBuilder {
min-width: 8rem;
`).
Children(map[string][]sunmao.BaseComponentBuilder{
"content": {u.checkboxStack(Path{}, c)},
"content": {
u.Arco.NewCheckbox().
Id("dieWithConnCheckbox").
Properties(ui.StructToMap(ui.CheckboxProperties[string]{
Options: []ui.CheckboxOptionProperties{
{
Label: "Die with Connection",
Value: "dieWithConn",
Disabled: false,
},
},
DefaultCheckedValues: []string{"dieWithConn"},
})),
u.checkboxStack(Path{}, c),
},
})

return container
Expand Down
5 changes: 5 additions & 0 deletions pkg/ui/naive/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (u UI) registerEvents() {

u.Runtime.Handle("Run", func(m *runtime.Message, connId int) error {
sess := ui.GetOrCreateSession(0, u.FormTemplates, connId)
p := ui.ToStruct[ui.RunParams](m.Params)

script, f := u.CLIs[0].Script(*sess.Form)
formatState.SetState(f, &connId)
Expand Down Expand Up @@ -90,6 +91,10 @@ func (u UI) registerEvents() {
sess.Exec.StateCh <- struct{}{}

go func() {
if !p.DieWithConn.(bool) {
return
}

for sess.Exec.State.IsRunning {
// TODO(xinxi.guo): this can be extended to send more useful messages
err := u.Runtime.Ping(&connId, "Ping")
Expand Down
18 changes: 17 additions & 1 deletion pkg/ui/naive/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ func (u UI) runButton() sunmao.BaseComponentBuilder {
ComponentId: "$utils",
Method: sunmao.EventMethod{
Name: "binding/v1/Run",
Parameters: ui.RunParams{
DieWithConn: "{{ dieWithConnCheckbox.isCheckAll }}",
},
},
},
})
Expand Down Expand Up @@ -437,7 +440,20 @@ func (u UI) headerElements() []sunmao.BaseComponentBuilder {
},
})

return []sunmao.BaseComponentBuilder{title, help}
cb := u.Arco.NewCheckbox().
Id("dieWithConnCheckbox").
Properties(ui.StructToMap(ui.CheckboxProperties[string]{
Options: []ui.CheckboxOptionProperties{
{
Label: "Die with Connection",
Value: "dieWithConn",
Disabled: false,
},
},
DefaultCheckedValues: []string{"dieWithConn"},
}))

return []sunmao.BaseComponentBuilder{title, help, cb}
}

func (u UI) helpModal() sunmao.BaseComponentBuilder {
Expand Down

0 comments on commit 764b920

Please sign in to comment.