From cb8d20724996d45751b933827d1b026a171f1a59 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Tue, 24 Aug 2021 20:32:17 -0700 Subject: [PATCH] Switch to iota for request types and move to types package --- cmd/itctl/cmd/address.go | 5 +++-- cmd/itctl/cmd/battery.go | 5 +++-- cmd/itctl/cmd/common.go | 17 ----------------- cmd/itctl/cmd/heart.go | 5 +++-- cmd/itctl/cmd/notify.go | 5 +++-- cmd/itctl/cmd/root.go | 13 +++++++++++++ cmd/itctl/cmd/time.go | 7 ++++--- cmd/itctl/cmd/upgrade.go | 8 ++++---- cmd/itctl/cmd/version.go | 5 +++-- internal/types/types.go | 19 +++++++++++++++++-- socket.go | 33 +++++++++------------------------ 11 files changed, 62 insertions(+), 60 deletions(-) diff --git a/cmd/itctl/cmd/address.go b/cmd/itctl/cmd/address.go index 29aa1aa..e2f492f 100644 --- a/cmd/itctl/cmd/address.go +++ b/cmd/itctl/cmd/address.go @@ -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") diff --git a/cmd/itctl/cmd/battery.go b/cmd/itctl/cmd/battery.go index 04bbc65..ab396c0 100644 --- a/cmd/itctl/cmd/battery.go +++ b/cmd/itctl/cmd/battery.go @@ -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") diff --git a/cmd/itctl/cmd/common.go b/cmd/itctl/cmd/common.go index 159426b..d7ee64d 100644 --- a/cmd/itctl/cmd/common.go +++ b/cmd/itctl/cmd/common.go @@ -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"` diff --git a/cmd/itctl/cmd/heart.go b/cmd/itctl/cmd/heart.go index ed0045a..4fc4871 100644 --- a/cmd/itctl/cmd/heart.go +++ b/cmd/itctl/cmd/heart.go @@ -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") diff --git a/cmd/itctl/cmd/notify.go b/cmd/itctl/cmd/notify.go index cac0b00..ddf8ca3 100644 --- a/cmd/itctl/cmd/notify.go +++ b/cmd/itctl/cmd/notify.go @@ -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], diff --git a/cmd/itctl/cmd/root.go b/cmd/itctl/cmd/root.go index 14c45bb..7a23e7e 100644 --- a/cmd/itctl/cmd/root.go +++ b/cmd/itctl/cmd/root.go @@ -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") +} diff --git a/cmd/itctl/cmd/time.go b/cmd/itctl/cmd/time.go index 95ec285..e69a2ff 100644 --- a/cmd/itctl/cmd/time.go +++ b/cmd/itctl/cmd/time.go @@ -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 { diff --git a/cmd/itctl/cmd/upgrade.go b/cmd/itctl/cmd/upgrade.go index dfdf173..6521fc9 100644 --- a/cmd/itctl/cmd/upgrade.go +++ b/cmd/itctl/cmd/upgrade.go @@ -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 { diff --git a/cmd/itctl/cmd/version.go b/cmd/itctl/cmd/version.go index 7f51e33..397587d 100644 --- a/cmd/itctl/cmd/version.go +++ b/cmd/itctl/cmd/version.go @@ -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") diff --git a/internal/types/types.go b/internal/types/types.go index f964bea..2e2fe95 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -1,5 +1,20 @@ package types +const ( + ReqTypeHeartRate = iota + ReqTypeBattLevel + ReqTypeFwVersion + ReqTypeFwUpgrade + ReqTypeBtAddress + ReqTypeNotify + ReqTypeSetTime +) + +const ( + UpgradeTypeArchive = iota + UpgradeTypeFiles +) + type ReqDataFwUpgrade struct { Type int Files []string @@ -12,11 +27,11 @@ type Response struct { } type Request struct { - Type string `json:"type"` + Type int `json:"type"` Data interface{} `json:"data,omitempty"` } type ReqDataNotify struct { Title string Body string -} \ No newline at end of file +} diff --git a/socket.go b/socket.go index 58429a9..0b0b2b4 100644 --- a/socket.go +++ b/socket.go @@ -34,21 +34,6 @@ import ( "go.arsenm.dev/itd/internal/types" ) -const ( - ReqTypeHeartRate = "hrt" - ReqTypeBattLevel = "battlvl" - ReqTypeFwVersion = "fwver" - ReqTypeFwUpgrade = "fwupg" - ReqTypeBtAddress = "btaddr" - ReqTypeNotify = "notify" - ReqTypeSetTime = "settime" -) - -const ( - UpgradeTypeArchive = iota - UpgradeTypeFiles -) - func startSocket(dev *infinitime.Device) error { // Make socket directory if non-existent err := os.MkdirAll(filepath.Dir(viper.GetString("socket.path")), 0755) @@ -107,7 +92,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { } switch req.Type { - case ReqTypeHeartRate: + case types.ReqTypeHeartRate: // Get heart rate from watch heartRate, err := dev.HeartRate() if err != nil { @@ -118,7 +103,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { json.NewEncoder(conn).Encode(types.Response{ Value: heartRate, }) - case ReqTypeBattLevel: + case types.ReqTypeBattLevel: // Get battery level from watch battLevel, err := dev.BatteryLevel() if err != nil { @@ -129,7 +114,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { json.NewEncoder(conn).Encode(types.Response{ Value: battLevel, }) - case ReqTypeFwVersion: + case types.ReqTypeFwVersion: // Get firmware version from watch version, err := dev.Version() if err != nil { @@ -140,12 +125,12 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { json.NewEncoder(conn).Encode(types.Response{ Value: version, }) - case ReqTypeBtAddress: + case types.ReqTypeBtAddress: // Encode bluetooth address to connection json.NewEncoder(conn).Encode(types.Response{ Value: dev.Address(), }) - case ReqTypeNotify: + case types.ReqTypeNotify: // If no data, return error if req.Data == nil { connErr(conn, nil, "Data required for notify request") @@ -166,7 +151,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { } // Encode empty types.Response to connection json.NewEncoder(conn).Encode(types.Response{}) - case ReqTypeSetTime: + case types.ReqTypeSetTime: // If no data, return error if req.Data == nil { connErr(conn, nil, "Data required for settime request") @@ -198,7 +183,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { } // Encode empty types.Response to connection json.NewEncoder(conn).Encode(types.Response{}) - case ReqTypeFwUpgrade: + case types.ReqTypeFwUpgrade: // If no data, return error if req.Data == nil { connErr(conn, nil, "Data required for firmware upgrade request") @@ -212,7 +197,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { break } switch reqData.Type { - case UpgradeTypeArchive: + case types.UpgradeTypeArchive: // If less than one file, return error if len(reqData.Files) < 1 { connErr(conn, nil, "Archive upgrade requires one file with .zip extension") @@ -229,7 +214,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) { connErr(conn, err, "Error loading archive file") break } - case UpgradeTypeFiles: + case types.UpgradeTypeFiles: // If less than two files, return error if len(reqData.Files) < 2 { connErr(conn, nil, "Files upgrade requires two files. First with .dat and second with .bin extension.")