ziggs/internal/cli/cli.go

198 lines
4.3 KiB
Go
Raw Normal View History

2022-09-26 15:25:21 +00:00
package cli
2022-03-07 14:57:42 +00:00
import (
"fmt"
"os"
"strings"
"time"
cli "git.tcp.direct/Mirrors/go-prompt"
tui "github.com/manifoldco/promptui"
"github.com/rs/zerolog"
2022-08-30 06:04:48 +00:00
"github.com/davecgh/go-spew/spew"
2022-07-29 10:44:08 +00:00
"git.tcp.direct/kayos/ziggs/internal/common"
"git.tcp.direct/kayos/ziggs/internal/config"
"git.tcp.direct/kayos/ziggs/internal/ziggy"
2022-03-07 14:57:42 +00:00
)
2022-09-06 00:29:02 +00:00
var (
log *zerolog.Logger
prompt *cli.Prompt
extraDebug = false
)
2022-03-07 14:57:42 +00:00
2022-09-26 15:25:21 +00:00
// Interpret is where we will actuall define our CLICommands
2022-03-07 14:57:42 +00:00
func executor(cmd string) {
cmd = strings.TrimSpace(cmd)
2022-07-31 08:23:26 +00:00
var args = strings.Fields(cmd)
2022-08-30 06:04:48 +00:00
if len(args) == 0 {
return
}
2022-03-07 14:57:42 +00:00
switch args[0] {
case "quit", "exit":
os.Exit(0)
case "use":
2022-09-06 00:29:02 +00:00
if len(ziggy.Lucifer.Bridges) < 2 {
return
}
2022-07-08 21:35:11 +00:00
if len(args) < 2 {
println("use: use <bridge>")
return
2022-03-07 14:57:42 +00:00
}
2022-07-09 00:09:49 +00:00
if br, ok := ziggy.Lucifer.Bridges[args[1]]; !ok {
2022-03-07 14:57:42 +00:00
println("invalid bridge: " + args[1])
} else {
2022-09-06 00:29:02 +00:00
sel.Bridge = args[1]
log.Info().Str("host", br.Host).Int("lights", len(br.HueLights)).Msg("switched to bridge: " + sel.Bridge)
2022-03-07 14:57:42 +00:00
}
case "debug":
levelsdebug := map[string]zerolog.Level{"info": zerolog.InfoLevel, "debug": zerolog.DebugLevel, "trace": zerolog.TraceLevel}
debuglevels := map[zerolog.Level]string{zerolog.InfoLevel: "info", zerolog.DebugLevel: "debug", zerolog.TraceLevel: "trace"}
if len(args) < 2 {
println("current debug level: " + debuglevels[log.GetLevel()])
}
if newlevel, ok := levelsdebug[args[1]]; ok {
zerolog.SetGlobalLevel(newlevel)
2022-09-06 00:29:02 +00:00
} else {
println("invalid argument: " + args[1])
2022-03-07 14:57:42 +00:00
}
case "help":
if len(args) < 2 {
getHelp("meta")
return
}
getHelp(args[len(args)-1])
case "clear":
print("\033[H\033[2J")
2022-09-06 00:29:02 +00:00
case "debugcli":
if extraDebug {
extraDebug = false
} else {
extraDebug = true
}
2022-08-30 06:04:48 +00:00
spew.Dump(suggestions)
2022-03-07 14:57:42 +00:00
default:
2022-08-30 06:04:48 +00:00
if len(args) == 0 {
return
}
2022-09-26 15:25:21 +00:00
bcmd, ok := CLICommands[args[0]]
2022-03-07 14:57:42 +00:00
if !ok {
return
}
2022-09-06 00:29:02 +00:00
br, ok := ziggy.Lucifer.Bridges[sel.Bridge]
if sel.Bridge == "" || !ok {
q := tui.Select{
2022-07-09 00:09:49 +00:00
Label: "Send to all known bridges?",
Items: []string{"yes", "no"},
Pointer: common.ZiggsPointer,
}
2022-09-06 00:29:02 +00:00
_, ch, _ := q.Run()
2022-07-09 00:09:49 +00:00
if ch != "yes" {
return
}
for _, br := range ziggy.Lucifer.Bridges {
2022-08-30 06:04:48 +00:00
go func(brj *ziggy.Bridge) {
2022-09-26 15:25:21 +00:00
err := bcmd.reactor(brj, args[1:])
2022-08-30 06:04:48 +00:00
if err != nil {
log.Error().Err(err).Msg("bridge command failed")
}
}(br)
2022-03-07 14:57:42 +00:00
}
} else {
2022-09-26 15:25:21 +00:00
err := bcmd.reactor(br, args[1:])
2022-07-08 21:35:11 +00:00
if err != nil {
log.Error().Err(err).Msg("error executing command")
}
2022-03-07 14:57:42 +00:00
}
}
}
func getHelp(target string) {
2022-09-26 15:25:21 +00:00
/* if target == "" {
*/
for _, sug := range suggestions {
for _, su := range sug {
println(su.Text + "(" + strings.Join(su.inner.aliases, ", ") + ")\t" + su.Description)
2022-03-07 14:57:42 +00:00
}
2022-09-26 15:25:21 +00:00
}
return
// }
2022-03-07 14:57:42 +00:00
}
2022-07-09 00:09:49 +00:00
func cmdScan(br *ziggy.Bridge, args []string) error {
2022-03-07 14:57:42 +00:00
r, err := br.FindLights()
if err != nil {
return err
}
for resp := range r.Success {
2022-03-07 14:57:42 +00:00
log.Info().Msg(resp)
}
var count = 0
timer := time.NewTimer(5 * time.Second)
var newLights []string
loop:
for {
select {
case <-timer.C:
break loop
default:
newl, _ := br.GetNewLights()
if len(newl.Lights) <= count {
time.Sleep(250 * time.Millisecond)
print(".")
continue
}
count = len(newl.Lights)
timer.Reset(5 * time.Second)
newLights = append(newLights, newl.Lights...)
}
}
for _, nl := range newLights {
log.Info().Str("caller", nl).Msg("discovered light")
}
return nil
}
const bulb = ``
2022-07-31 08:23:26 +00:00
func getHist() []string {
return []string{}
}
2022-03-07 14:57:42 +00:00
func StartCLI() {
log = config.GetLogger()
2022-09-06 00:29:02 +00:00
processBridges()
grpmap, err := ziggy.GetGroupMap()
2022-09-06 00:29:02 +00:00
if err != nil {
log.Fatal().Err(err).Msg("error getting group map")
2022-07-31 08:23:26 +00:00
}
2022-09-06 00:29:02 +00:00
processGroups(grpmap)
processLights()
prompt = cli.New(
2022-03-07 14:57:42 +00:00
executor,
completer,
// cli.OptionPrefixBackgroundColor(cli.Black),
cli.OptionPrefixTextColor(cli.Yellow),
2022-07-31 08:23:26 +00:00
cli.OptionHistory(getHist()),
2022-03-07 14:57:42 +00:00
cli.OptionSuggestionBGColor(cli.Black),
cli.OptionSuggestionTextColor(cli.White),
cli.OptionSelectedSuggestionBGColor(cli.Black),
cli.OptionSelectedSuggestionTextColor(cli.Green),
cli.OptionLivePrefix(
func() (prefix string, useLivePrefix bool) {
2022-09-06 00:29:02 +00:00
if len(ziggy.Lucifer.Bridges) == 1 {
2022-09-22 03:48:13 +00:00
for brid := range ziggy.Lucifer.Bridges {
2022-09-06 00:29:02 +00:00
sel.Bridge = brid
}
2022-03-07 14:57:42 +00:00
}
2022-09-06 00:29:02 +00:00
return fmt.Sprintf("ziggs[%s] %s ", sel.String(), bulb), true
2022-03-07 14:57:42 +00:00
}),
cli.OptionTitle("ziggs"),
2022-07-29 10:44:08 +00:00
cli.OptionCompletionOnDown(),
)
2022-03-07 14:57:42 +00:00
2022-09-06 00:29:02 +00:00
prompt.Run()
2022-03-07 14:57:42 +00:00
}