passgo/cmd/passgo/main.go

77 lines
1.1 KiB
Go
Raw Normal View History

2019-09-04 22:19:39 -04:00
package main
import (
"fmt"
"os"
"path"
"strings"
2019-09-05 06:41:39 -04:00
"git.wow.st/gmp/passgo"
2019-09-04 22:19:39 -04:00
)
func usage() {
2019-09-05 06:41:39 -04:00
fmt.Println("passgo [-c|--clip] [name]")
2019-09-04 22:19:39 -04:00
os.Exit(-1)
}
type options struct {
clip bool
}
2019-09-05 06:41:39 -04:00
func parse(args []string) ([]string, options) {
ret := make([]string, 0)
2019-09-04 22:19:39 -04:00
opts := options{}
2019-09-05 06:41:39 -04:00
for _, a := range args {
2019-09-04 22:19:39 -04:00
switch {
2019-09-05 06:41:39 -04:00
case a == "-c", a == "--clip":
2019-09-04 22:19:39 -04:00
opts.clip = true
case a[0] == '-':
usage()
default:
2019-09-05 06:41:39 -04:00
ret = append(ret, a)
2019-09-04 22:19:39 -04:00
}
}
2019-09-05 06:41:39 -04:00
return ret, opts
2019-09-04 22:19:39 -04:00
}
func main() {
2019-09-05 06:41:39 -04:00
store, err := passgo.GetStore()
2019-09-04 22:19:39 -04:00
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
args := os.Args[1:]
2019-09-05 06:41:39 -04:00
args, opts := parse(args)
2019-09-04 22:19:39 -04:00
switch len(args) {
case 0:
2019-09-05 06:41:39 -04:00
l, err := store.List()
2019-09-04 22:19:39 -04:00
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
for _, x := range l {
_, n := path.Split(x.Pathname)
2019-09-05 06:41:39 -04:00
s := strings.Repeat(" /", x.Level)
2019-09-04 22:19:39 -04:00
z := ""
2019-09-05 06:41:39 -04:00
if x.Dir {
z = "/"
}
fmt.Println(strings.Join([]string{s, n, z}, ""))
2019-09-04 22:19:39 -04:00
}
case 1:
2019-09-05 06:41:39 -04:00
p, err := store.Decrypt(args[0])
2019-09-04 22:19:39 -04:00
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
if opts.clip {
2019-09-05 06:41:39 -04:00
passgo.Clip(p)
2019-09-04 22:19:39 -04:00
} else {
fmt.Print(p)
}
default:
usage()
}
}