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
/
clean.go
60 lines (55 loc) · 1.72 KB
/
clean.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
//nolint:gochecknoglobals,gochecknoinits
package cmd
import (
"os"
"github.com/Defacto2/df2/cmd/internal/arg"
"github.com/Defacto2/df2/pkg/assets"
"github.com/Defacto2/df2/pkg/database/msql"
"github.com/Defacto2/df2/pkg/directories"
"github.com/spf13/cobra"
)
var clean arg.Clean
// cleanCmd represents the clean command.
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Discover or clean orphan files.",
Long: `Discover or clean orphan files found on the web server.
Files are considered orphan when they do not match to a correlating record in the
database. These can include UUID named thumbnails, previews, textfile previews.`,
Aliases: []string{"c"},
GroupID: "group2",
Run: func(cmd *cobra.Command, args []string) {
if _, err := directories.Init(confg, clean.MakeDirs); err != nil {
logr.Fatal(err)
}
db, err := msql.Connect(confg)
if err != nil {
logr.Fatal(err)
}
defer db.Close()
c := assets.Clean{
Name: clean.Target,
Remove: clean.Delete,
Human: clean.Humanise,
Config: confg,
}
if err := c.Walk(db, os.Stdout); err != nil {
logr.Error(err)
}
},
}
func init() {
rootCmd.AddCommand(cleanCmd)
cleanCmd.Flags().StringVarP(&clean.Target, "target", "t", "all",
"which file section to clean"+arg.CleanOpts(arg.Targets()...))
cleanCmd.Flags().BoolVarP(&clean.Delete, "delete", "x", false,
"erase all discovered files to free up drive space")
cleanCmd.Flags().BoolVar(&clean.Humanise, "humanise", true,
"humanise file sizes and date times")
cleanCmd.Flags().BoolVar(&clean.MakeDirs, "makedirs", false,
"generate uuid directories and placeholder files")
cleanCmd.Flags().SortFlags = false
if err := cleanCmd.Flags().MarkHidden("makedirs"); err != nil {
logr.Error(err)
}
}