This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
/
pack.go
71 lines (59 loc) · 1.83 KB
/
pack.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
package cmd
import (
"context"
"flag"
"fmt"
"os"
"github.com/g0dsCookie/gopbo/pbo"
"github.com/google/subcommands"
)
// PackCmd will pack any directory into a pbo file.
type PackCmd struct {
verbose bool
deleteAfter bool
destination string
}
// Name returns the name of this command.
func (*PackCmd) Name() string { return "pack" }
// Synopsis returns the synopsis of this command.
func (*PackCmd) Synopsis() string { return "Packs a directory into a PBO" }
// Usage returns the usage of this command.
func (*PackCmd) Usage() string {
return `pack [-verbose] [-delete] [-destination <pbo>] <directory>
`
}
// SetFlags registers all flags for this command.
func (u *PackCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&u.verbose, "verbose", false, "be verbose")
f.BoolVar(&u.deleteAfter, "delete", false, "delete directory after successful packing")
f.StringVar(&u.destination, "destination", "", "set destination where to save the PBO. Defaults to <directory>.pbo")
}
// Execute parses and executes the input.
func (u *PackCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
path := f.Arg(0)
if path == "" {
fmt.Printf("Please provide the directory to pack.\n\n")
return subcommands.ExitUsageError
}
if u.destination == "" {
if path[len(path)-1] == '/' || path[len(path)-1] == '\\' {
u.destination = path[:len(path)-1] + ".pbo"
} else {
u.destination = path + ".pbo"
}
}
if err := pbo.Pack(path, u.destination, u.verbose); err != nil {
fmt.Printf("An error occurred while packing PBO: %v\n", err)
return subcommands.ExitFailure
}
if u.deleteAfter {
if err := os.RemoveAll(path); err != nil {
fmt.Printf("Could not remove directory: %v\n", err)
return subcommands.ExitFailure
}
if u.verbose {
fmt.Printf("%s deleted\n", path)
}
}
return subcommands.ExitSuccess
}