aboutsummaryrefslogtreecommitdiff
path: root/pkg/client/input.go
blob: bcdab133635864111d03456a88a0648a35b68278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package client

import (
	"bufio"
	"fmt"
	"os"
)

// getValidInput returns the selected menu command as a string or an error
func (s *State) getValidInput() (string, error) {
	menu := s.config.Menus[s.currentMenu]

	reader := bufio.NewReader(os.Stdin)
	for {
		input, err := reader.ReadByte()
		if err != nil {
			return "", fmt.Errorf("Could not read byte from stdin : %w", err)
		}
		for _, menuEntry := range menu.MenuEntries {
			if []byte(menuEntry.Key)[0] == input {
				return menuEntry.Action, nil
			}
		}
	}
}