forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.go
79 lines (64 loc) · 1.95 KB
/
aliases.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
package action
import (
"sort"
"strings"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/pwgen/pwrules"
"github.com/urfave/cli/v2"
)
// AliasesPrint prints all cofigured aliases.
func (s *Action) AliasesPrint(c *cli.Context) error {
out.Printf(c.Context, "Configured aliases:")
aliases := pwrules.AllAliases()
keys := make([]string, 0, len(aliases))
for k := range aliases {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
out.Printf(c.Context, "- %s -> %s", k, strings.Join(aliases[k], ", "))
}
return nil
}
// AliasesAdd adds a single alias to a domain.
func (s *Action) AliasesAdd(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
domain := c.Args().First()
alias := c.Args().Get(1)
if domain == "" || alias == "" {
return ExitError(ExitUsage, nil, "Usage: %s alias add <domain> <alias>", s.Name)
}
if err := pwrules.AddCustomAlias(domain, alias); err != nil {
return err
}
out.Printf(ctx, "Added alias %q to domain %q", alias, domain)
return nil
}
// AliasesRemove removes a single alias from a domain.
func (s *Action) AliasesRemove(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
domain := c.Args().First()
alias := c.Args().Get(1)
if domain == "" || alias == "" {
return ExitError(ExitUsage, nil, "Usage: %s alias remove <domain> <alias>", s.Name)
}
if err := pwrules.RemoveCustomAlias(domain, alias); err != nil {
return err
}
out.Printf(ctx, "Remove alias %q from domain %q", alias, domain)
return nil
}
// AliasesDelete remove an alias mapping for a domain.
func (s *Action) AliasesDelete(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
domain := c.Args().First()
if domain == "" {
return ExitError(ExitUsage, nil, "Usage: %s alias delete <domain>", s.Name)
}
if err := pwrules.DeleteCustomAlias(domain); err != nil {
return err
}
out.Printf(ctx, "Remove aliases for domain %q", domain)
return nil
}