-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrongbox.go
More file actions
96 lines (81 loc) · 1.87 KB
/
strongbox.go
File metadata and controls
96 lines (81 loc) · 1.87 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strings"
"syscall"
"golang.org/x/term"
config "strongbox/configuration"
"strongbox/control"
log "github.com/sirupsen/logrus"
)
func credentials() (string, error) {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
password := string(bytePassword)
return strings.TrimSpace(password), nil
}
func getDefaultConfigPath() string {
usr, err := user.Current()
if err != nil {
log.Warn("Failed to get current user:", err)
return "config.yml"
}
configDir := filepath.Join(usr.HomeDir, ".strongbox")
err = os.MkdirAll(configDir, 0755)
if err != nil {
log.Warn("Failed to create config directory:", err)
return "config.yml"
}
return filepath.Join(configDir, "config.yml")
}
func canRunUI() bool {
switch runtime.GOOS {
case "darwin":
return os.Getenv("DISPLAY") != "" || os.Getenv("APP_NAME") != "" || os.Getenv("HOME") != ""
case "linux":
return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != ""
case "windows":
return true
default:
return false
}
}
func main() {
defaultConfigPath := getDefaultConfigPath()
runAsUI := flag.Bool("ui", canRunUI(), "run with ui.")
configFile := flag.String("c", defaultConfigPath, "config file.")
flag.Parse()
err := config.Cfg.Load(*configFile)
if err != nil {
log.Fatal("read config file error:", err)
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
control.GetControl().Unmount()
}()
if *runAsUI {
control.RunStrongBoxApp()
return
}
passwd, err := credentials()
if passwd == "" || err != nil {
log.Fatal("must set password")
}
config.Cfg.SetPasswd(passwd)
err = control.GetControl().Mount()
if err != nil {
log.Fatal("Mount failed:", err)
}
control.GetControl().Wait()
}