From fe891cc7d48c7f79d284693086f28993f4f534fe Mon Sep 17 00:00:00 2001 From: sam80180 Date: Tue, 15 Aug 2023 02:02:39 +0800 Subject: [PATCH] fix: collect errors and print at last --- cmd/devices.go | 28 +++++++++++++++++++--------- cmd/listen.go | 10 ++++++---- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cmd/devices.go b/cmd/devices.go index ec412b3..7a883f2 100644 --- a/cmd/devices.go +++ b/cmd/devices.go @@ -20,11 +20,12 @@ package cmd import ( "encoding/json" "fmt" + "os" + giDevice "github.com/SonicCloudOrg/sonic-gidevice" "github.com/SonicCloudOrg/sonic-ios-bridge/src/entity" "github.com/SonicCloudOrg/sonic-ios-bridge/src/util" "github.com/spf13/cobra" - "os" ) var devicesCmd = &cobra.Command{ @@ -37,11 +38,12 @@ var devicesCmd = &cobra.Command{ return util.NewErrorPrint(util.ErrConnect, "usbMux", err) } list, err1 := usbMuxClient.Devices() - remoteList, err2 := util.ReadRemote() + remoteList, errRemoto := util.ReadRemote() if err1 != nil { return util.NewErrorPrint(util.ErrSendCommand, "listDevices", err1) } + todosErrores := []error{} if len(list) != 0 || len(remoteList) != 0 { if len(list) == 0 { list = []giDevice.Device{} @@ -53,25 +55,27 @@ var devicesCmd = &cobra.Command{ if isDetail { detail, err2 := entity.GetDetail(d) if err2 != nil { - return err2 + todosErrores = append(todosErrores, err2) + } else { + device.DeviceDetail = *detail } - device.DeviceDetail = *detail } json.Unmarshal(deviceByte, device) device.Status = device.GetStatus() device.RemoteAddr = "localhost" deviceList.DeviceList = append(deviceList.DeviceList, *device) } - if err2 == nil { + if errRemoto == nil { for k, dev := range remoteList { deviceByte, _ := json.Marshal(dev.Properties()) device := &entity.Device{} if isDetail { detail, err2 := entity.GetDetail(dev) if err2 != nil { - return err2 + todosErrores = append(todosErrores, err2) + } else { + device.DeviceDetail = *detail } - device.DeviceDetail = *detail } json.Unmarshal(deviceByte, device) device.Status = device.GetStatus() @@ -93,9 +97,10 @@ var devicesCmd = &cobra.Command{ if isDetail { detail, err2 := entity.GetDetail(d) if err2 != nil { - return err2 + todosErrores = append(todosErrores, err2) + } else { + device.DeviceDetail = *detail } - device.DeviceDetail = *detail } json.Unmarshal(deviceByte, device) device.Status = device.GetStatus() @@ -114,6 +119,11 @@ var devicesCmd = &cobra.Command{ os.Exit(0) } } + if len(todosErrores) > 0 { + for _, e := range todosErrores { + fmt.Fprintf(os.Stderr, "%+v\n", e) + } + } return nil }, } diff --git a/cmd/listen.go b/cmd/listen.go index 5d9f6d5..891ddcb 100644 --- a/cmd/listen.go +++ b/cmd/listen.go @@ -20,12 +20,13 @@ package cmd import ( "encoding/json" "fmt" + "os" + "os/signal" + giDevice "github.com/SonicCloudOrg/sonic-gidevice" "github.com/SonicCloudOrg/sonic-ios-bridge/src/entity" "github.com/SonicCloudOrg/sonic-ios-bridge/src/util" "github.com/spf13/cobra" - "os" - "os/signal" ) var listenCmd = &cobra.Command{ @@ -62,9 +63,10 @@ var listenCmd = &cobra.Command{ if device.Status == "online" && isDetail { detail, err1 := entity.GetDetail(d) if err1 != nil { - continue + fmt.Fprintf(os.Stderr, "%+v\n", err1) + } else { + device.DeviceDetail = *detail } - device.DeviceDetail = *detail } data := util.ResultData(device) fmt.Println(util.Format(data, isFormat, isDetail))