-
Notifications
You must be signed in to change notification settings - Fork 12
/
ls.go
82 lines (67 loc) · 1.77 KB
/
ls.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
package cli
import (
"fmt"
"strings"
"github.com/fishi0x01/vsh/client"
"github.com/fishi0x01/vsh/log"
)
// ListCommand container for 'ls' parameters
type ListCommand struct {
name string
args *ListCommandArgs
client *client.Client
}
// ListCommandArgs provides a struct for go-arg parsing
type ListCommandArgs struct {
Path string `arg:"positional" help:"path to list contents of, defaults to current path"`
}
// Description provides detail on what the command does
func (ListCommandArgs) Description() string {
return "lists the secrets at a path"
}
// NewListCommand creates a new ListCommand parameter container
func NewListCommand(c *client.Client) *ListCommand {
return &ListCommand{
name: "ls",
client: c,
args: &ListCommandArgs{},
}
}
// GetName returns the ListCommand's name identifier
func (cmd *ListCommand) GetName() string {
return cmd.name
}
// GetArgs provides the struct holding arguments for the command
func (cmd *ListCommand) GetArgs() interface{} {
return cmd.args
}
// IsSane returns true if command is sane
func (cmd *ListCommand) IsSane() bool {
return cmd.args.Path != ""
}
// PrintUsage print command usage
func (cmd *ListCommand) PrintUsage() {
fmt.Println(Help(cmd))
}
// Parse given arguments and return status
func (cmd *ListCommand) Parse(args []string) error {
_, err := parseCommandArgs(args, cmd)
if err != nil {
return err
}
if cmd.args.Path == "" {
cmd.args.Path = cmd.client.Pwd
}
return nil
}
// Run executes 'ls' with given ListCommand's parameters
func (cmd *ListCommand) Run() int {
newPwd := cmdPath(cmd.client.Pwd, cmd.args.Path)
result, err := cmd.client.List(newPwd)
if err != nil {
log.UserError("Not a valid path for operation: %s", newPwd)
return 1
}
log.UserInfo(strings.Join(result, "\n"))
return 0
}