-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.go
101 lines (88 loc) · 2.3 KB
/
modify.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
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"io"
"os"
omg "github.com/dotneko/omg/app"
cfg "github.com/dotneko/omg/config"
"github.com/spf13/cobra"
)
// modifyCmd represents the modify command
var modifyCmd = &cobra.Command{
Aliases: []string{"mod", "mv", "m", "rename"},
Use: "modify [name] [new name] *OPTIONAL -a [new address]",
Short: "Modify an address book entry",
Long: `Modify an address book entry
Examples:
# omg addr modify user1 user2
# omg addr modify user1 user2 -a newaddress1234567890123456789
# omg addr modify user1 -a newaddress1234567890123456789`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
if err := cobra.RangeArgs(1, 2)(cmd, args); err != nil {
return fmt.Errorf("expecting [name] [new name] as arguments")
}
if omg.IsValidAddress(args[0]) {
return fmt.Errorf("[name] cannot be an address")
}
if len(args) > 1 && omg.IsValidAddress(args[1]) {
return fmt.Errorf("[new name] cannot be an address")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
address, err := cmd.Flags().GetString("address")
if err != nil {
return err
}
if !omg.IsNormalAddress(address) {
return fmt.Errorf("invalid address")
}
if len(args) == 1 && address == "" {
return fmt.Errorf("missing new name or address flag")
}
return modifyAction(os.Stdout, address, args)
},
}
func init() {
addressCmd.AddCommand(modifyCmd)
modifyCmd.Flags().StringP("address", "a", "", "New address to use")
}
func modifyAction(out io.Writer, address string, args []string) error {
var newAlias string
oldAlias := args[0]
if len(args) > 1 {
newAlias = args[1]
} else if address != "" {
newAlias = oldAlias
}
l := &omg.Accounts{}
// Read from saved address book
if err := l.Load(cfg.OmgFilepath); err != nil {
return err
}
if len(*l) == 0 {
return fmt.Errorf("no accounts in store")
}
if len(newAlias) < cfg.MinAliasLength {
return fmt.Errorf("please use alias of at least 3 characters")
}
idx := l.GetIndex(oldAlias)
err := l.Modify(idx, newAlias, address)
if err != nil {
return err
}
err = l.Save(cfg.OmgFilepath)
if err != nil {
return err
}
address = l.GetAddress(newAlias)
fmt.Fprintf(out, "==> %s [%s]\n", newAlias, address)
return nil
}