-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (50 loc) · 1.37 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
// © 2014 Brad Ackerman. Licensed under the WTFPL.
//
//
package main
import (
"io"
"log"
"os"
"github.com/backerman/dfworld/pkg/savefile"
"github.com/spf13/cobra"
)
var inFilename string
var outFilename string
func main() {
var rootCmd = &cobra.Command{
Use: "dfworld",
Short: "dfworld is tar for Dwarf Fortress save files",
Long: "dfworld is tar for Dwarf Fortress save files.",
}
rootCmd.PersistentFlags().StringVarP(&inFilename, "in", "f", "world.sav", "Specify the save file to use")
var decompressCmd = &cobra.Command{
Use: "decompress",
Short: "Decompress a compressed save file",
Run: decompress,
}
decompressCmd.Flags().StringVarP(&outFilename, "out", "o", "world-out.sav", "Specify the new save file to write to")
rootCmd.AddCommand(decompressCmd)
rootCmd.Execute()
}
func decompress(cmd *cobra.Command, args []string) {
in, err := savefile.NewFileFromPath(inFilename)
if err != nil {
log.Fatalf("Unable to open input file: %v", err)
}
if in.Header().IsCompressed != 1 {
log.Fatalf("Input file %v is already compressed; exiting.", inFilename)
}
out, err := os.Create(outFilename)
if err != nil {
log.Fatalf("Unable to open output file: %v", err)
}
inReader, err := in.DecompressedReader()
if err != nil {
log.Fatalf("Unable to open output file: %v", err)
}
io.Copy(out, inReader)
out.Close()
inReader.Close()
in.Close()
}