/
aliases.go
128 lines (108 loc) · 2.81 KB
/
aliases.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
package cmd
import (
"fmt"
"regexp"
"strconv"
"github.com/mightymatth/arcli/config"
"github.com/jedib0t/go-pretty/table"
"github.com/mightymatth/arcli/utils"
"github.com/spf13/cobra"
)
func newAliasesCmd() *cobra.Command {
c := &cobra.Command{
Use: "aliases",
Aliases: []string{"a", "alias"},
Short: "Words that can be used instead of issue or project ids",
}
c.AddCommand(newAliasesListCmd())
c.AddCommand(newAliasesAddCmd())
c.AddCommand(newAliasesDeleteCmd())
return c
}
func newAliasesListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Aliases: []string{"ls", "all"},
Short: "List of all user aliases",
Run: func(cmd *cobra.Command, args []string) {
drawAliases()
},
}
}
func newAliasesAddCmd() *cobra.Command {
return &cobra.Command{
Use: "add [aliasName] [id]",
Aliases: []string{"set", "new"},
Args: validAliasesAddArgs(),
Short: "Add alias entry",
Run: func(cmd *cobra.Command, args []string) {
err := config.SetAlias(args[0], args[1])
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("'%v: %v' has been successfully added to aliases.\n", args[0], args[1])
},
}
}
func newAliasesDeleteCmd() *cobra.Command {
return &cobra.Command{
Use: "delete [aliasName]",
Aliases: []string{"remove", "rm", "del"},
Args: validAliasesDeleteArgs(),
Short: "Remove alias entry",
Run: func(cmd *cobra.Command, args []string) {
_, found := config.GetAlias(args[0])
if !found {
fmt.Printf("Alias with key '%v' does not exist, so can't be deleted.\n", args[0])
return
}
err := config.SetAlias(args[0], "")
if err != nil {
fmt.Println("Cannot delete alias:", err)
return
}
fmt.Printf("Alias with key '%v' has been deleted.\n", args[0])
},
}
}
func drawAliases() {
aliases := config.GetAliases()
if len(aliases) == 0 {
fmt.Println("You have no previously aliases set.")
fmt.Printf("These can be set with: '%v'\n", newAliasesAddCmd().UseLine())
return
}
t := utils.NewTable()
t.AppendHeader(table.Row{"Alias", "ID"})
for key, val := range aliases {
t.AppendRow(table.Row{key, val})
}
t.Render()
}
func validAliasesAddArgs() cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
err := cobra.ExactArgs(2)(cmd, args)
if err != nil {
return err
}
keyPattern := "^[[:alnum:]-_]{1,30}$"
if !regexp.MustCompile(keyPattern).MatchString(args[0]) {
return fmt.Errorf("alias key must have pattern '%v'", keyPattern)
}
_, err = strconv.ParseInt(args[1], 10, 64)
if err != nil {
return fmt.Errorf("alias value must be integer")
}
return nil
}
}
func validAliasesDeleteArgs() cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
err := cobra.ExactArgs(1)(cmd, args)
if err != nil {
return err
}
return nil
}
}