CookieMod/util.go

60 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"golang.org/x/term"
)
func FolderExists(path string) (exists bool, fileAtPath bool) {
info, err := os.Stat(path)
exists = err == nil && info != nil && info.IsDir()
fileAtPath = info != nil && !info.IsDir()
return
}
func FileExists(path string) (exists bool, folderAtPath bool) {
info, err := os.Stat(path)
exists = err == nil && info != nil && !info.IsDir()
folderAtPath = info != nil && info.IsDir()
return
}
func SecretPrompt(prompt string) string {
fmt.Print(prompt)
data, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't read your input! Reason: %s", err.Error()))
}
return string(data)
}
// !ban @ExampleUser "Spam"
// !tempban @ExampleUser "Spam" 1h
func ParseArgs(s string) []string {
var args []string
var currentArg string
var insideQuotes bool
for _, char := range s {
switch char {
case ' ':
if !insideQuotes {
if currentArg != "" {
args = append(args, currentArg)
currentArg = ""
}
continue
}
case '"':
insideQuotes = !insideQuotes
continue
}
currentArg += string(char)
}
if currentArg != "" {
args = append(args, currentArg)
}
return args
}