Enhance CLI history, improve error handling, improve testing
This commit is contained in:
parent
bf9f3e19c4
commit
c15ffeab6f
@ -29,11 +29,12 @@ var noHist = map[string]bool{"clear": true, "exit": true, "quit": true}
|
|||||||
|
|
||||||
// Interpret is where we will actuall define our Commands
|
// Interpret is where we will actuall define our Commands
|
||||||
func executor(cmd string) {
|
func executor(cmd string) {
|
||||||
|
var status = 0
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Error().Msgf("PANIC: %s", r)
|
log.Error().Msgf("PANIC: %s", r)
|
||||||
}
|
}
|
||||||
if _, ok := noHist[cmd]; !ok {
|
if _, ok := noHist[cmd]; !ok && status == 0 {
|
||||||
history = append(history, cmd)
|
history = append(history, cmd)
|
||||||
go saveHist()
|
go saveHist()
|
||||||
}
|
}
|
||||||
@ -54,12 +55,15 @@ func executor(cmd string) {
|
|||||||
println("use: use <bridge>")
|
println("use: use <bridge>")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if br, ok := ziggy.Lucifer.Bridges[args[1]]; !ok {
|
br, ok := ziggy.Lucifer.Bridges[args[1]]
|
||||||
println("invalid bridge: " + args[1])
|
if !ok {
|
||||||
} else {
|
log.Error().Msg("invalid bridge: " + args[1])
|
||||||
sel.Bridge = args[1]
|
status = 1
|
||||||
log.Info().Str("host", br.Host).Int("lights", len(br.HueLights)).Msg("switched to bridge: " + sel.Bridge)
|
return
|
||||||
}
|
}
|
||||||
|
sel.Bridge = args[1]
|
||||||
|
log.Info().Str("host", br.Host).Int("lights", len(br.HueLights)).Msg("switched to bridge: " + sel.Bridge)
|
||||||
|
|
||||||
case "debug":
|
case "debug":
|
||||||
levelsdebug := map[string]zerolog.Level{"info": zerolog.InfoLevel, "debug": zerolog.DebugLevel, "trace": zerolog.TraceLevel}
|
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"}
|
debuglevels := map[zerolog.Level]string{zerolog.InfoLevel: "info", zerolog.DebugLevel: "debug", zerolog.TraceLevel: "trace"}
|
||||||
@ -94,6 +98,7 @@ func executor(cmd string) {
|
|||||||
bcmd, ok := Commands[args[0]]
|
bcmd, ok := Commands[args[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error().Msg("invalid command: " + args[0])
|
log.Error().Msg("invalid command: " + args[0])
|
||||||
|
status = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
br, ok := ziggy.Lucifer.Bridges[sel.Bridge]
|
br, ok := ziggy.Lucifer.Bridges[sel.Bridge]
|
||||||
@ -115,11 +120,13 @@ func executor(cmd string) {
|
|||||||
}
|
}
|
||||||
}(br)
|
}(br)
|
||||||
}
|
}
|
||||||
} else {
|
return
|
||||||
err := bcmd.reactor(br, args[1:])
|
}
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("error executing command")
|
err := bcmd.reactor(br, args[1:])
|
||||||
}
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("error executing command")
|
||||||
|
status = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,17 +172,29 @@ var (
|
|||||||
histLoaded bool
|
histLoaded bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func loadHist() {
|
||||||
|
var histMap = make(map[string]bool)
|
||||||
|
pth, _ := filepath.Split(config.Filename)
|
||||||
|
rb, _ := os.OpenFile(filepath.Join(pth, ".ziggs_history"), os.O_RDONLY, 0644)
|
||||||
|
xerox := bufio.NewScanner(rb)
|
||||||
|
for xerox.Scan() {
|
||||||
|
_, ok := histMap[strings.TrimSpace(xerox.Text())]
|
||||||
|
switch {
|
||||||
|
case strings.TrimSpace(xerox.Text()) == "":
|
||||||
|
continue
|
||||||
|
case ok:
|
||||||
|
continue
|
||||||
|
default:
|
||||||
|
histMap[strings.TrimSpace(xerox.Text())] = true
|
||||||
|
history = append(history, xerox.Text())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
histLoaded = true
|
||||||
|
}
|
||||||
|
|
||||||
func getHist() []string {
|
func getHist() []string {
|
||||||
if !histLoaded {
|
if !histLoaded {
|
||||||
pth, _ := filepath.Split(config.Filename)
|
loadHist()
|
||||||
rb, _ := os.OpenFile(filepath.Join(pth, ".ziggs_history"), os.O_RDONLY, 0644)
|
|
||||||
xerox := bufio.NewScanner(rb)
|
|
||||||
for xerox.Scan() {
|
|
||||||
if strings.TrimSpace(xerox.Text()) != "" {
|
|
||||||
history = append(history, xerox.Text())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
histLoaded = true
|
|
||||||
}
|
}
|
||||||
return history
|
return history
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func cmdSet(bridge *ziggy.Bridge, args []string) error {
|
|||||||
return errors.New("not enough arguments")
|
return errors.New("not enough arguments")
|
||||||
}
|
}
|
||||||
argHead++
|
argHead++
|
||||||
newHue, numErr := strconv.Atoi(args[argHead])
|
newHue, numErr := strconv.Atoi(strings.TrimSpace(args[argHead]))
|
||||||
if numErr != nil || newHue > 65535 || newHue < 0 {
|
if numErr != nil || newHue > 65535 || newHue < 0 {
|
||||||
return fmt.Errorf("given hue is not a valid number: %w", numErr)
|
return fmt.Errorf("given hue is not a valid number: %w", numErr)
|
||||||
}
|
}
|
||||||
@ -159,9 +159,9 @@ func cmdSet(bridge *ziggy.Bridge, args []string) error {
|
|||||||
return errors.New("not enough arguments")
|
return errors.New("not enough arguments")
|
||||||
}
|
}
|
||||||
argHead++
|
argHead++
|
||||||
newSat, numErr := strconv.Atoi(args[argHead])
|
newSat, numErr := strconv.Atoi(strings.TrimSpace(args[argHead]))
|
||||||
if numErr != nil || newSat > 255 || newSat < 0 {
|
if numErr != nil {
|
||||||
return fmt.Errorf("given saturation is not a valid number: %w", numErr)
|
return fmt.Errorf("given saturation is not a valid number: %v", numErr)
|
||||||
}
|
}
|
||||||
actions = append(actions, func() error {
|
actions = append(actions, func() error {
|
||||||
err := target.Sat(uint8(newSat))
|
err := target.Sat(uint8(newSat))
|
||||||
@ -175,7 +175,7 @@ func cmdSet(bridge *ziggy.Bridge, args []string) error {
|
|||||||
return errors.New("not enough arguments")
|
return errors.New("not enough arguments")
|
||||||
}
|
}
|
||||||
argHead++
|
argHead++
|
||||||
newTemp, numErr := strconv.Atoi(args[argHead])
|
newTemp, numErr := strconv.Atoi(strings.TrimSpace(args[argHead]))
|
||||||
if numErr != nil || newTemp > 500 || newTemp < 153 {
|
if numErr != nil || newTemp > 500 || newTemp < 153 {
|
||||||
terr := fmt.Errorf("given temperature is not a valid number: %w", numErr)
|
terr := fmt.Errorf("given temperature is not a valid number: %w", numErr)
|
||||||
if numErr == nil {
|
if numErr == nil {
|
||||||
@ -214,7 +214,7 @@ func cmdSet(bridge *ziggy.Bridge, args []string) error {
|
|||||||
if argHead > len(args)-1 {
|
if argHead > len(args)-1 {
|
||||||
return errors.New("not enough arguments")
|
return errors.New("not enough arguments")
|
||||||
}
|
}
|
||||||
targetScene := args[argHead]
|
targetScene := strings.TrimSpace(args[argHead])
|
||||||
actions = append(actions, func() error {
|
actions = append(actions, func() error {
|
||||||
err := target.Scene(targetScene)
|
err := target.Scene(targetScene)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -44,6 +45,9 @@ func grind(ctx context.Context) {
|
|||||||
var once = &sync.Once{}
|
var once = &sync.Once{}
|
||||||
|
|
||||||
func TestCPULoadGradient(t *testing.T) {
|
func TestCPULoadGradient(t *testing.T) {
|
||||||
|
if os.Getenv("ZIGGS_TEST_CPU_LOAD") == "" {
|
||||||
|
t.Skip("skipping CPU load test")
|
||||||
|
}
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
grad, err := CPULoadGradient(ctx, "deepskyblue", "deeppink")
|
grad, err := CPULoadGradient(ctx, "deepskyblue", "deeppink")
|
||||||
@ -72,7 +76,7 @@ func TestCPULoadGradient(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCoreLoadHue(t *testing.T) {
|
func TestCoreLoadHue(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(20*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
huint, err := CoreLoadHue(ctx)
|
huint, err := CoreLoadHue(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user