-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
73 lines (61 loc) · 1.19 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
// Copyright (C) 2016 - Will Glozer. All rights reserved.
package main
import (
"fmt"
"os"
"github.com/wg/arc/archive"
)
type Cmd struct {
Op interface{}
Archiver Archiver
Verbose int
Names []string
Private *KeyContainer
Public *KeyContainer
}
func main() {
c, err := NewCommand()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch op := c.Op.(type) {
case func(*archive.Writer, ...string) error:
arc := c.createArchive()
err = op(arc.Writer, c.Names...)
defer arc.Close()
case func(*RegexFilter) error:
arc, filter := c.filterArchive()
err = op(filter)
defer arc.Close()
case func(*KeyContainer, *KeyContainer) error:
err = op(c.Public, c.Private)
defer c.Public.Close()
defer c.Private.Close()
}
if err != nil {
c.Fatal(err)
}
}
func (c *Cmd) createArchive() *Writer {
arc, err := c.Archiver.Writer()
if err != nil {
c.Fatal(err)
}
return arc
}
func (c *Cmd) filterArchive() (*Reader, *RegexFilter) {
arc, err := c.Archiver.Reader()
if err != nil {
c.Fatal(err)
}
f, err := NewRegexFilter(arc.Reader, c.Names...)
if err != nil {
c.Fatal(err)
}
return arc, f
}
func (c *Cmd) Fatal(v ...interface{}) {
fmt.Println(v...)
os.Exit(1)
}