-
Notifications
You must be signed in to change notification settings - Fork 49
/
console.go
219 lines (188 loc) · 6.66 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"flag"
"fmt"
"github.com/giwty/switch-library-manager/db"
"github.com/giwty/switch-library-manager/process"
"github.com/giwty/switch-library-manager/settings"
"github.com/jedib0t/go-pretty/table"
"github.com/schollz/progressbar/v3"
"go.uber.org/zap"
"os"
"path"
"path/filepath"
"strings"
)
var (
nspFolder = flag.String("f", "", "path to NSP folder")
recursive = flag.Bool("r", true, "recursively scan sub folders")
mode = flag.String("m", "", "**deprecated**")
progressBar *progressbar.ProgressBar
)
type Console struct {
baseFolder string
sugarLogger *zap.SugaredLogger
}
func CreateConsole(baseFolder string, sugarLogger *zap.SugaredLogger) *Console {
return &Console{baseFolder: baseFolder, sugarLogger: sugarLogger}
}
func (c *Console) Start() {
flag.Parse()
if mode != nil && *mode != "" {
fmt.Println("note : the mode option ('-m') is deprecated, please use the settings.json to control options.")
}
settingsObj := settings.ReadSettings(c.baseFolder)
//1. load the titles JSON object
fmt.Printf("Downlading latest switch titles json file")
progressBar = progressbar.New(2)
filename := filepath.Join(c.baseFolder, settings.TITLE_JSON_FILENAME)
titleFile, titlesEtag, err := db.LoadAndUpdateFile(settings.TITLES_JSON_URL, filename, settingsObj.TitlesEtag)
if err != nil {
fmt.Printf("title json file doesn't exist\n")
return
}
settingsObj.TitlesEtag = titlesEtag
progressBar.Add(1)
//2. load the versions JSON object
filename = filepath.Join(c.baseFolder, settings.VERSIONS_JSON_FILENAME)
versionsFile, versionsEtag, err := db.LoadAndUpdateFile(settings.VERSIONS_JSON_URL, filename, settingsObj.VersionsEtag)
if err != nil {
fmt.Printf("version json file doesn't exist\n")
return
}
settingsObj.VersionsEtag = versionsEtag
progressBar.Add(1)
progressBar.Finish()
newUpdate, err := settings.CheckForUpdates()
if newUpdate {
fmt.Printf("\n=== New version available, download from Github ===\n")
}
//3. update the config file with new etag
settings.SaveSettings(settingsObj, c.baseFolder)
//4. create switch title db
titlesDB, err := db.CreateSwitchTitleDB(titleFile, versionsFile)
//5. read local files
folderToScan := settingsObj.Folder
if nspFolder != nil && *nspFolder != "" {
folderToScan = *nspFolder
}
if folderToScan == "" {
fmt.Printf("\n\nNo folder to scan was defined, please edit settings.json with the folder path\n")
return
}
fmt.Printf("\n\nScanning folder [%v]", folderToScan)
progressBar = progressbar.New(2000)
keys, _ := settings.InitSwitchKeys(c.baseFolder)
if keys == nil || keys.GetKey("header_key") == "" {
fmt.Printf("\n!!NOTE!!: keys file was not found, deep scan is disabled, library will be based on file tags.\n %v", err)
}
recursiveMode := settingsObj.ScanRecursively
if recursive != nil && *recursive != true {
recursiveMode = *recursive
}
localDbManager, err := db.NewLocalSwitchDBManager(c.baseFolder)
if err != nil {
fmt.Printf("failed to create local files db :%v\n", err)
return
}
defer localDbManager.Close()
scanFolders := settingsObj.ScanFolders
scanFolders = append(scanFolders, folderToScan)
localDB, err := localDbManager.CreateLocalSwitchFilesDB(scanFolders, c, recursiveMode, true)
if err != nil {
fmt.Printf("\nfailed to process local folder\n %v", err)
return
}
progressBar.Finish()
p := (float32(len(localDB.TitlesMap)) / float32(len(titlesDB.TitlesMap))) * 100
fmt.Printf("Local library completion status: %.2f%% (have %d titles, out of %d titles)\n", p, len(localDB.TitlesMap), len(titlesDB.TitlesMap))
c.processIssues(localDB)
if settingsObj.OrganizeOptions.DeleteOldUpdateFiles {
progressBar = progressbar.New(2000)
fmt.Printf("\nDeleting old updates\n")
process.DeleteOldUpdates(c.baseFolder, localDB, c)
progressBar.Finish()
}
if settingsObj.OrganizeOptions.RenameFiles || settingsObj.OrganizeOptions.CreateFolderPerGame {
progressBar = progressbar.New(2000)
fmt.Printf("\nStarting library organization\n")
process.OrganizeByFolders(folderToScan, localDB, titlesDB, c)
progressBar.Finish()
}
if settingsObj.CheckForMissingUpdates {
fmt.Printf("\nChecking for missing updates\n")
c.processMissingUpdates(localDB, titlesDB)
}
if settingsObj.CheckForMissingDLC {
fmt.Printf("\nChecking for missing DLC\n")
c.processMissingDLC(localDB, titlesDB)
}
fmt.Printf("Completed")
}
func (c *Console) processIssues(localDB *db.LocalSwitchFilesDB) {
if len(localDB.Skipped) != 0 {
fmt.Print("\nSkipped files:\n\n")
} else {
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(table.Row{"#", "Skipped file", "Reason"})
i := 0
for k, v := range localDB.Skipped {
t.AppendRow([]interface{}{i, path.Join(k.BaseFolder, k.FileName), v})
i++
}
t.AppendFooter(table.Row{"", "", "", "", "Total", len(localDB.Skipped)})
t.Render()
}
func (c *Console) processMissingUpdates(localDB *db.LocalSwitchFilesDB, titlesDB *db.SwitchTitlesDB) {
incompleteTitles := process.ScanForMissingUpdates(localDB.TitlesMap, titlesDB.TitlesMap)
if len(incompleteTitles) != 0 {
fmt.Print("\nFound available updates:\n\n")
} else {
fmt.Print("\nAll NSP's are up to date!\n\n")
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(table.Row{"#", "Title", "TitleId", "Local version", "Latest Version", "Update Date"})
i := 0
for _, v := range incompleteTitles {
t.AppendRow([]interface{}{i, v.Attributes.Name, v.Attributes.Id, v.LocalUpdate, v.LatestUpdate, v.LatestUpdateDate})
i++
}
t.AppendFooter(table.Row{"", "", "", "", "Total", len(incompleteTitles)})
t.Render()
}
func (c *Console) processMissingDLC(localDB *db.LocalSwitchFilesDB, titlesDB *db.SwitchTitlesDB) {
settingsObj := settings.ReadSettings(c.baseFolder)
ignoreIds := map[string]struct{}{}
for _, id := range settingsObj.IgnoreDLCTitleIds {
ignoreIds[strings.ToLower(id)] = struct{}{}
}
incompleteTitles := process.ScanForMissingDLC(localDB.TitlesMap, titlesDB.TitlesMap, ignoreIds)
if len(incompleteTitles) != 0 {
fmt.Print("\nFound missing DLCS:\n\n")
} else {
fmt.Print("\nYou have all the DLCS!\n\n")
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(table.Row{"#", "Title", "TitleId", "Missing DLCs (titleId - Name)"})
i := 0
for _, v := range incompleteTitles {
t.AppendRow([]interface{}{i, v.Attributes.Name, v.Attributes.Id, strings.Join(v.MissingDLC, "\n")})
i++
}
t.AppendFooter(table.Row{"", "", "", "", "Total", len(incompleteTitles)})
t.Render()
}
func (c *Console) UpdateProgress(curr int, total int, message string) {
progressBar.ChangeMax(total)
progressBar.Set(curr)
}