forked from g0dsCookie/gopbo
/
unpack.go
67 lines (55 loc) · 1.71 KB
/
unpack.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
package cmd
import (
"context"
"flag"
"fmt"
"os"
"github.com/Dev-Time/gopbo/pbo"
"github.com/google/subcommands"
)
// UnpackCmd is used to unpack pbo files into directories.
type UnpackCmd struct {
verbose bool
deleteAfter bool
destination string
}
// Name returns the name of this command.
func (*UnpackCmd) Name() string { return "unpack" }
// Synopsis returns the synopsis of this command.
func (*UnpackCmd) Synopsis() string { return "Unpacks a PBO" }
// Usage returns the usage of this command.
func (*UnpackCmd) Usage() string {
return `unpack [-verbose] [-delete] [-destination <dir>] <pbo>
`
}
// SetFlags registers all flags for this command.
func (u *UnpackCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&u.verbose, "verbose", false, "be verbose")
f.BoolVar(&u.deleteAfter, "delete", false, "delete pbo after successful unpacking")
f.StringVar(&u.destination, "destination", "", "set destination where to unpack the pbo. Defaults to <pbo> without .pbo extension")
}
// Execute parses and executes the input.
func (u *UnpackCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
path := f.Arg(0)
if path == "" {
fmt.Printf("Please provide the pbo to unpack.\n\n")
return subcommands.ExitUsageError
}
if u.destination == "" {
u.destination = path[:len(path)-4]
}
if err := pbo.Unpack(path, u.destination, u.verbose); err != nil {
fmt.Printf("An error occurred while unpacking PBO: %v\n", err)
return subcommands.ExitFailure
}
if u.deleteAfter {
if err := os.Remove(path); err != nil {
fmt.Printf("Could not remove PBO: %v\n", err)
return subcommands.ExitFailure
}
if u.verbose {
fmt.Printf("%s deleted\n", path)
}
}
return subcommands.ExitSuccess
}