-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
100 lines (82 loc) · 2.21 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"fmt"
"os"
"sort"
ap "passline/pkg/action"
"passline/pkg/config"
"passline/pkg/ctxutil"
"github.com/blang/semver"
ucli "github.com/urfave/cli/v2"
"golang.org/x/term"
)
func setupApp(ctx context.Context, sv semver.Version) (context.Context, *ucli.App) {
// try to load config
cfg, err := config.Get()
if err != nil {
os.Exit(ap.ExitConfig)
}
// set config values
ctx = initContext(ctx, cfg)
action, err := ap.New(cfg, sv)
if err != nil {
os.Exit(ap.ExitUnknown)
}
app := ucli.NewApp()
app.Name = "Passline"
app.Usage = "Password manager"
app.HelpName = "passline"
app.Version = sv.String()
app.Description = "Password manager for the command line"
app.EnableBashCompletion = true
// Append website information to default helper print
app.CustomAppHelpTemplate = fmt.Sprintf(`%s
WEBSITE:
https://github.com/perryrh0dan/passline
`, ucli.AppHelpTemplate)
app.Flags = []ucli.Flag{
&ucli.BoolFlag{
Name: "print",
Aliases: []string{"p"},
Usage: "Prints the password to the terminal",
},
&ucli.BoolFlag{
Name: "yes",
Usage: "Assume yes on all yes/no questions or use the default on all others",
},
&ucli.StringFlag{
Name: "category",
Aliases: []string{"c"},
Usage: "Select only items with given category",
},
&ucli.BoolFlag{
Name: "noclip",
Usage: "Disable copy to clipboard",
},
}
// default command to get password
app.Action = func(c *ucli.Context) error {
return action.Default(c)
}
app.Commands = action.GetCommands()
sort.Sort(ucli.FlagsByName(app.Flags))
sort.Sort(ucli.CommandsByName(app.Commands))
return ctx, app
}
func initContext(ctx context.Context, cfg *config.Config) context.Context {
// initialize from config, may be overridden by env vars
ctx = cfg.WithContext(ctx)
// only emit color codes when stdout is a terminal
if !term.IsTerminal(int(os.Stdout.Fd())) {
ctx = ctxutil.WithColor(ctx, false)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithInteractive(ctx, false)
}
// reading from stdin?
if info, err := os.Stdin.Stat(); err == nil && info.Mode()&os.ModeCharDevice == 0 {
ctx = ctxutil.WithInteractive(ctx, false)
ctx = ctxutil.WithStdin(ctx, true)
}
return ctx
}