passgo/cmd/gpass/main.go

77 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"path"
"strings"
"gitlab.wow.st/gmp/pass"
)
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 := pass.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 {
pass.Clip(p)
} else {
fmt.Print(p)
}
default:
usage()
}
pass.AskPass2()
}