-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
132 lines (107 loc) · 3.26 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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"github.com/bwmarrin/discordgo"
)
// Supported color constants for discord output
const (
COLOR_WHITE = "\n"
COLOR_RED = "diff\n-"
COLOR_GREEN = "diff\n+"
COLOR_BLUE = "yaml\n"
COLOR_YELLOW = "fix\n"
)
// Load configurations from a specified file and insert them into a config map
// @param config: *map[string]interface{} - The configuration map that will be loaded
// @param path: string - The path to the configuration file
// @return error - Any error that may have occurred
func getConfig(config *map[string]interface{}, path string) error {
// Load the configuration file
jsonConfig, err := os.Open(path)
if err != nil {
return err
}
// Close the file
defer jsonConfig.Close()
// Read the file's contents
byteValue, _ := ioutil.ReadAll(jsonConfig)
// Convert the byte array to a string and JSON parse it
json.Unmarshal(byteValue, config)
return nil
}
// Parse an incoming command and return the command and its arguments
// @param message: string - The command to parse
// @return []string - The command parsed
// @return error - Any error that may have occurred
func parseCommandLine(input string) ([]string, error) {
// Read the command and properly parse the input
reader := csv.NewReader(strings.NewReader(input))
reader.Comma = ' '
fields, err := reader.Read()
return fields, err
}
// **DISCORD FEATURE ONLY**
// Print a message to the discord channel
// @param session: *discordgo.Session - The discord session to use
// @param channelId: string - The channel to print the message to
// @param message: string - The message to print
// @param color: string - The color of the message
func echo(session *discordgo.Session, channelId string, message string, color string) {
// Send the message to the discord channel
if len(message) > 1950 {
message = message[:1950] + "(...)"
}
_, err := session.ChannelMessageSend(channelId, "```"+color+message+"```")
if err != nil {
fmt.Println("Error: Could not send a response message,", err)
}
}
// Support for file and folder directory formatting
// @param path: *string - The path to the directory
// @return error - Any error that may have occurred
func formatPath(path *string, channel string) error {
// Return to parent directory
if strings.Contains(*path, "..") {
currPath := getCurrentDirectoryPath(channel)
for _, dir := range strings.Split(*path, "/") {
if dir == ".." {
currDir, err := getDirectory(currPath, false, channel)
if err != nil {
return err
}
currPath = (*currDir).path
} else if dir != "" {
currPath += "/" + dir
}
}
*path = currPath
return nil
} else if (*path)[0] == '~' {
// Return to root directory
*path = "/" + (*path)[1:]
return nil
} else if (*path)[0] == '.' {
// Return to current directory
*path = getCurrentDirectoryPath(channel) + "/" + (*path)[1:]
return nil
} else {
*path = getCurrentDirectoryPath(channel) + "/" + *path
return nil
}
}
// Generate a new Discord channel name
// @param size int - The size of the channel name
// @return string - The new channel name
func NewChannelName(size int) string {
name := "channel-"
for i := 0; i < size; i++ {
name += fmt.Sprint(rand.Intn(10))
}
return name
}