-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (133 loc) · 3.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"os"
"os/exec"
"path"
"strings"
"syscall"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"github.com/walkert/stash/client"
"github.com/walkert/stash/server"
)
const (
certName = ".stash.cert.pem"
keyName = ".stash.key.pem"
confName = ".stash"
)
var (
auth string
certFile string
clientAuth string
configFile string
expiration int
host string
keyFile string
port int
verbose bool
)
func setConfig() {
dir, _ := homedir.Dir()
configFile = path.Join(dir, confName)
if certFile == "" {
certFile = path.Join(dir, certName)
}
if keyFile == "" {
keyFile = path.Join(dir, keyName)
}
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
DisableLevelTruncation: true,
},
)
}
func obscure(s string) {
if !isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Println(s)
return
}
fg, _ := exec.Command("tput", "setaf", "7").Output()
bg, _ := exec.Command("tput", "setab", "7").Output()
reset, _ := exec.Command("tput", "sgr", "0").Output()
fmt.Printf("%s%s%s%s\n", string(fg), string(bg), s, string(reset))
}
func main() {
asClient := flag.Bool("client", true, "run in client mode")
daemon := flag.Bool("daemon", false, "run the server as a daemon")
asServer := flag.Bool("server", false, "run in server mode")
flag.StringVar(&certFile, "cert-file", "", "the TLS certificate file to use")
flag.IntVar(&expiration, "expiration", 12, "The amount of time in `hours` after which the stash should expire")
get := flag.Bool("get", false, "get data")
help := flag.Bool("help", false, "show help")
flag.StringVar(&host, "host", "localhost", "the hostname to listen on")
flag.StringVar(&keyFile, "key-file", "", "the TLS key file to use")
flag.IntVar(&port, "port", 2002, "The daemon will listen on this local port")
set := flag.Bool("set", false, "set the password")
validate := flag.Bool("validate", false, "check whether a password is currently set")
flag.BoolVar(&verbose, "verbose", false, "enable debugging")
flag.Parse()
setConfig()
prog := path.Base(os.Args[0])
if *help {
flag.Usage()
os.Exit(0)
}
if *asServer {
if *daemon {
binary, _ := exec.LookPath(os.Args[0])
args := []string{binary}
for _, arg := range os.Args[1:] {
if arg == "--daemon" {
continue
}
args = append(args, arg)
}
cmdEnv := os.Environ()
pid, err := syscall.ForkExec(binary, args, &syscall.ProcAttr{Env: cmdEnv})
if err != nil {
log.Fatalf("ERROR: unable to start %s in daemon mode: %v\n", prog, err)
}
fmt.Printf("Started %s in daemon mode with pid %d\n", prog, pid)
os.Exit(0)
}
s, err := server.New(host, port, certFile, keyFile, expiration)
if err != nil {
log.Fatalf("Can't start server: %v\n", err)
}
s.Start()
}
if *asClient {
c, err := client.New(port, configFile, certFile)
if err != nil {
log.Fatalf("ERROR: %v\n", err)
}
if *get {
out, err := c.GetPassword()
if err != nil {
if *validate {
if strings.Contains(err.Error(), "not set") {
fmt.Println("Password not set")
os.Exit(99)
}
}
log.Fatalf("ERROR: %v\n", err)
}
obscure(out)
}
if *set {
err := c.SetPassword()
if err != nil {
log.Fatalf("ERROR: %v\n", err)
}
}
}
}