Skip to content

Commit

Permalink
Added support for recursive scan using "-r"
Browse files Browse the repository at this point in the history
  • Loading branch information
giwty authored and giwty committed Jan 17, 2020
1 parent 0546629 commit 2bf0af3
Showing 1 changed file with 54 additions and 41 deletions.
95 changes: 54 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
Expand All @@ -25,6 +26,9 @@ const (

var (
nspFolder = flag.String("f", "", "path to NSP folder")
recursive = flag.Bool("r", false, "recursively scan sub folders")
versionR = regexp.MustCompile(`\[[vV]?(?P<version>[0-9]{1,10})\]`)
titleIdR = regexp.MustCompile(`\[(?P<titleId>[A-Z,a-z,0-9]{16})\]`)
s = spinner.New(spinner.CharSets[26], 100*time.Millisecond)
)

Expand Down Expand Up @@ -116,47 +120,7 @@ func main() {
var localVersionsDb = map[string][]int{}
var skippedFiles = map[string]string{}

versionR := regexp.MustCompile(`\[[vV]?(?P<version>[0-9]{1,10})\]`)
titleIdR := regexp.MustCompile(`\[(?P<titleId>[A-Z,a-z,0-9]{16})\]`)
for _, file := range files {
if file.Name()[0:1] == "." || file.IsDir() {
continue
}

if !strings.HasSuffix(file.Name(), "nsp") && !strings.HasSuffix(file.Name(), "nsz") {
skippedFiles[file.Name()] = "non NSP file"
continue
}

res := versionR.FindStringSubmatch(file.Name())
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
verStr := res[1]
res = titleIdR.FindStringSubmatch(file.Name())
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
titleId := strings.ToLower(res[1])

if strings.HasSuffix(titleId, "800") {
titleId = titleId[0:len(titleId)-3] + "000"
}

ver, err := strconv.Atoi(verStr)
if err != nil {
skippedFiles[file.Name()] = "failed to parse version"
continue
}

localVersionsDb[titleId] = append(localVersionsDb[titleId], ver)
}
scanLocalFiles(*nspFolder, files, *recursive, localVersionsDb, skippedFiles)

var numTobeUpdated int = 0
type Dlcs struct {
Expand Down Expand Up @@ -262,6 +226,55 @@ func main() {
t.Render()
}

func scanLocalFiles(path string, files []os.FileInfo, recurise bool, localVersionsDb map[string][]int, skippedFiles map[string]string) {

for _, file := range files {
if file.Name()[0:1] == "." || file.IsDir() {
folder := filepath.Join(path, file.Name())
innerFiles, err := ioutil.ReadDir(folder)
if err != nil {
fmt.Printf("\nfailed scanning NSP folder\n %v", err)
continue
}
scanLocalFiles(folder, innerFiles, recurise, localVersionsDb, skippedFiles)
}

if !strings.HasSuffix(file.Name(), "nsp") && !strings.HasSuffix(file.Name(), "nsz") {
skippedFiles[file.Name()] = "non NSP file"
continue
}

res := versionR.FindStringSubmatch(file.Name())
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
verStr := res[1]
res = titleIdR.FindStringSubmatch(file.Name())
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
if len(res) != 2 {
skippedFiles[file.Name()] = "failed to parse name"
continue
}
titleId := strings.ToLower(res[1])

if strings.HasSuffix(titleId, "800") {
titleId = titleId[0:len(titleId)-3] + "000"
}

ver, err := strconv.Atoi(verStr)
if err != nil {
skippedFiles[file.Name()] = "failed to parse version"
continue
}

localVersionsDb[titleId] = append(localVersionsDb[titleId], ver)
}
}

func loadOrDownloadFileFromUrl(url string, fileName string, etag string, target interface{}) (string, error) {

req, err := http.NewRequest("GET", url, nil)
Expand Down

0 comments on commit 2bf0af3

Please sign in to comment.