Update itctl to use api

This commit is contained in:
2021-10-23 18:41:03 -07:00
parent e198b769f9
commit ef29b9bee4
10 changed files with 71 additions and 294 deletions

View File

@@ -19,16 +19,12 @@
package get
import (
"bufio"
"encoding/json"
"fmt"
"net"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types"
"go.arsenm.dev/itd/api"
)
// steps.goCmd represents the steps.go command
@@ -36,42 +32,11 @@ var motionCmd = &cobra.Command{
Use: "motion",
Short: "Get motion values from InfiniTime",
Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket
conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
}
defer conn.Close()
client := viper.Get("client").(*api.Client)
// Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{
Type: types.ReqTypeMotion,
})
motionVals, err := client.Motion()
if err != nil {
log.Fatal().Err(err).Msg("Error making request")
}
// Read one line from connection
line, _, err := bufio.NewReader(conn).ReadLine()
if err != nil {
log.Fatal().Err(err).Msg("Error reading line from connection")
}
var res types.Response
// Decode line into response
err = json.Unmarshal(line, &res)
if err != nil {
log.Fatal().Err(err).Msg("Error decoding JSON data")
}
var motionVals types.MotionValues
err = mapstructure.Decode(res.Value, &motionVals)
if err != nil {
log.Fatal().Err(err).Msg("Error decoding motion values")
}
if res.Error {
log.Fatal().Msg(res.Message)
log.Fatal().Err(err).Msg("Error getting motion values")
}
if viper.GetBool("shell") {