This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
list.go
184 lines (160 loc) · 4.74 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package secrethub
import (
"errors"
"fmt"
"io"
"sort"
"text/tabwriter"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-go/internals/errio"
)
// LsCommand lists a repo, secret or namespace.
type LsCommand struct {
path api.Path
quiet bool
useTimestamps bool
io ui.IO
newClient newClientFunc
}
// NewLsCommand creates a new LsCommand.
func NewLsCommand(io ui.IO, newClient newClientFunc) *LsCommand {
return &LsCommand{
io: io,
newClient: newClient,
}
}
// Register registers the command, arguments and flags on the provided Registerer.
func (cmd *LsCommand) Register(r cli.Registerer) {
clause := r.Command("ls", "List contents of a path.")
clause.Alias("list")
clause.Flags().BoolVarP(&cmd.quiet, "quiet", "q", false, "Only print paths.")
registerTimestampFlag(clause, &cmd.useTimestamps)
clause.BindAction(cmd.Run)
clause.BindArguments([]cli.Argument{
{Value: &cmd.path, Name: "path", Required: false, Description: "The path to list contents of."},
})
}
// Run lists a repo, secret or namespace.
func (cmd *LsCommand) Run() error {
timeFormatter := NewTimeFormatter(cmd.useTimestamps)
if cmd.path == "" {
repoLSCommand := NewRepoLSCommand(cmd.io, cmd.newClient)
repoLSCommand.quiet = cmd.quiet
repoLSCommand.useTimestamps = cmd.useTimestamps
return repoLSCommand.Run()
}
client, err := cmd.newClient()
if err != nil {
return err
}
// It must be a SecretPath as only SecretPaths has versions.
if cmd.path.HasVersion() {
secretPath, err := cmd.path.ToSecretPath()
if err != nil {
fmt.Println("no secret path!")
return err
}
version, err := client.Secrets().Versions().GetWithoutData(secretPath.Value())
if err != nil {
return err
}
err = printVersions(cmd.io.Output(), cmd.quiet, timeFormatter, version)
if err != nil {
return err
}
return nil
}
// Try DirPath
dirPath, err := cmd.path.ToDirPath()
if err == nil {
dirFS, err := client.Dirs().GetTree(dirPath.Value(), 1, false)
if api.IsErrNotFound(err) && dirPath.IsRepoPath() {
return err
} else if err != nil && !api.IsErrNotFound(err) {
return err
} else if err == nil {
err = printDir(cmd.io.Output(), cmd.quiet, dirFS.RootDir, timeFormatter)
if err != nil {
return err
}
return nil
}
}
// Try SecretPath
secretPath, err := cmd.path.ToSecretPath()
if err == nil {
versions, err := client.Secrets().Versions().ListWithoutData(secretPath.Value())
if api.IsErrNotFound(err) {
return ErrResourceNotFound(cmd.path)
} else if err != nil {
return err
}
err = printVersions(cmd.io.Output(), cmd.quiet, timeFormatter, versions...)
if err != nil {
return err
}
return nil
}
workspace, err := cmd.path.ToNamespace()
if err == nil {
cmd := RepoLSCommand{
workspace: workspace,
useTimestamps: cmd.useTimestamps,
quiet: cmd.quiet,
io: cmd.io,
newClient: cmd.newClient,
}
return cmd.Run()
}
// Path should always be a namespace, repository, directory, secret or secret version.
// Therefore, this should never happen.
return errio.UnexpectedError(errors.New("invalid path argument"))
}
// printVersions prints out secret versions in long or short format.
func printVersions(w io.Writer, quiet bool, timeFormatter TimeFormatter, versions ...*api.SecretVersion) error {
if quiet {
for _, version := range versions {
fmt.Fprintf(w, "%s\n", version.Name())
}
} else {
w := tabwriter.NewWriter(w, 0, 2, 2, ' ', 0)
fmt.Fprintf(w, "%s\t%s\t%s\n", "NAME", "STATUS", "CREATED")
for _, version := range versions {
fmt.Fprintf(w, "%s\t%s\t%s\n", version.Name(), version.Status, timeFormatter.Format(version.CreatedAt.Local()))
}
err := w.Flush()
if err != nil {
return err
}
}
return nil
}
// printDir prints out directory contents in long or short format.
func printDir(w io.Writer, quiet bool, dir *api.Dir, timeFormatter TimeFormatter) error {
sort.Sort(api.SortDirByName(dir.SubDirs))
sort.Sort(api.SortSecretByName(dir.Secrets))
if quiet {
for _, dir := range dir.SubDirs {
fmt.Fprintf(w, "%s/\n", dir.Name)
}
for _, secret := range dir.Secrets {
fmt.Fprintf(w, "%s\n", secret.Name)
}
} else {
tw := tabwriter.NewWriter(w, 0, 2, 2, ' ', 0)
fmt.Fprintf(tw, "%s\t%s\t%s\n", "NAME", "STATUS", "CREATED")
for _, dir := range dir.SubDirs {
fmt.Fprintf(tw, "%s/\t%s\t%s\n", dir.Name, dir.Status, timeFormatter.Format(dir.CreatedAt.Local()))
}
for _, secret := range dir.Secrets {
fmt.Fprintf(tw, "%s\t%s\t%s\n", secret.Name, secret.Status, timeFormatter.Format(secret.CreatedAt.Local()))
}
err := tw.Flush()
if err != nil {
return err
}
}
return nil
}