-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
99 lines (82 loc) · 2.71 KB
/
main.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
//nolint:forbidigo
package main
import (
"fmt"
"log"
"os"
"github.com/Unpackerr/xt/pkg/xt"
flag "github.com/spf13/pflag"
"golift.io/version"
"golift.io/xtractr"
)
func parseFlags(pwd string) (*xt.Job, *flags) {
flag.Usage = func() {
// XXX: Write more "help" info here.
fmt.Println("If you pass a directory, this app will extract every archive in it.")
fmt.Printf("Usage: %s [-v] [--output <path>] <path> [paths...]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
job := &xt.Job{}
flags := &flags{}
flag.BoolVarP(&flags.PrintVer, "version", "v", false, "Print application version and exit")
// These cli options create 1 job. Using job files creates N jobs.
flag.StringVarP(&job.Output, "output", "o", pwd, "Output directory, default is current directory")
flag.UintVarP(&job.MaxDepth, "max-depth", "d", 0, "Maximum folder depth to recursively search for archives.")
flag.UintVarP(&job.MinDepth, "min-depth", "m", 0, "Minimum folder depth to recursively search for archives.")
flag.StringSliceVarP(&job.Include, "extension", "e", nil, "Only extract files with these extensions.")
flag.StringSliceVarP(&job.Passwords, "password", "P", nil, "Attempt these passwords for rar and 7zip archives.")
flag.StringSliceVarP(&flags.JobFiles, "job-file", "j", nil, "Read additional extraction jobs from these files.")
flag.Parse()
// Preserve paths?
// flag.BoolVarP(&job.Preserve, "preserve-paths", "", false, "Recreate directory hierarchy while extracting.")
// flag.UintVarP(&job.Recurse, "recurse", "r", 0, "Extract archives inside archives, up to this depth.")
job.Paths = flag.Args()
return job, flags
}
// flags contains the non-job flags used on the cli.
type flags struct {
PrintVer bool
JobFiles []string
}
func main() {
// Where we extract to.
pwd, err := os.Getwd()
if err != nil {
pwd = "."
}
// Get 1 job and other flag info from cli args.
cliJob, flags := parseFlags(pwd)
flags.printVer()
// Read in jobs from 1 or more job files.
jobs, err := xt.ParseJobs(flags.JobFiles)
if err != nil {
log.Fatal("[ERROR]", err)
}
// Append cli job to job file jobs.
if len(cliJob.Paths) > 0 {
jobs = append(jobs, cliJob)
}
// Check for jobs?
if len(jobs) < 1 || len(jobs[0].Paths) < 1 {
flag.Usage()
}
// Extract the jobs.
for i, job := range jobs {
log.Printf("Starting Job %d of %d with %s", i+1, len(jobs), job)
xt.Extract(job)
}
}
// printVer prints the version and exits.
// Only if the user passed -v or --version.
func (f *flags) printVer() {
if !f.PrintVer {
return
}
fmt.Printf("xt v%s-%s (%s)\n", version.Version, version.Revision, version.Branch)
fmt.Println(" - Supported Extensions:")
for _, ext := range xtractr.SupportedExtensions() {
fmt.Println(" ", ext)
}
os.Exit(0)
}