internal/platform.go
68 lines · 1335 bytes
1package internal
2
3import (
4 "os"
5 "path/filepath"
6 "runtime"
7 "strings"
8)
9
10type Platform string
11
12const (
13 PlatformDesktop Platform = "desktop"
14 PlatformAndroid Platform = "android"
15)
16
17func DetectPlatform() Platform {
18 if runtime.GOOS == "android" {
19 return PlatformAndroid
20 }
21
22 termuxMarkers := []string{
23 os.Getenv("TERMUX_VERSION"),
24 os.Getenv("TERMUX_APP_PID"),
25 }
26 for _, marker := range termuxMarkers {
27 if strings.TrimSpace(marker) != "" {
28 return PlatformAndroid
29 }
30 }
31
32 prefix := os.Getenv("PREFIX")
33 if strings.Contains(prefix, "com.termux") {
34 return PlatformAndroid
35 }
36
37 return PlatformDesktop
38}
39
40func DefaultConfigPathForPlatform(platform Platform) string {
41 homeDir := os.Getenv("HOME")
42 if runtime.GOOS == "windows" && homeDir == "" {
43 homeDir = os.Getenv("USERPROFILE")
44 }
45
46 fileName := "curd.conf"
47 if platform == PlatformAndroid {
48 fileName = "android.conf"
49 }
50
51 return filepath.Join(homeDir, ".config", "curd", fileName)
52}
53
54func DefaultConfigPath() string {
55 return DefaultConfigPathForPlatform(DetectPlatform())
56}
57
58func InferPlatformForConfigPath(configPath string, detected Platform) Platform {
59 base := strings.ToLower(filepath.Base(strings.TrimSpace(configPath)))
60 switch base {
61 case "android.conf":
62 return PlatformAndroid
63 case "curd.conf":
64 return PlatformDesktop
65 default:
66 return detected
67 }
68}