-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
76 lines (67 loc) · 2.06 KB
/
Copy pathpaths.go
File metadata and controls
76 lines (67 loc) · 2.06 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
74
75
76
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
func contextsBase() string {
return filepath.Join(azenvHome(), "contexts")
}
func azenvHome() string {
if v := os.Getenv("AZENV_HOME"); v != "" {
return v
}
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "azenv")
}
return filepath.Join(homeDir(), ".config", "azenv")
}
func contextDir(name string) string { return filepath.Join(contextsBase(), name) }
func contextPath(name string) string { return filepath.Join(contextDir(name), "azure") }
func metaPath(name string) string { return filepath.Join(contextDir(name), "metadata.yaml") }
func configPath() string { return filepath.Join(azenvHome(), "config.yaml") }
func homeDir() string { h, _ := os.UserHomeDir(); return h }
func validateName(name string) error {
if name == "" {
return errors.New("context name cannot be empty")
}
if name == "." || name == ".." {
return fmt.Errorf("invalid context name %q; use letters, numbers, dots, underscores, and hyphens", name)
}
for _, r := range name {
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '.' || r == '_' || r == '-' {
continue
}
return fmt.Errorf("invalid context name %q; use letters, numbers, dots, underscores, and hyphens", name)
}
return nil
}
func currentContextName() string {
if cfg := os.Getenv("AZURE_CONFIG_DIR"); cfg != "" {
return inferContextName(cfg)
}
return ""
}
func inferContextName(cfg string) string {
base := contextsBase()
rel, err := filepath.Rel(filepath.Clean(base), filepath.Clean(cfg))
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
return ""
}
parts := strings.Split(rel, string(os.PathSeparator))
if len(parts) == 2 && parts[0] != "." && parts[1] == "azure" {
return parts[0]
}
return ""
}
func samePath(a, b string) bool {
if a == "" || b == "" {
return false
}
return filepath.Clean(a) == filepath.Clean(b)
}
func shellQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
}