forked from Elara6331/itd
Switch to iota for request types and move to types package
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ var addressCmd = &cobra.Command{
|
||||
Short: "Get InfiniTime's bluetooth address",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -44,7 +45,7 @@ var addressCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeBtAddress,
|
||||
Type: types.ReqTypeBtAddress,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ var batteryCmd = &cobra.Command{
|
||||
Short: "Get battery level from InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -44,7 +45,7 @@ var batteryCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeBattLevel,
|
||||
Type: types.ReqTypeBattLevel,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@@ -18,23 +18,6 @@
|
||||
|
||||
package cmd
|
||||
|
||||
const SockPath = "/tmp/itd/socket"
|
||||
|
||||
const (
|
||||
ReqTypeHeartRate = "hrt"
|
||||
ReqTypeBattLevel = "battlvl"
|
||||
ReqTypeFwVersion = "fwver"
|
||||
ReqTypeFwUpgrade = "fwupg"
|
||||
ReqTypeBtAddress = "btaddr"
|
||||
ReqTypeNotify = "notify"
|
||||
ReqTypeSetTime = "settime"
|
||||
)
|
||||
|
||||
const (
|
||||
UpgradeTypeArchive = iota
|
||||
UpgradeTypeFiles
|
||||
)
|
||||
|
||||
type DFUProgress struct {
|
||||
Received int64 `mapstructure:"recvd"`
|
||||
Total int64 `mapstructure:"total"`
|
||||
|
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,7 @@ var heartCmd = &cobra.Command{
|
||||
Short: "Get heart rate from InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -43,7 +44,7 @@ var heartCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeHeartRate,
|
||||
Type: types.ReqTypeHeartRate,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@@ -25,6 +25,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -40,7 +41,7 @@ var notifyCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -48,7 +49,7 @@ var notifyCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeNotify,
|
||||
Type: types.ReqTypeNotify,
|
||||
Data: types.ReqDataNotify{
|
||||
Title: args[0],
|
||||
Body: args[1],
|
||||
|
@@ -21,6 +21,7 @@ package cmd
|
||||
import (
|
||||
"github.com/abiosoft/ishell"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
@@ -63,3 +64,15 @@ func Execute() {
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
cobra.CheckErr(rootCmd.Execute())
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Register flag for socket path
|
||||
rootCmd.Flags().StringP("socket-path", "s", "", "Path to itd socket")
|
||||
|
||||
// Bind flag and environment variable to viper key
|
||||
viper.BindPFlag("sockPath", rootCmd.Flags().Lookup("socket-path"))
|
||||
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
|
||||
|
||||
// Set default value for socket path
|
||||
viper.SetDefault("sockPath", "/tmp/itd/socket")
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -39,9 +40,9 @@ var timeCmd = &cobra.Command{
|
||||
log.Warn().Msg("Command time requires one argument")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -49,7 +50,7 @@ var timeCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeSetTime,
|
||||
Type: types.ReqTypeSetTime,
|
||||
Data: args[0],
|
||||
})
|
||||
if err != nil {
|
||||
|
@@ -38,7 +38,7 @@ var upgradeCmd = &cobra.Command{
|
||||
Aliases: []string{"upg"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -49,13 +49,13 @@ var upgradeCmd = &cobra.Command{
|
||||
if viper.GetString("archive") != "" {
|
||||
// Get archive data struct
|
||||
data = types.ReqDataFwUpgrade{
|
||||
Type: UpgradeTypeArchive,
|
||||
Type: types.UpgradeTypeArchive,
|
||||
Files: []string{viper.GetString("archive")},
|
||||
}
|
||||
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
|
||||
// Get files data struct
|
||||
data = types.ReqDataFwUpgrade{
|
||||
Type: UpgradeTypeFiles,
|
||||
Type: types.UpgradeTypeFiles,
|
||||
Files: []string{viper.GetString("initPkt"), viper.GetString("firmware")},
|
||||
}
|
||||
} else {
|
||||
@@ -66,7 +66,7 @@ var upgradeCmd = &cobra.Command{
|
||||
|
||||
// Encode response into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeFwUpgrade,
|
||||
Type: types.ReqTypeFwUpgrade,
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
|
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ var versionCmd = &cobra.Command{
|
||||
Short: "Get firmware version of InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@@ -44,7 +45,7 @@ var versionCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeFwVersion,
|
||||
Type: types.ReqTypeFwVersion,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
Reference in New Issue
Block a user