This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.go
211 lines (198 loc) · 5.76 KB
/
fix.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
//nolint:gochecknoglobals,gochecknoinits
package cmd
import (
"errors"
"os"
"github.com/Defacto2/df2/cmd/internal/arg"
"github.com/Defacto2/df2/cmd/internal/run"
"github.com/Defacto2/df2/pkg/database"
"github.com/Defacto2/df2/pkg/demozoo"
"github.com/Defacto2/df2/pkg/groups"
"github.com/Defacto2/df2/pkg/images"
"github.com/Defacto2/df2/pkg/people"
"github.com/Defacto2/df2/pkg/text"
"github.com/Defacto2/df2/pkg/zipcmmt"
"github.com/Defacto2/df2/pkg/zipcontent"
"github.com/spf13/cobra"
)
var zipc arg.ZipCmmt
// fixCmd represents the fix command.
var fixCmd = &cobra.Command{
Use: "fix",
Short: "Fixes database entries and records.",
Long: "Repair broken or invalid formatting for the database records and entries.",
Aliases: []string{"f"},
GroupID: "group1",
Run: func(cmd *cobra.Command, args []string) {
// the fix command by itself should always return help.
if len(args) == 0 {
if err := cmd.Usage(); err != nil {
logr.Fatal(err)
}
return
}
// handle any unknown commands
if err := cmd.Usage(); err != nil {
logr.Fatal(err)
}
logr.Errorf("%q subcommand for fix is an %s\n", args[0], ErrCommand)
},
}
var fixArchivesCmd = &cobra.Command{
Use: "archives",
Short: "Repair archives listing empty content.",
Long: `Records with downloads that are packaged into archives need to have
their file content added to the database. This command finds and repair
records that do not have this expected context.`,
Aliases: []string{"a"},
GroupID: "groupU",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
if err := zipcontent.Fix(db, os.Stdout, logr, confg, true); err != nil {
logr.Errorf("archives fix: %s", err)
}
},
}
var fixDatabaseCmd = &cobra.Command{
Use: "database",
Short: "Repair malformed database entries.",
Long: `Repair malformed records and entries in the database.
This includes the formatting and trimming of groups, people, platforms and sections.`,
Aliases: []string{"d", "db"},
GroupID: "groupU",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
w := os.Stdout
if err := database.Fix(db, w); err != nil {
logr.Error(err)
}
if err := groups.Fix(db, w); err != nil {
logr.Error(err)
}
if err := people.Fix(db, w); err != nil {
logr.Error(err)
}
},
}
var fixDemozooCmd = &cobra.Command{
Use: "demozoo",
Short: "Repair imported Demozoo data conflicts.",
Aliases: []string{"dz"},
GroupID: "groupU",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
if err := demozoo.Fix(db, os.Stdout); err != nil {
logr.Errorf("demozoo fix: %s", err)
}
},
}
var fixImagesCmd = &cobra.Command{
Use: "images",
Short: "Generate missing images.",
Long: `Create missing previews, thumbnails and optimised formats for records
that are raster images.`,
Aliases: []string{"i"},
GroupID: "groupG",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
if err := images.Fix(db, os.Stdout, confg); err != nil {
logr.Error(err)
}
},
}
var fixRenGroup = &cobra.Command{
Use: "rename group replacement",
Short: "Rename all instances of a group.",
Aliases: []string{"ren", "r"},
GroupID: "groupR",
Example: ` df2 fix rename "The Group" "New Group Name"`,
Run: func(cmd *cobra.Command, args []string) {
// in the future this command could be adapted to use a --person flag
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
err = run.Rename(db, os.Stdout, args...)
if errors.Is(err, run.ErrToFew) {
if err := cmd.Usage(); err != nil {
logr.Fatal(err)
}
return
}
if err != nil {
logr.Error(err)
}
},
}
var fixTextCmd = &cobra.Command{
Use: "text",
Short: "Generate missing text previews.",
Long: `Create missing previews, thumbnails and optimised formats for records
that are plain text files.`,
Aliases: []string{"t", "txt"},
GroupID: "groupG",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
if err := text.Fix(db, os.Stdout, logr, confg); err != nil {
logr.Error(err)
}
},
}
var fixZipCmmtCmd = &cobra.Command{
Use: "zip",
Short: "Extract missing comments from zip archives.",
Long: `Extract and save missing comments from zip archives.
"A comment is optional text information that is embedded in a Zip file."`,
Aliases: []string{"z"},
GroupID: "groupG",
Run: func(cmd *cobra.Command, args []string) {
db, err := database.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
if err := zipcmmt.Fix(db, os.Stdout, confg, zipc.Unicode, zipc.OW, zipc.Stdout); err != nil {
logr.Error(err)
}
},
}
func init() {
rootCmd.AddCommand(fixCmd)
fixCmd.AddGroup(&cobra.Group{ID: "groupU", Title: "Repair:"})
fixCmd.AddGroup(&cobra.Group{ID: "groupG", Title: "Create:"})
fixCmd.AddGroup(&cobra.Group{ID: "groupR", Title: "Update:"})
fixCmd.AddCommand(fixArchivesCmd)
fixCmd.AddCommand(fixDatabaseCmd)
fixCmd.AddCommand(fixDemozooCmd)
fixCmd.AddCommand(fixImagesCmd)
fixCmd.AddCommand(fixRenGroup)
fixCmd.AddCommand(fixTextCmd)
fixCmd.AddCommand(fixZipCmmtCmd)
fixZipCmmtCmd.PersistentFlags().BoolVarP(&zipc.Stdout, "print", "p", false,
"also print saved comments to the stdout")
fixZipCmmtCmd.PersistentFlags().BoolVarP(&zipc.Unicode, "unicode", "u", false,
"also convert saved comments into Unicode and print to the stdout")
fixZipCmmtCmd.PersistentFlags().BoolVarP(&zipc.OW, "overwrite", "o", false,
"overwrite all existing saved comments")
}