-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (61 loc) · 1.89 KB
/
main.go
File metadata and controls
76 lines (61 loc) · 1.89 KB
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
package main
import (
"flag"
"fmt"
"github.com/raspi/megafile/lib"
"os"
"path"
)
var (
// These are set with Makefile -X=main.VERSION, etc
VERSION = `v0.0.0`
BUILD = `dev`
BUILDDATE = `0000-00-00T00:00:00+00:00`
)
func main() {
// Command line arguments
extractionDirectory := flag.String(`d`, `extracted`, `Extraction directory`)
extractFilesArg := flag.Bool(`x`, false, `Extract file(s) from archive`)
showVersionArg := flag.Bool(`V`, false, `Show version`)
flag.Usage = func() {
f := os.Args[0]
_, _ = fmt.Fprintf(os.Stdout, `Extract MegaFile (.MEG) file(s)`+"\n")
_, _ = fmt.Fprintf(os.Stdout, "\n")
_, _ = fmt.Fprintf(os.Stdout, "Parameters:\n")
flag.VisitAll(func(f *flag.Flag) {
_, _ = fmt.Fprintf(os.Stdout, " -%s %s default: %q\n", f.Name, f.Usage, f.DefValue)
})
_, _ = fmt.Fprintf(os.Stdout, "\n")
_, _ = fmt.Fprintf(os.Stdout, `List file(s):`+"\n")
_, _ = fmt.Fprintf(os.Stdout, ` %s <file.meg>`+"\n", f)
_, _ = fmt.Fprintf(os.Stdout, `Extract file(s):`+"\n")
_, _ = fmt.Fprintf(os.Stdout, ` %s -x <file.meg>`+"\n", f)
_, _ = fmt.Fprintf(os.Stdout, `Example:`+"\n")
_, _ = fmt.Fprintf(os.Stdout, ` %s -x MUSIC.MEG`+"\n", f)
}
flag.Parse()
if flag.NArg() == 0 {
_, _ = fmt.Fprintf(os.Stdout, `See --help`)
os.Exit(1)
}
if *showVersionArg {
// Show version information
_, _ = fmt.Fprintf(os.Stdout, `Version %s build %s built on %s`+"\n", VERSION, BUILD, BUILDDATE)
os.Exit(0)
}
// Read file
mf := lib.New(flag.Args()[flag.NArg()-1])
files := mf.ListFiles()
fcount := len(files)
for i, f := range files {
fmt.Printf(`File #%03d/%03d (idx:%03d): %s %d bytes at offset %d`+"\n", i+1, fcount, f.Index+1, path.Join(f.Path, f.Name), f.Length, f.StartOffset)
if *extractFilesArg {
savedAs, err := mf.Extract(f, *extractionDirectory)
if err != nil {
panic(err)
}
fmt.Printf(`Saved as %q`+"\n", savedAs)
}
}
mf.Close()
}