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

fix(*): trivial fix for unique keys #40

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pkg/ui/flat/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type UpdateSubcommandParams[T string | ui.Path] struct {
Path T `json:"path"`
Path T `json:"myPath"`
Subcommand string `json:"subcommand"`
}

Expand Down
34 changes: 20 additions & 14 deletions pkg/ui/flat/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flat
import (
"CLI2UI/pkg/config"
"CLI2UI/pkg/ui"
"encoding/json"
"fmt"

"github.com/yuyz0112/sunmao-ui-go-binding/pkg/sunmao"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (u UI) optionSection() sunmao.BaseComponentBuilder {
Children(map[string][]sunmao.BaseComponentBuilder{
"content": {
u.commandStack(Path{}, u.CLIs[0].Command),
u.checkbox(Path{}, u.CLIs[0].Command),
u.checkbox(u.CLIs[0].Command),
u.Arco.NewStack(),
u.buttons(),
},
Expand Down Expand Up @@ -173,7 +174,7 @@ func (u UI) dryRunButton() sunmao.BaseComponentBuilder {
})
}

func (u UI) checkbox(p Path, c config.Command) sunmao.BaseComponentBuilder {
func (u UI) checkbox(c config.Command) sunmao.BaseComponentBuilder {
container := u.Arco.NewStack().
Id("CheckboxWrapper").
Properties(ui.StructToMap(ui.StackProperties{
Expand All @@ -191,7 +192,7 @@ func (u UI) checkbox(p Path, c config.Command) sunmao.BaseComponentBuilder {
min-width: 8rem;
`).
Children(map[string][]sunmao.BaseComponentBuilder{
"content": {u.checkboxStack(p, c)},
"content": {u.checkboxStack(Path{}, c)},
})

return container
Expand All @@ -216,11 +217,12 @@ func (u UI) checkboxStack(p Path, c config.Command) *sunmao.StackComponentBuilde

for _, sc := range c.Subcommands {
path := p.Append(sc.Name)
ps, _ := json.Marshal(path)
items := u.checkboxStack(Path{path}, sc).
Slot(sunmao.Container{
ID: "CheckboxWrapper",
Slot: "content",
}, fmt.Sprintf("{{ path.state.some(o => o === \"%s\") }}", sc.Name))
}, fmt.Sprintf("{{ %s.every(v => path.state.includes(v)) }}", string(ps)))
cs = append(cs, items)
}

Expand Down Expand Up @@ -471,7 +473,7 @@ func (u UI) commandMenu() sunmao.BaseComponentBuilder {
return u.Arco.NewTree().
Id("SubcommandMenuTree").
Properties(ui.StructToMap(
ui.TreeProperties{
ui.TreeProperties[string]{
Data: u.menuItems(),
Size: "large",
},
Expand All @@ -488,25 +490,29 @@ func (u UI) commandMenu() sunmao.BaseComponentBuilder {
Method: sunmao.EventMethod{
Name: "binding/v1/UpdateSubcommand",
Parameters: UpdateSubcommandParams[string]{
Path: "{{ SubcommandMenuTree.selectedNodes[0].path }}",
Subcommand: "{{ SubcommandMenuTree.selectedKeys[0] }}",
Path: "{{ SubcommandMenuTree.selectedNodes[0].myPath }}",
Subcommand: "{{ SubcommandMenuTree.selectedNodes[0].subcommand }}",
},
},
},
})
}

func (u UI) menuItems() []ui.TreeNodeProperties {
return menuItems(u.CLIs[0].Command, []ui.TreeNodeProperties{})
func (u UI) menuItems() []ui.TreeNodeProperties[string] {
p := Path{}
return menuItems(u.CLIs[0].Command, []ui.TreeNodeProperties[string]{}, p)
}

func menuItems(c config.Command, i []ui.TreeNodeProperties) []ui.TreeNodeProperties {
func menuItems(c config.Command, i []ui.TreeNodeProperties[string], p Path) []ui.TreeNodeProperties[string] {
for _, sc := range c.Subcommands {
tnp := ui.TreeNodeProperties{
path := Path{p.Append(sc.Name)}
tnp := ui.TreeNodeProperties[string]{
Title: sc.DisplayName(),
Key: sc.Name,
Children: menuItems(sc, []ui.TreeNodeProperties{}),
Selectable: true,
Key: path.menuItemKey(),
Children: menuItems(sc, []ui.TreeNodeProperties[string]{}, path),
Subcommand: sc.Name,
Selectable: "{{ !exec.state.isRunning }}",
MyPath: path.Path,
}
i = append(i, tnp)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/ui/flat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package flat

import (
"CLI2UI/pkg/ui"
"fmt"
)

type Path struct {
ui.Path
}

func (p Path) menuItemKey() string {
return fmt.Sprintf("MenuItem%s", p)
}
32 changes: 17 additions & 15 deletions pkg/ui/props.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,21 @@ type DatePickerProperties[T string | bool] struct {
Disabled T `json:"disabled"`
}

type TreeNodeProperties struct {
Title string `json:"title"`
Key string `json:"key"`
Children []TreeNodeProperties `json:"children"`
Selectable bool `json:"selectable"`
Checkable bool `json:"checkable"`
}

type TreeProperties struct {
Data []TreeNodeProperties `json:"data"`
Size string `json:"size"`
Multiple bool `json:"multiple"`
AutoExpandParent bool `json:"autoExpandParent"`
AutoExpandParentWhenDataChanges bool `json:"autoExpandParentWhenDataChanges"`
DefaultExpandKeys []string `json:"defaultExpandKeys"`
type TreeNodeProperties[T string | bool] struct {
Title string `json:"title"`
Key string `json:"key"`
Children []TreeNodeProperties[T] `json:"children"`
Selectable T `json:"selectable"`
Kiyo5hi marked this conversation as resolved.
Show resolved Hide resolved
Checkable bool `json:"checkable"`
Subcommand string `json:"subcommand"`
MyPath Path `json:"myPath"`
}

type TreeProperties[T string | bool] struct {
Data []TreeNodeProperties[T] `json:"data"`
Size string `json:"size"`
Multiple bool `json:"multiple"`
AutoExpandParent bool `json:"autoExpandParent"`
AutoExpandParentWhenDataChanges bool `json:"autoExpandParentWhenDataChanges"`
DefaultExpandKeys []string `json:"defaultExpandKeys"`
}
88 changes: 88 additions & 0 deletions samples/cloudtower-admin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# yaml-language-server: $schema=../cli.schema.json
name: CloudTower Admin
command:
name: ./cloudtower-admin
display: CloudTower Admin
flags:
- name: v
display: Verbosity
type: enum
options:
- "0"
- "1"
- "2"
- "3"
- "4"
subcommands:
- name: deploy
display: Deploy
- name: diagnose
display: Diagnose
- name: logs
display: Logs
subcommands:
- name: k8s
display: K8s
args:
- name: Service
type: enum
required: true
options:
- agent-mesh-hub
- import-data
- observer
- openresty
- postgres
- prepare-static
- prisma
- prisma-worker
- rollback
- server
flags:
- name: f
display: Follow
type: boolean
- name: ps
display: Show Status
subcommands:
- name: host
display: Host
args:
- name: Service
type: enum
required: true
options:
- containerd
- kubelet
flags:
- name: o
display: Output Format
type: enum
options:
- table
- json
- yaml
- name: k8s
display: K8s
args:
- name: Service
type: enum
required: true
options:
- agent-mesh-hub
- import-data
- observer
- openresty
- postgres
- prepare-static
- prisma
- prisma-worker
- rollback
- server
- name: upgrade
display: Upgrade
args:
- name: path
display: Path
description: Path to upgrade.tar.gz
type: string