-
Notifications
You must be signed in to change notification settings - Fork 12
/
mv.go
95 lines (76 loc) · 2.01 KB
/
mv.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
package cli
import (
"fmt"
"github.com/fishi0x01/vsh/client"
"github.com/fishi0x01/vsh/log"
)
// MoveCommand container for all 'mv' parameters
type MoveCommand struct {
name string
args *MoveCommandArgs
client *client.Client
}
// MoveCommandArgs provides a struct for go-arg parsing
type MoveCommandArgs struct {
Source string `arg:"positional,required" help:"path to move"`
Target string `arg:"positional,required" help:"path to move source to"`
}
// Description provides detail on what the command does
func (MoveCommandArgs) Description() string {
return "moves a secret from one path to another"
}
// NewMoveCommand creates a new MoveCommand parameter container
func NewMoveCommand(c *client.Client) *MoveCommand {
return &MoveCommand{
name: "mv",
client: c,
args: &MoveCommandArgs{},
}
}
// GetName returns the MoveCommand's name identifier
func (cmd *MoveCommand) GetName() string {
return cmd.name
}
// GetArgs provides the struct holding arguments for the command
func (cmd *MoveCommand) GetArgs() interface{} {
return cmd.args
}
// IsSane returns true if command is sane
func (cmd *MoveCommand) IsSane() bool {
return cmd.args.Source != "" && cmd.args.Target != ""
}
// PrintUsage print command usage
func (cmd *MoveCommand) PrintUsage() {
fmt.Println(Help(cmd))
}
// Parse given arguments and return status
func (cmd *MoveCommand) Parse(args []string) error {
_, err := parseCommandArgs(args, cmd)
if err != nil {
return err
}
return nil
}
// Run executes 'mv' with given MoveCommand's parameters
func (cmd *MoveCommand) Run() int {
return transportSecrets(cmd.client, cmd.args.Source, cmd.args.Target, cmd.moveSecret)
}
func (cmd *MoveCommand) moveSecret(source string, target string) error {
// read
secret, err := cmd.client.Read(source)
if err != nil {
return err
}
// write
err = cmd.client.Write(target, secret)
if err != nil {
return err
}
// delete
err = cmd.client.Delete(source)
if err != nil {
return err
}
log.UserDebug("Moved %s to %s", source, target)
return nil
}