/
main.go
61 lines (52 loc) · 1.07 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/user"
"path/filepath"
"github.com/nymsio/nyms-agent/keymgr"
gl "github.com/op/go-logging"
)
var pipe bool
var protoDebug bool
func init() {
flag.BoolVar(&pipe, "pipe", false, "Run RPC service on stdin/stdout")
flag.BoolVar(&protoDebug, "debug", false, "Log RPC traffic")
flag.Parse()
}
func main() {
createLogger()
if pipe {
keymgr.LoadDefaultKeyring()
runPipeServer(protoDebug)
return
}
}
const defaultLogPath = ".nyms/log"
func createLogger() {
f, err := openLogFile()
if err != nil {
return
}
be := gl.NewLogBackend(f, "", log.Ltime)
gl.SetBackend(be)
}
func openLogFile() (io.Writer, error) {
logPath := getLogPath()
dirPath := filepath.Dir(logPath)
err := os.MkdirAll(dirPath, 0711)
if err != nil {
return nil, err
}
return os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
}
func getLogPath() string {
u, err := user.Current()
if err != nil {
panic(fmt.Sprintf("Failed to get current user information: %v", err))
}
return filepath.Join(u.HomeDir, defaultLogPath)
}