diff --git a/luci.config.toml b/luci.config.toml index 89553d6..d0876c1 100755 --- a/luci.config.toml +++ b/luci.config.toml @@ -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!" diff --git a/utils/action.go b/utils/action.go index f813951..8b71472 100644 --- a/utils/action.go +++ b/utils/action.go @@ -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 } diff --git a/utils/config.go b/utils/config.go index 73c2e0e..ac7e30a 100644 --- a/utils/config.go +++ b/utils/config.go @@ -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) diff --git a/utils/exec.go b/utils/exec.go index 0989c26..37df1c3 100644 --- a/utils/exec.go +++ b/utils/exec.go @@ -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 } @@ -51,7 +52,7 @@ func Act(c types.Config, inputs []string) { } } - PrintAction(c, inputs, 0) + PrintActionWithInputs(shell, inputs, 0) } func execAction(action any) bool { diff --git a/utils/print.go b/utils/print.go index 15dc24a..b6b63bf 100644 --- a/utils/print.go +++ b/utils/print.go @@ -1,8 +1,8 @@ package utils import ( + "errors" "fmt" - "reflect" "strings" "github.com/fatih/color" @@ -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: