-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoptions.go
59 lines (50 loc) · 1.06 KB
/
options.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
package certgrep
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
"go.uber.org/zap"
)
type Option func(*Extractor) error
func Logger(logger *zap.SugaredLogger) Option {
return func(e *Extractor) (err error) {
e.logger = logger.Named("certgrep")
return
}
}
func OutputDir(dir string) Option {
// TODO(jca): check env SUDO_UID|GID and reset?
return func(e *Extractor) (err error) {
dir, err = filepath.Abs(dir)
if err != nil {
return
}
now := strings.Replace(time.Now().UTC().Format(time.RFC3339), ":", "_", -1)
e.outputOptions.dir = path.Join(dir, now)
return os.MkdirAll(e.outputOptions.dir, defaultDirPerm)
}
}
func LogToStdout(do bool) Option {
return func(e *Extractor) (err error) {
e.logToStdout = do
return nil
}
}
func EnableOutputFormat(format string, do bool) Option {
return func(e *Extractor) (err error) {
switch format {
case "json":
e.outputOptions.json = do
case "der":
e.outputOptions.der = do
case "pem":
e.outputOptions.pem = do
default:
return fmt.Errorf("invalid format")
}
return
}
}