Update itctl to use api
This commit is contained in:
parent
e45bfe3de8
commit
32cab6d00f
14
api/notify.go
Normal file
14
api/notify.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import "go.arsenm.dev/itd/internal/types"
|
||||||
|
|
||||||
|
func (c *Client) Notify(title string, body string) error {
|
||||||
|
_, err := c.request(types.Request{
|
||||||
|
Type: types.ReqTypeNotify,
|
||||||
|
Data: types.ReqDataNotify{
|
||||||
|
Title: title,
|
||||||
|
Body: body,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
@ -19,15 +19,11 @@
|
|||||||
package firmware
|
package firmware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/cheggaaa/pb/v3"
|
"github.com/cheggaaa/pb/v3"
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go.arsenm.dev/itd/api"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,47 +32,34 @@ type DFUProgress struct {
|
|||||||
Total int64 `mapstructure:"total"`
|
Total int64 `mapstructure:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// upgradeCmd represents the upgrade command
|
// upgradeCmd represents the upgrade command
|
||||||
var upgradeCmd = &cobra.Command{
|
var upgradeCmd = &cobra.Command{
|
||||||
Use: "upgrade",
|
Use: "upgrade",
|
||||||
Short: "Upgrade InfiniTime firmware using files or archive",
|
Short: "Upgrade InfiniTime firmware using files or archive",
|
||||||
Aliases: []string{"upg"},
|
Aliases: []string{"upg"},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
var data types.ReqDataFwUpgrade
|
var upgType api.UpgradeType
|
||||||
|
var files []string
|
||||||
// Get relevant data struct
|
// Get relevant data struct
|
||||||
if viper.GetString("archive") != "" {
|
if viper.GetString("archive") != "" {
|
||||||
// Get archive data struct
|
// Get archive data struct
|
||||||
data = types.ReqDataFwUpgrade{
|
upgType = types.UpgradeTypeArchive
|
||||||
Type: types.UpgradeTypeArchive,
|
files = []string{viper.GetString("archive")}
|
||||||
Files: []string{viper.GetString("archive")},
|
|
||||||
}
|
|
||||||
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
|
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
|
||||||
// Get files data struct
|
// Get files data struct
|
||||||
data = types.ReqDataFwUpgrade{
|
upgType = types.UpgradeTypeFiles
|
||||||
Type: types.UpgradeTypeFiles,
|
files = []string{viper.GetString("initPkt"), viper.GetString("firmware")}
|
||||||
Files: []string{viper.GetString("initPkt"), viper.GetString("firmware")},
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
cmd.Usage()
|
cmd.Usage()
|
||||||
log.Warn().Msg("Upgrade command requires either archive or init packet and firmware.")
|
log.Warn().Msg("Upgrade command requires either archive or init packet and firmware.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode response into connection
|
progress, err := client.FirmwareUpgrade(upgType, files...)
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeFwUpgrade,
|
|
||||||
Data: data,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error initiating DFU")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create progress bar template
|
// Create progress bar template
|
||||||
@ -84,23 +67,7 @@ var upgradeCmd = &cobra.Command{
|
|||||||
// Start full bar at 0 total
|
// Start full bar at 0 total
|
||||||
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
bar := pb.ProgressBarTemplate(barTmpl).Start(0)
|
||||||
// Create new scanner of connection
|
// Create new scanner of connection
|
||||||
scanner := bufio.NewScanner(conn)
|
for event := range progress {
|
||||||
for scanner.Scan() {
|
|
||||||
var res types.Response
|
|
||||||
// Decode scanned line into response struct
|
|
||||||
err = json.Unmarshal(scanner.Bytes(), &res)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error decoding JSON response")
|
|
||||||
}
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
|
||||||
var event DFUProgress
|
|
||||||
// Decode response data into progress struct
|
|
||||||
err = mapstructure.Decode(res.Value, &event)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error decoding response data")
|
|
||||||
}
|
|
||||||
// Set total bytes in progress bar
|
// Set total bytes in progress bar
|
||||||
bar.SetTotal(event.Total)
|
bar.SetTotal(event.Total)
|
||||||
// Set amount of bytes received in progress bar
|
// Set amount of bytes received in progress bar
|
||||||
@ -112,9 +79,6 @@ var upgradeCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
// Finish progress bar
|
// Finish progress bar
|
||||||
bar.Finish()
|
bar.Finish()
|
||||||
if scanner.Err() != nil {
|
|
||||||
log.Fatal().Err(scanner.Err()).Msg("Error while scanning output")
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +19,12 @@
|
|||||||
package firmware
|
package firmware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// versionCmd represents the version command
|
// versionCmd represents the version command
|
||||||
@ -36,40 +33,14 @@ var versionCmd = &cobra.Command{
|
|||||||
Aliases: []string{"ver"},
|
Aliases: []string{"ver"},
|
||||||
Short: "Get firmware version of InfiniTime",
|
Short: "Get firmware version of InfiniTime",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
|
||||||
|
version, err := client.Version()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
log.Fatal().Err(err).Msg("Error getting firmware version")
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Encode request into connection
|
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeFwVersion,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read one line from connection
|
fmt.Println(version)
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print returned value
|
|
||||||
fmt.Println(res.Value)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +19,12 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// addressCmd represents the address command
|
// addressCmd represents the address command
|
||||||
@ -36,40 +33,14 @@ var addressCmd = &cobra.Command{
|
|||||||
Aliases: []string{"addr"},
|
Aliases: []string{"addr"},
|
||||||
Short: "Get InfiniTime's bluetooth address",
|
Short: "Get InfiniTime's bluetooth address",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
|
||||||
|
address, err := client.Address()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
log.Fatal().Err(err).Msg("Error getting bluetooth address")
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Encode request into connection
|
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeBtAddress,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read one line from connection
|
fmt.Println(address)
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print returned value
|
|
||||||
fmt.Println(res.Value)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +19,12 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// batteryCmd represents the batt command
|
// batteryCmd represents the batt command
|
||||||
@ -36,40 +33,15 @@ var batteryCmd = &cobra.Command{
|
|||||||
Aliases: []string{"batt"},
|
Aliases: []string{"batt"},
|
||||||
Short: "Get battery level from InfiniTime",
|
Short: "Get battery level from InfiniTime",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
// Encode request into connection
|
battLevel, err := client.BatteryLevel()
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeBattLevel,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error getting battery level")
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// Deocde line into response
|
|
||||||
err = json.Unmarshal(line, &res)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Error decoding JSON data")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print returned percentage
|
// Print returned percentage
|
||||||
fmt.Printf("%d%%\n", int(res.Value.(float64)))
|
fmt.Printf("%d%%\n", battLevel)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +19,12 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// heartCmd represents the heart command
|
// heartCmd represents the heart command
|
||||||
@ -35,40 +32,15 @@ var heartCmd = &cobra.Command{
|
|||||||
Use: "heart",
|
Use: "heart",
|
||||||
Short: "Get heart rate from InfiniTime",
|
Short: "Get heart rate from InfiniTime",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
// Encode request into connection
|
heartRate, err := client.HeartRate()
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeHeartRate,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error getting heart rate")
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print returned BPM
|
// Print returned BPM
|
||||||
fmt.Printf("%d BPM\n", int(res.Value.(float64)))
|
fmt.Printf("%d BPM\n", heartRate)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,16 +19,12 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// steps.goCmd represents the steps.go command
|
// steps.goCmd represents the steps.go command
|
||||||
@ -36,42 +32,11 @@ var motionCmd = &cobra.Command{
|
|||||||
Use: "motion",
|
Use: "motion",
|
||||||
Short: "Get motion values from InfiniTime",
|
Short: "Get motion values from InfiniTime",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
// Encode request into connection
|
motionVals, err := client.Motion()
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeMotion,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error getting motion values")
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetBool("shell") {
|
if viper.GetBool("shell") {
|
||||||
|
@ -19,15 +19,12 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// steps.goCmd represents the steps.go command
|
// steps.goCmd represents the steps.go command
|
||||||
@ -35,40 +32,15 @@ var stepsCmd = &cobra.Command{
|
|||||||
Use: "steps",
|
Use: "steps",
|
||||||
Short: "Get step count from InfiniTime",
|
Short: "Get step count from InfiniTime",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
// Encode request into connection
|
stepCount, err := client.StepCount()
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeStepCount,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error getting step count")
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print returned BPM
|
// Print returned BPM
|
||||||
fmt.Printf("%d Steps\n", int(res.Value.(float64)))
|
fmt.Printf("%d Steps\n", stepCount)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,15 +19,11 @@
|
|||||||
package notify
|
package notify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go.arsenm.dev/itd/api"
|
||||||
"go.arsenm.dev/itd/cmd/itctl/root"
|
"go.arsenm.dev/itd/cmd/itctl/root"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// notifyCmd represents the notify command
|
// notifyCmd represents the notify command
|
||||||
@ -41,40 +37,11 @@ var notifyCmd = &cobra.Command{
|
|||||||
log.Fatal().Msg("Command notify requires two arguments")
|
log.Fatal().Msg("Command notify requires two arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to itd UNIX socket
|
client := viper.Get("client").(*api.Client)
|
||||||
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()
|
|
||||||
|
|
||||||
// Encode request into connection
|
err := client.Notify(args[0], args[1])
|
||||||
err = json.NewEncoder(conn).Encode(types.Request{
|
|
||||||
Type: types.ReqTypeNotify,
|
|
||||||
Data: types.ReqDataNotify{
|
|
||||||
Title: args[0],
|
|
||||||
Body: args[1],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error making request")
|
log.Fatal().Err(err).Msg("Error sending notification")
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Error {
|
|
||||||
log.Fatal().Msg(res.Message)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,10 @@ package root
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/abiosoft/ishell"
|
"github.com/abiosoft/ishell"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go.arsenm.dev/itd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RootCmd represents the base command when called without any subcommands
|
// RootCmd represents the base command when called without any subcommands
|
||||||
@ -61,18 +63,25 @@ var RootCmd = &cobra.Command{
|
|||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
func Execute() {
|
||||||
|
client, err := api.New(viper.GetString("sockPath"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Error connecting to socket. Is itd running?")
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
viper.Set("client", client)
|
||||||
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
cobra.CheckErr(RootCmd.Execute())
|
cobra.CheckErr(RootCmd.Execute())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Register flag for socket path
|
// Register flag for socket path
|
||||||
RootCmd.Flags().StringP("socket-path", "s", "", "Path to itd socket")
|
RootCmd.Flags().StringP("socket-path", "s", api.DefaultAddr, "Path to itd socket")
|
||||||
|
|
||||||
// Bind flag and environment variable to viper key
|
// Bind flag and environment variable to viper key
|
||||||
viper.BindPFlag("sockPath", RootCmd.Flags().Lookup("socket-path"))
|
viper.BindPFlag("sockPath", RootCmd.Flags().Lookup("socket-path"))
|
||||||
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
|
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
|
||||||
|
|
||||||
// Set default value for socket path
|
// Set default value for socket path
|
||||||
viper.SetDefault("sockPath", "/tmp/itd/socket")
|
viper.SetDefault("sockPath", api.DefaultAddr)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user