Skip to content

Commit

Permalink
feat: support listen
Browse files Browse the repository at this point in the history
  • Loading branch information
electricbubble committed Jun 15, 2021
1 parent d80c0e6 commit 4645556
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ https://github.com/electricbubble/gidevice-cli/releases

```shell
$ gidevice list
$ gidevice listen
```

#### DeveloperDiskImage
Expand Down
49 changes: 49 additions & 0 deletions cmd/listen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"fmt"
giDevice "github.com/electricbubble/gidevice"
"github.com/electricbubble/gidevice-cli/internal"
"os"
"os/signal"

"github.com/spf13/cobra"
)

// listenCmd represents the listen command
var listenCmd = &cobra.Command{
Use: "listen",
Short: "Monitor the connection status of the device",
Run: func(cmd *cobra.Command, args []string) {
usbmux, err := giDevice.NewUsbmux()
internal.ErrorExit(err)

devNotifier := make(chan giDevice.Device)
cancel, err := usbmux.Listen(devNotifier)
internal.ErrorExit(err)

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, os.Kill)

for {
select {
case <-done:
cancel()
return
case dev := <-devNotifier:
if dev.Properties().ConnectionType != "" {
fmt.Printf("%-8s %d %-4s %-40s %d %d\n", "Attached", dev.Properties().DeviceID,
dev.Properties().ConnectionType, dev.Properties().SerialNumber, dev.Properties().ProductID, dev.Properties().LocationID,
)
} else {
fmt.Printf("%-8s %d\n", "Removed", dev.Properties().DeviceID)
}
}
}

},
}

func init() {
rootCmd.AddCommand(listenCmd)
}

0 comments on commit 4645556

Please sign in to comment.