2021-11-10 04:05:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-10 04:12:23 +00:00
|
|
|
"go.jolheiser.com/go-spectre"
|
2021-11-10 04:05:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-11-24 04:08:40 +00:00
|
|
|
pw, err := doMain(os.Args[1:])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(pw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doMain(args []string) (string, error) {
|
2021-11-10 04:05:41 +00:00
|
|
|
fs := flag.NewFlagSet("spectre", flag.ExitOnError)
|
2021-11-10 22:15:57 +00:00
|
|
|
fs.Usage = func() {
|
|
|
|
fmt.Fprintln(fs.Output(), "spectre [FLAGS] [site]")
|
|
|
|
fs.PrintDefaults()
|
|
|
|
}
|
2021-11-10 04:05:41 +00:00
|
|
|
usernameFlag := fs.String("username", "", "username")
|
|
|
|
secretFlag := fs.String("secret", "", "secret")
|
|
|
|
counterFlag := fs.Int("counter", 1, "counter")
|
|
|
|
scoperFlag := fs.String("scoper", "com.lyndir.masterpassword", "scoper base")
|
|
|
|
scopeFlag := spectre.Authentication
|
|
|
|
fs.Func("scope", "scope", func(s string) (err error) {
|
|
|
|
scopeFlag, err = spectre.ParseScope(s)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
var templateFlag spectre.Template
|
|
|
|
fs.Func("template", "template", func(s string) (err error) {
|
|
|
|
templateFlag, err = spectre.ParseTemplate(s)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
2021-11-24 04:08:40 +00:00
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
return "", err
|
2021-11-10 04:05:41 +00:00
|
|
|
}
|
|
|
|
if err := checkEnv(fs); err != nil {
|
2021-11-24 04:08:40 +00:00
|
|
|
return "", err
|
2021-11-10 04:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if templateFlag == "" {
|
|
|
|
templateFlag = scopeFlag.DefaultTemplate()
|
|
|
|
}
|
|
|
|
|
2021-11-24 03:47:01 +00:00
|
|
|
if *usernameFlag == "" || *secretFlag == "" || fs.NArg() < 1 {
|
2021-11-24 04:08:40 +00:00
|
|
|
return "", requiredArgs{
|
|
|
|
missingUsername: *usernameFlag == "",
|
|
|
|
missingSecret: *secretFlag == "",
|
|
|
|
missingSite: fs.NArg() < 1,
|
|
|
|
}
|
2021-11-10 04:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s, err := spectre.New(*usernameFlag, *secretFlag, spectre.WithScoper(spectre.SimpleScoper{
|
|
|
|
Key: *scoperFlag,
|
|
|
|
}))
|
|
|
|
if err != nil {
|
2021-11-24 04:08:40 +00:00
|
|
|
return "", err
|
2021-11-10 04:05:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 04:08:40 +00:00
|
|
|
return s.Site(fs.Arg(0),
|
2021-11-10 04:05:41 +00:00
|
|
|
spectre.WithScope(scopeFlag),
|
|
|
|
spectre.WithTemplate(templateFlag),
|
|
|
|
spectre.WithCounter(*counterFlag),
|
2021-11-24 04:08:40 +00:00
|
|
|
), nil
|
|
|
|
}
|
2021-11-10 04:05:41 +00:00
|
|
|
|
2021-11-24 04:08:40 +00:00
|
|
|
type requiredArgs struct {
|
|
|
|
missingUsername bool
|
|
|
|
missingSecret bool
|
|
|
|
missingSite bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r requiredArgs) Error() string {
|
|
|
|
s := "--username, --secret, and <site-name> are required, missing: "
|
|
|
|
if r.missingUsername {
|
|
|
|
s += "\n- username"
|
|
|
|
}
|
|
|
|
if r.missingSecret {
|
|
|
|
s += "\n- secret"
|
|
|
|
}
|
|
|
|
if r.missingSite {
|
|
|
|
s += "\n- site name"
|
|
|
|
}
|
|
|
|
return s
|
2021-11-10 04:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkEnv(fs *flag.FlagSet) error {
|
|
|
|
provided := map[string]struct{}{}
|
|
|
|
fs.Visit(func(f *flag.Flag) {
|
|
|
|
provided[f.Name] = struct{}{}
|
|
|
|
})
|
|
|
|
var visitErr error
|
|
|
|
fs.VisitAll(func(f *flag.Flag) {
|
|
|
|
if visitErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := provided[f.Name]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
env := os.Getenv(fmt.Sprintf("SPECTRE_%s", strings.ToUpper(f.Name)))
|
|
|
|
if env == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := fs.Set(f.Name, env); err != nil {
|
|
|
|
visitErr = fmt.Errorf("could not set flag %q to %q", f.Name, env)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|