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
10 changes: 10 additions & 0 deletions luci.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ description = "Luci config hello world example."
[bash.run]
example = "echo Hello World!"

[bash.group]
title = "Group 1"

[bash.group.value]
action1 = "echo g1 - s1 - action1"

[bash.group.value.action2]
title = "SubGroup action2"
value = "echo g1 - s1 - action2"

[zshell.run]
example = "echo Hello World!"

Expand Down
27 changes: 12 additions & 15 deletions utils/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,23 @@ func GetShellConfig(c types.Config) *types.ShellConfig {
}

// Return an Action or ActionRecord within the config c.
func Dig(c types.Config, inputs []string) any {
shellc := *GetShellConfig(c)
action := shellc[inputs[0]]
if action == nil {
return nil
}
func Dig(action any, inputs []string) any {
for i, input := range inputs {
switch actTyped := action.(type) {
case types.ShellConfig:
action = actTyped[input]

for _, input := range inputs[1:] {
switch action.(type) {
case types.AnnotatedAction:
val := action.(types.AnnotatedAction).Value
switch val := val.(type) {
case map[string]any:
action = val[input]
}
action = Dig(actTyped.Value, inputs[i:])
continue

case map[string]any:
action = action.(map[string]any)[input]
if actTyped["value"] != nil {
action = Dig(MapToAnnotatedAction(actTyped), inputs[i:])
continue
}
action = actTyped[input]
}
}

return action
}
4 changes: 2 additions & 2 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func digForAnnotatedActions(m map[string]any) {
digForAnnotatedActions(v)
continue
}
annAct := mapToAnnotatedAction(v)
annAct := MapToAnnotatedAction(v)
m[k] = annAct
}
}
}

func mapToAnnotatedAction(m map[string]any) types.AnnotatedAction {
func MapToAnnotatedAction(m map[string]any) types.AnnotatedAction {
title := ""
if m["title"] != nil {
title = m["title"].(string)
Expand Down
7 changes: 4 additions & 3 deletions utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
)

func Act(c types.Config, inputs []string) {
action := Dig(c, inputs)
shell := *GetShellConfig(c)
action := Dig(shell, inputs)

if action == nil {
if len(inputs) <= 1 {
PrintUsage(c)
return
}
PrintAction(c, inputs[0:len(inputs)-1], 0)
PrintActionWithInputs(shell, inputs[0:len(inputs)-1], 0)
return
}

Expand Down Expand Up @@ -51,7 +52,7 @@ func Act(c types.Config, inputs []string) {
}
}

PrintAction(c, inputs, 0)
PrintActionWithInputs(shell, inputs, 0)
}

func execAction(action any) bool {
Expand Down
47 changes: 28 additions & 19 deletions utils/print.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package utils

import (
"errors"
"fmt"
"reflect"
"strings"

"github.com/fatih/color"
Expand All @@ -27,39 +27,48 @@ func PrintHeader(c types.Config) {

func PrintUsage(c types.Config) {
PrintHeader(c)
for action := range *GetShellConfig(c) {
PrintAction(c, []string{action}, 0)
shell := *GetShellConfig(c)
for action := range shell {
PrintActionWithInputs(shell, []string{action}, 0)
}
}

func PrintAction(c types.Config, inputs []string, level int) {
func PrintActionWithInputs(c map[string]any, inputs []string, level int) error {
action := Dig(c, inputs)
if action == nil {
PrintUsage(c)
return
return errors.New("Action couldn't be found!")
}
PrintAction(action, inputs, level)
return nil
}

switch Dig(c, inputs).(type) {
func PrintAction(action any, inputs []string, level int) {
switch action := action.(type) {
case types.AnnotatedAction:
color.New(color.FgMagenta).Printf("%sluci %s\n", indent(level), strings.Join(inputs, " "))
annAct := action.(types.AnnotatedAction)
if annAct.Title != "" {
color.Blue("%s** %s **", indent(level+1), annAct.Title)
if action.Title != "" {
color.Blue("%s** %s **", indent(level+1), action.Title)
}
if annAct.Description != "" {
color.Black("%s> %s", indent(level+1), annAct.Description)
if action.Description != "" {
color.Black("%s> %s", indent(level+1), action.Description)
}
if reflect.ValueOf(annAct.Value).Kind() == reflect.Map {
for key := range annAct.Value.(map[string]any) {
PrintAction(c, append(inputs, key), level+1)
switch annVal := action.Value.(type) {
case map[string]any:
if annVal["value"] != nil {
fmt.Println("", inputs)
PrintAction(MapToAnnotatedAction(annVal), inputs, level+1)
return
}
}
return
PrintAction(action.Value, inputs, level+1)

case map[string]any:
m := action.(map[string]any)
for key := range m {
PrintAction(c, append(inputs, key), level)
if action["value"] != nil {
PrintAction(MapToAnnotatedAction(action), inputs, level)
return
}
for key := range action {
PrintAction(action[key], append(inputs, key), level)
}

case []string:
Expand Down