-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
107 lines (94 loc) · 2.7 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
package ffcss
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
)
// isValidURL tests a string to determine if it is a well-structured url or not.
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
// getConfigDir returns the absolute path of ffcss's configuration directory
func getConfigDir() string {
homedir, err := os.UserHomeDir()
if err != nil {
panic(fmt.Errorf("couldn't get your home directory: %w", err))
}
return filepath.Join(homedir, ".config", "ffcss")
}
// getCacheDir returns the temporary path for cloned repos and other stuff
func getCacheDir() string {
homedir, err := os.UserHomeDir()
if err != nil {
panic(fmt.Errorf("couldn't get your home directory: %w", err))
}
return filepath.Join(homedir, ".cache", "ffcss")
}
// CacheDir joins the cache directory with the given path segments
func CacheDir(pathSegments ...string) string {
return filepath.Join(getCacheDir(), filepath.Join(pathSegments...))
}
// ConfigDir joins the config directory with the given path segments
func ConfigDir(pathSegments ...string) string {
return filepath.Join(getConfigDir(), filepath.Join(pathSegments...))
}
func cwd() string {
wd, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("couldn't get the current working directory: %w", err))
}
return wd
}
// renameIfExists renames from to to if from exists. If it doesn't, don't attempt renaming.
func renameIfExists(from string, to string) error {
if _, err := os.Stat(from); os.IsNotExist(err) {
return nil
}
if _, err := os.Stat(to); os.IsNotExist(err) {
return os.Rename(from, to)
}
err := os.RemoveAll(to)
if err != nil {
return err
}
return os.Rename(from, to)
}
// vimModeEnabled returns true if the user has explicitly set vim mode, or if the $EDITOR is vim/neovim
func vimModeEnabled() bool {
if os.Getenv("VIM_MODE") == "1" || os.Getenv("VIM_STYLE") == "1" {
return true
}
progname := filepath.Base(os.Getenv("EDITOR"))
return progname == "vim" || progname == "nvim"
}
// prefixEachLine prepends each line of s with the provided prefix (with).
// Only supports UNIX-Style line endings (\n)
func prefixEachLine(s string, with string) string {
var prefixedLines []string
for _, line := range strings.Split(s, "\n") {
prefixedLines = append(prefixedLines, with+line)
}
return strings.Join(prefixedLines, "\n")
}
// GOOStoOS returns user-friendly OS names from a given GOOS.
// darwin becomes macos and plan9 becomes linux.
func GOOStoOS(GOOS string) string {
switch GOOS {
case "darwin":
return "macos"
case "plan9":
return "linux"
default:
return GOOS
}
}