forked from MrMilenko/Pinecone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
133 lines (111 loc) · 3.5 KB
/
cli.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
package main
import (
"fmt"
"log"
"strings"
"github.com/fatih/color"
)
const (
headerWidth = 100
separator = ""
)
type CLIOptions struct {
DataFolder string
JSONFilePath string
JSONUrl string
}
func printHeader(title string) {
title = strings.TrimSpace(title)
if len(title) > headerWidth-6 { // -6 to account for spaces and equals signs
title = title[:headerWidth-9] + "..."
}
formattedTitle := "== " + title + " =="
padLen := (headerWidth - len(formattedTitle)) / 2
color.New(color.FgCyan).Println(strings.Repeat("=", padLen) + formattedTitle + strings.Repeat("=", headerWidth-padLen-len(formattedTitle)))
}
func printInfo(colorCode color.Attribute, format string, args ...interface{}) {
color.New(colorCode).Printf(" "+format, args...)
}
// Prints statistics for a specific title or for all titles if batch is true.
func printStats(titleID string, batch bool) {
if batch {
printTotalStats()
} else {
data, ok := titles.Titles[titleID]
if !ok {
fmt.Printf("No data found for title ID %s\n", titleID)
return
}
fmt.Printf("Statistics for title ID %s:\n", titleID)
printTitleStats(&data)
}
}
// Prints statistics for TitleData.
func printTitleStats(data *TitleData) {
fmt.Println("Title:", data.TitleName)
fmt.Println("Total number of Content IDs:", len(data.ContentIDs))
fmt.Println("Total number of Title Updates:", len(data.TitleUpdates))
fmt.Println("Total number of Known Title Updates:", len(data.TitleUpdatesKnown))
fmt.Println("Total number of Archived items:", len(data.Archived))
fmt.Println()
}
func printTotalStats() {
totalTitles := len(titles.Titles)
totalContentIDs := 0
totalTitleUpdates := 0
totalKnownTitleUpdates := 0
totalArchivedItems := 0
// Set to store unique hashes of known title updates and archived items
knownTitleUpdateHashes := make(map[string]struct{})
archivedItemHashes := make(map[string]struct{})
for _, data := range titles.Titles {
totalContentIDs += len(data.ContentIDs)
totalTitleUpdates += len(data.TitleUpdates)
// Count unique known title updates
for _, knownUpdate := range data.TitleUpdatesKnown {
for hash := range knownUpdate {
knownTitleUpdateHashes[hash] = struct{}{}
}
}
// Count unique archived items
for _, archivedItem := range data.Archived {
for hash := range archivedItem {
archivedItemHashes[hash] = struct{}{}
}
}
}
totalKnownTitleUpdates = len(knownTitleUpdateHashes)
totalArchivedItems = len(archivedItemHashes)
fmt.Println("Total Titles:", totalTitles)
fmt.Println("Total Content IDs:", totalContentIDs)
fmt.Println("Total Title Updates:", totalTitleUpdates)
fmt.Println("Total Known Title Updates:", totalKnownTitleUpdates)
fmt.Println("Total Archived Items:", totalArchivedItems)
}
func cliPromptForDownload(url string) bool {
var response string
fmt.Printf("The required JSON data is not found. It can be downloaded from %s\n", url)
fmt.Print("Do you want to download it now? (yes/no): ")
fmt.Scanln(&response)
return strings.ToLower(response) == "yes"
}
func startCLI(options CLIOptions) {
err := checkDataFolder(options.DataFolder)
if err != nil {
log.Fatalln(err)
}
err = checkDatabaseFile(options.JSONFilePath, options.JSONUrl, updateFlag)
if err != nil {
log.Fatalln(err)
}
err = checkDumpFolder(dumpLocation)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Pinecone v%s\n", version)
fmt.Println("Please share output of this program with the Pinecone team if you find anything interesting!")
err = checkParsingSettings()
if err != nil {
log.Fatalln(err)
}
}