fix: adb device parse attributes error

This commit is contained in:
余泓铮
2024-03-18 15:07:47 +08:00
parent cc4944b133
commit bea0a6a273
2 changed files with 37 additions and 11 deletions

View File

@@ -41,7 +41,11 @@ var listAndroidDevicesCmd = &cobra.Command{
if isDetail {
fmt.Println(format(d.DeviceInfo()))
} else {
fmt.Println(d.Serial(), d.Usb())
if usb, err := d.Usb(); err != nil {
fmt.Println(d.Serial())
} else {
fmt.Println(d.Serial(), usb)
}
}
}
return nil

View File

@@ -107,20 +107,37 @@ func (d *Device) features() (features Features, err error) {
return features, nil
}
func (d *Device) Product() string {
return d.attrs["product"]
func (d Device) HasAttribute(key string) bool {
_, ok := d.attrs[key]
return ok
}
func (d *Device) Model() string {
return d.attrs["model"]
func (d Device) Product() (string, error) {
if d.HasAttribute("product") {
return d.attrs["product"], nil
}
return "", errors.New("does not have attribute: product")
}
func (d *Device) Usb() string {
return d.attrs["usb"]
func (d Device) Model() (string, error) {
if d.HasAttribute("model") {
return d.attrs["model"], nil
}
return "", errors.New("does not have attribute: model")
}
func (d *Device) transportId() string {
return d.attrs["transport_id"]
func (d *Device) Usb() (string, error) {
if d.HasAttribute("usb") {
return d.attrs["usb"], nil
}
return "", errors.New("does not have attribute: usb")
}
func (d Device) transportId() (string, error) {
if d.HasAttribute("transport_id") {
return d.attrs["transport_id"], nil
}
return "", errors.New("does not have attribute: transport_id")
}
func (d *Device) DeviceInfo() map[string]string {
@@ -132,8 +149,13 @@ func (d *Device) Serial() string {
return d.serial
}
func (d *Device) IsUsb() bool {
return d.Usb() != ""
func (d *Device) IsUsb() (bool, error) {
usb, err := d.Usb()
if err != nil {
return false, err
}
return usb != "", nil
}
func (d *Device) State() (DeviceState, error) {