Skip to content

Commit

Permalink
add filter and filterValue flags for more advanced and flexible filte…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
cj123 committed Aug 19, 2018
1 parent cfd3e38 commit 9fc0762
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Usage of ./allthefirmwares:
-d string
the location to save/check IPSW files.
Can include templates e.g. {{.Identifier}} or {{.BuildID}} (default "./")
-filter string
filter by a specific struct field
-filterValue string
the value to filter by (used with -filter)
-i string
only download for the specified device
-l only download the latest firmware for the specified devices
Expand Down
38 changes: 38 additions & 0 deletions allthefirmwares.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/signal"
"path/filepath"
"reflect"
"sort"
"text/template"

Expand All @@ -25,6 +26,8 @@ import (
var (
ipswClient = api.NewIPSWClient("https://api.ipsw.me/v4", nil)

filter, filterValue string

// flags
verifyIntegrity, reDownloadOnVerificationFailed, downloadSigned, downloadLatest bool
downloadDirectoryTemplate, specifiedDevice string
Expand All @@ -41,6 +44,8 @@ func init() {
flag.BoolVar(&downloadSigned, "s", false, "only download signed firmwares")
flag.StringVar(&downloadDirectoryTemplate, "d", "./", "the location to save/check IPSW files.\n\tCan include templates e.g. {{.Identifier}} or {{.BuildID}}")
flag.StringVar(&specifiedDevice, "i", "", "only download for the specified device")
flag.StringVar(&filter, "filter", "", "filter by a specific struct field")
flag.StringVar(&filterValue, "filterValue", "", "the value to filter by (used with -filter)")
flag.Parse()
}

Expand Down Expand Up @@ -91,6 +96,10 @@ func main() {
continue
}

if filter != "" && filterValue != "" && !passesFilter(ipsw, filter, filterValue) {
continue
}

directory, err := parseDownloadDirectory(&ipsw, device.Identifier)

if err != nil {
Expand Down Expand Up @@ -302,3 +311,32 @@ func download(url string, location string, writer io.Writer, callback func(n, do

return hex.EncodeToString(h.Sum(nil)), err
}

func passesFilter(firmware api.Firmware, filterName, filterValue string) bool {
field := reflect.Indirect(reflect.ValueOf(firmware)).FieldByName(filterName)

str := ""

switch t := field.Interface().(type) {
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
str = fmt.Sprintf("%d", t)

case string:
str = t

case fmt.Stringer:
str = t.String()

case bool:
if t {
str = "true"
} else {
str = "false"
}

default:
return false
}

return filterValue == str
}

0 comments on commit 9fc0762

Please sign in to comment.