-
Notifications
You must be signed in to change notification settings - Fork 67
/
common.go
74 lines (62 loc) 路 1.42 KB
/
common.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
package utils
import (
"fmt"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"unicode"
)
func StringToIOReader(s string) io.Reader {
return strings.NewReader(s)
}
func IOReadCloserToString(r io.ReadCloser) string {
b, _ := io.ReadAll(r)
return string(b)
}
func ReadFileContent(filePath string) (string, error) {
fileContent, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return string(fileContent), nil
}
func StringReferencesFile(path string) bool {
// for the current working dir, check if the file exists
// by finding its absolute path and executing a stat command
absolutePath, err := filepath.Abs(path)
if err != nil {
return false
}
info, err := os.Stat(absolutePath)
if err != nil {
return false
}
// if the string is empty the absolute path will the entire dir
// otherwise the user also could send a directory by mistake
return info != nil && !info.IsDir()
}
func OpenBrowser(u string) error {
_, err := url.Parse(u)
if err != nil {
return err
}
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", u).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", u).Start()
case "darwin":
return exec.Command("open", u).Start()
default:
return fmt.Errorf("unsupported platform")
}
}
func Capitalize(str string) string {
runes := []rune(str)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}