-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
73 lines (62 loc) · 1.87 KB
/
utils.go
File metadata and controls
73 lines (62 loc) · 1.87 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// CompoundHandler is a function that handles compound commands
type CompoundHandler func(args []string, dryRun bool, planJson bool)
// getCompoundHandler returns a handler for compound commands if one exists
func getCompoundHandler(command string) (CompoundHandler, bool) {
handlers := map[string]CompoundHandler{
"json": handleJsonCommand,
"md": handleMarkdownCommand,
"excalifont": handleExcalifontCommand,
}
handler, ok := handlers[command]
return handler, ok
}
// isCommandSafe checks if a command is read-only/safe (doesn't modify system state)
func isCommandSafe(command string) bool {
safeCommands := []string{
"ps", "top", "uptime", "whoami", "id", "hostname", "uname",
"df", "du", "free", "vmstat", "iostat", "netstat", "ss",
"ls", "pwd", "cat", "head", "tail", "wc", "grep",
"cal", "env", "printenv", "which", "whereis",
"git status", "git log", "git diff", "git show",
}
cmdLower := strings.ToLower(strings.TrimSpace(command))
for _, safe := range safeCommands {
if strings.HasPrefix(cmdLower, safe) {
return true
}
}
return false
}
// isHexString checks if a string contains only hexadecimal characters
func isHexString(s string) bool {
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
return false
}
}
return true
}
// usePureMd uses the pure.md service to convert URL to markdown
func usePureMd(url string) {
if !isToolAvailable("curl") {
fmt.Println("Error: curl is required but not available")
os.Exit(1)
}
pureMdURL := "https://pure.md/" + url
fmt.Printf("Fetching from pure.md: %s\n\n", pureMdURL)
cmd := exec.Command("curl", "-s", pureMdURL)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("\nError: Failed to fetch from pure.md\n")
os.Exit(1)
}
}