-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
235 lines (213 loc) · 6.17 KB
/
utils.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Copyright © 2020 Fatih Ermiş <ermissaim@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/jedib0t/go-pretty/table"
)
var (
version = "0.1.0"
appDirName = ".katip"
commandsFileName = "commands.json"
warningCommandsFileNotExist = "No commands to show. Add one by 'katip new'"
confirmationTextForDeleteCommand = "Command will be removed"
confirmationTextForRunCommand = "Execute?"
confirmationTextForDeleteAppDirectory = "[CRITICAL] Remove everything inside ~/.katip ?"
)
type Command struct {
Command string `json:"command"`
Description string `json:"description"`
Alias string `json:"alias"`
}
type Commands struct {
Commands []Command `json:"commands"`
}
// Checks if app directory is exists
func checkIfAppDirExists() (bool, error) {
appDirPath, err := getAppDirPath()
if err != nil {
return false, err
}
if _, err := os.Stat(appDirPath); !os.IsNotExist(err) {
return true, nil
}
return false, nil
}
// Checks if commands file exists
func checkIfCommandsFileExists() bool {
commandsFilePath, err := getCommandsFilePath()
if err != nil {
return false
}
if _, err := os.Stat(commandsFilePath); !os.IsNotExist(err) {
return true
}
return false
}
// Returns home directory path
func getHomeDirPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return home, nil
}
// Returns application directory path
func getAppDirPath() (string, error) {
homeDir, err := getHomeDirPath()
if err != nil {
return "", err
}
return homeDir + "/" + appDirName, nil
}
// Returns commands file path
func getCommandsFilePath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return homeDir + "/" + appDirName + "/" + commandsFileName, nil
}
// Returns saved commands
func getCommands() (*Commands, error) {
var existingCommands *Commands
commandsFilePath, err := getCommandsFilePath()
if err != nil {
return nil, err
}
file, _ := ioutil.ReadFile(commandsFilePath)
err = json.Unmarshal(file, &existingCommands)
if err != nil {
return nil, err
}
return existingCommands, nil
}
// Creates app directory
func createAppDirectory(dirPath string) error {
fmt.Printf("Initializing environment in ~/%s directory\n", appDirName)
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
time.Sleep(2 * time.Second)
err := os.MkdirAll(dirPath, 0755)
if err != nil {
s.Stop()
return err
}
s.Stop()
return nil
}
// Creates commands file in app directory
func createCommandsFile() error {
commandsFilePath, err := getCommandsFilePath()
if err != nil {
return err
}
f, err := os.OpenFile(commandsFilePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
return nil
}
// Writes commands to commands file
func writeCommandsToFile(commands Commands) error {
commandsJSON, err := json.MarshalIndent(commands, "", "")
if err != nil {
return err
}
commandsFilePath, err := getCommandsFilePath()
if err != nil {
return err
}
err = ioutil.WriteFile(commandsFilePath, commandsJSON, 0644)
if err != nil {
return err
}
return nil
}
// Search commands that contains concatenated string of args
func searchCommands(args []string) ([]string, error) {
// concatenate args into the single string
concatenatedArgs := strings.Join(args[:], " ")
commands, err := getCommands()
if err != nil {
return nil, err
}
var concatenatedCommands, matches []string
for _, command := range commands.Commands {
concatenatedCommands = append(concatenatedCommands, command.Command+" :: "+command.Description+" :: "+command.Alias)
}
for _, concatenatedCommand := range concatenatedCommands {
isMatched, err := regexp.MatchString(concatenatedArgs, concatenatedCommand)
if err != nil {
return nil, err
} else if isMatched {
matches = append(matches, concatenatedCommand)
}
}
if len(matches) == 0 {
return nil, err
}
return matches, nil
}
// Prints commands as table
func printCommandsAsTable(commands *Commands) {
// convert commands to table.Row type
var commandRow []table.Row
for _, command := range commands.Commands {
commandRow = append(commandRow, table.Row{command.Command, command.Description, command.Alias})
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.Style().Options.SeparateRows = true
t.AppendHeader(table.Row{"Command", "Description", "Alias"})
t.AppendRows(commandRow)
t.Render()
return
}
// Prints commands with indexes
func printCommandsAsTableWithIndexes(commands *Commands) {
// convert commands to table.Row type
var commandRow []table.Row
for index, command := range commands.Commands {
commandRow = append(commandRow, table.Row{index + 1, command.Command, command.Description, command.Alias})
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.Style().Options.SeparateRows = true
t.AppendHeader(table.Row{"#", "Command", "Description", "Alias"})
t.AppendRows(commandRow)
t.Render()
return
}
func isIntInSlice(i int, slice []int) bool {
for _, a := range slice {
if a == i {
return true
}
}
return false
}