passgo/cmd/gpass/main.go
2019-09-04 22:37:42 -04:00

76 lines
1.1 KiB
Go

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