-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
list.go
159 lines (134 loc) · 3.66 KB
/
list.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package action
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/store/leaf"
"github.com/gopasspw/gopass/internal/tree"
"github.com/gopasspw/gopass/pkg/ctxutil"
shellquote "github.com/kballard/go-shellquote"
"github.com/noborus/ov/oviewer"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)
// List all secrets as a tree. If the filter argument is non-empty
// display only those that have this prefix.
func (s *Action) List(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
filter := c.Args().First()
flat := c.Bool("flat")
stripPrefix := c.Bool("strip-prefix")
folders := c.Bool("folders")
// print the path if the argument is a direct hit.
if s.Store.Exists(ctx, filter) && !s.Store.IsDir(ctx, filter) {
fmt.Println(filter)
return nil
}
// we only support listing folders in flat mode currently.
if folders {
flat = true
}
l, err := s.Store.Tree(ctx)
if err != nil {
return exit.Error(exit.List, err, "failed to list store: %s", err)
}
// set limit to infinite by default unless it's set with the flag
limit := tree.INF
if c.IsSet("limit") {
limit = c.Int("limit")
}
return s.listFiltered(ctx, l, limit, flat, folders, stripPrefix, filter)
}
func (s *Action) listFiltered(ctx context.Context, l *tree.Root, limit int, flat, folders, stripPrefix bool, filter string) error {
sep := leaf.Sep
if filter == "" || filter == sep {
// We list all entries then.
stripPrefix = true
} else {
// If the filter ends with a separator, we still want to find the content of that folder.
if strings.HasSuffix(filter, sep) {
filter = filter[:len(filter)-1]
}
// To avoid shadowing l since we need it outside of the else scope.
var err error
// We restrict ourselves to the filter.
l, err = l.FindFolder(filter)
if err != nil {
return exit.Error(exit.NotFound, nil, "Entry %q not found", filter)
}
l.SetName(filter + sep)
}
if flat {
listOver := l.List
if folders {
listOver = l.ListFolders
}
for _, e := range listOver(limit) {
if stripPrefix {
e = strings.TrimPrefix(e, filter+sep)
}
fmt.Fprintln(stdout, e)
}
return nil
}
// we may need to redirect stdout for the pager support.
so, buf := redirectPager(ctx, l)
fmt.Fprintln(so, l.Format(limit))
if buf != nil {
if err := s.pager(ctx, buf); err != nil {
return exit.Error(exit.Unknown, err, "failed to invoke pager: %s", err)
}
}
return nil
}
// redirectPager returns a redirected io.Writer if the output would exceed
// the terminal size.
func redirectPager(ctx context.Context, subtree *tree.Root) (io.Writer, *bytes.Buffer) {
if config.Bool(ctx, "core.nopager") {
return stdout, nil
}
_, rows, err := term.GetSize(0)
if err != nil {
return stdout, nil
}
if subtree == nil || subtree.Len() < rows {
return stdout, nil
}
if pager := os.Getenv("PAGER"); pager == "" {
return stdout, nil
}
color.NoColor = true
buf := &bytes.Buffer{}
return buf, buf
}
// pager invokes the default pager with the given content.
func (s *Action) pager(ctx context.Context, buf io.Reader) error {
if config.Bool(ctx, "output.internal-pager") {
ov, err := oviewer.NewRoot(buf)
if err != nil {
return err
}
return ov.Run()
}
pager := os.Getenv("PAGER")
if pager == "" {
fmt.Fprintln(stdout, buf)
return nil
}
args, err := shellquote.Split(pager)
if err != nil {
return fmt.Errorf("failed to split pager command: %w", err)
}
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Stdin = buf
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}