diff --git a/.gitignore b/.gitignore index fdb3507..e1ddad7 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ dkms.conf # Go bin/* + +# Log Files +log/ diff --git a/src/main.go b/src/main.go index c441ec3..2257da8 100644 --- a/src/main.go +++ b/src/main.go @@ -4,8 +4,32 @@ import ( "fmt" "os" "flag" + "encoding/json" + "log" + "sort" ) +type CmdGroup struct { + Category string + Commands []CmdItem +} + +type CmdList []CmdGroup + +type CmdItem struct { + CommandName string `json:"command"` + CommandDescription string `json:"desc"` +} + +func (list CmdList) Get(category string) []CmdItem { + for _, group := range list { + if group.Category == category { + return group.Commands + } + } + return nil +} + func main() { @@ -15,22 +39,41 @@ func main() { flag.Parse() - if len(os.Args) == 1 { - StartTui() + configFile := "/etc/cheatsh/commands.json" + + var commands CmdList + var err error + + if *configFlag != "" { + commands, err = loadCommands(*configFlag) + if err != nil { + log.Fatalf("Cannot load commands file: %v", err) + } + StartTui(commands) + return + } else if len(os.Args) == 1 { + commands, err = loadCommands(configFile) + if err != nil { + log.Fatalf("Cannot load commands file: %v", err) + } + StartTui(commands) return } switch { + case *helpFlag: printHelp() - case *configFlag != "": - StartTui(*configFlag) + case *newFlag: HandleInput() + default: printHelp() os.Exit(1) + } + } func printHelp() { @@ -38,5 +81,32 @@ func printHelp() { Options: --config Specify a config file --help Show this help message - --new Add a new command to the config file`) + --new Add a new command to the config file`) +} + +func loadCommands(path string) (CmdList, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + var raw map[string][]CmdItem + err = json.Unmarshal(data, &raw) + if err != nil { + return nil, err + } + + cmdList := make(CmdList, 0, len(raw)) + for category, commands := range raw { + cmdList = append(cmdList, CmdGroup{ + Category: category, + Commands: commands, + }) + } + + sort.Slice(cmdList, func(i, j int) bool { + return cmdList[i].Category < cmdList[j].Category + }) + + return cmdList, nil } diff --git a/src/tui.go b/src/tui.go index 5b2f687..49288d2 100644 --- a/src/tui.go +++ b/src/tui.go @@ -1,9 +1,7 @@ package main import ( - "encoding/json" "fmt" - "log" "os" //"sort" //"strings" @@ -15,11 +13,6 @@ import ( "github.com/charmbracelet/lipgloss" ) -type CmdMap map[string][]CmdItem -type CmdItem struct { - CommandName string `json:"command"` - CommandDescription string `json:"desc"` -} type item struct { title string @@ -33,7 +26,7 @@ type view int type model struct { list list.Model - commands CmdMap + commands CmdList currentView view currentKey string selectedCmd string @@ -58,7 +51,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.currentKey = selected.title m.currentView = viewCommands m.list.Title = m.currentKey - m.list.SetItems(cmdItemsToList(m.commands[m.currentKey])) + m.list.SetItems(cmdItemsToList(m.commands.Get(m.currentKey))) } else if m.currentView == viewCommands { m.selectedCmd = selected.title return m, tea.Quit @@ -70,7 +63,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.currentView == viewCommands { m.currentView = viewCategories m.list.Title = "Choose a list of commands" - m.list.SetItems(cmdMapKeysToList(m.commands)) + m.list.SetItems(cmdListKeysToList(m.commands)) } } @@ -98,20 +91,7 @@ const ( viewCommands ) -func StartTui(configFilePath ...string) { - - configFile := "/etc/cheatsh/commands.json" - if len(configFilePath) > 0 { - configFile = configFilePath[0] - } - - commands, err := loadCommands(configFile) - if err != nil { - log.Fatalf("Cannot load commands file: %v", err) - } - - // ToDo: Sort Items - // ToDo: Handle readout of file before calling this function +func StartTui(commands CmdList) { delegate := list.NewDefaultDelegate() backKey := key.NewBinding( @@ -129,7 +109,7 @@ func StartTui(configFilePath ...string) { } } - items := cmdMapKeysToList(commands) + items := cmdListKeysToList(commands) l := list.New(items, delegate, 0, 0) l.Title = "Choose a list of commands" @@ -151,34 +131,16 @@ func StartTui(configFilePath ...string) { } } -func loadCommands(path string) (CmdMap, error) { - - data, err := os.ReadFile(path) - if err != nil { - return nil, err - } - - var m CmdMap - err = json.Unmarshal(data, &m) - if err != nil { - return nil, err - } - - return m, nil -} - -func cmdMapKeysToList(m CmdMap) []list.Item { - - items := []list.Item{} - - for name, _ := range m { - listItem := item{ - title: name, - description: "", - } - items = append(items, listItem) - } - return items +func cmdListKeysToList(cmds CmdList) []list.Item { + items := []list.Item{} + for _, group := range cmds { + listItem := item{ + title: group.Category, + description: "", + } + items = append(items, listItem) + } + return items } func cmdItemsToList(cmds []CmdItem) []list.Item {