forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
84 lines (66 loc) · 2.21 KB
/
context.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
package main
import (
"context"
"os"
"runtime"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/backend/crypto/gpg"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/ctxutil"
"golang.org/x/crypto/ssh/terminal"
)
func initContext(ctx context.Context, cfg *config.Config) context.Context {
// autosync
ctx = sub.WithAutoSync(ctx, cfg.Root.AutoSync)
// always trust
ctx = gpg.WithAlwaysTrust(ctx, true)
// ask for more
ctx = ctxutil.WithAskForMore(ctx, cfg.Root.AskForMore)
// clipboard timeout
ctx = ctxutil.WithClipTimeout(ctx, cfg.Root.ClipTimeout)
// no confirm
ctx = ctxutil.WithNoConfirm(ctx, cfg.Root.NoConfirm)
// no pager
ctx = ctxutil.WithNoPager(ctx, cfg.Root.NoPager)
// show safe content
ctx = ctxutil.WithShowSafeContent(ctx, cfg.Root.SafeContent)
// always use symbols
ctx = ctxutil.WithUseSymbols(ctx, cfg.Root.UseSymbols)
// never use color
ctx = ctxutil.WithNoColor(ctx, cfg.Root.NoColor)
// check recipients conflicts with always trust, make sure it's not enabled
// when always trust is
if gpg.IsAlwaysTrust(ctx) {
ctx = sub.WithCheckRecipients(ctx, false)
}
// debug flag
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
ctx = ctxutil.WithDebug(ctx, true)
}
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" || ctxutil.IsNoColor(ctx) {
color.NoColor = true
ctx = ctxutil.WithColor(ctx, false)
}
// only emit color codes when stdout is a terminal
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
color.NoColor = true
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)
}
// disable colored output on windows since cmd.exe doesn't support ANSI color
// codes. Other terminal may do, but until we can figure that out better
// disable this for all terms on this platform
if runtime.GOOS == "windows" {
color.NoColor = true
ctx = ctxutil.WithColor(ctx, false)
}
return ctx
}