|
| 1 | +//go:build darwin || linux || windows |
| 2 | + |
| 3 | +package discord |
| 4 | + |
| 5 | +import ( |
| 6 | + "io/fs" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/betterdiscord/cli/internal/utils" |
| 13 | +) |
| 14 | + |
| 15 | +// validateWindowsStyleInstall validates a Windows-style Discord installation path. |
| 16 | +// This is used for native Windows installs and WSL installs that point to Windows Discord. |
| 17 | +// Windows Discord has a nested structure: Discord/app-1.0.9002/modules/discord_desktop_core-1/discord_desktop_core |
| 18 | +func validateWindowsStyleInstall(proposed string) *DiscordInstall { |
| 19 | + var finalPath = "" |
| 20 | + var selected = filepath.Base(proposed) |
| 21 | + |
| 22 | + if strings.HasPrefix(selected, "Discord") { |
| 23 | + // Get version dir like app-1.0.9002 |
| 24 | + dFiles, err := os.ReadDir(proposed) |
| 25 | + if err != nil { |
| 26 | + return nil |
| 27 | + } |
| 28 | + |
| 29 | + candidates := utils.Filter(dFiles, func(file fs.DirEntry) bool { |
| 30 | + return file.IsDir() && versionRegex.MatchString(file.Name()) |
| 31 | + }) |
| 32 | + if len(candidates) == 0 { |
| 33 | + return nil |
| 34 | + } |
| 35 | + sort.Slice(candidates, func(i, j int) bool { return candidates[i].Name() < candidates[j].Name() }) |
| 36 | + versionDir := candidates[len(candidates)-1].Name() |
| 37 | + |
| 38 | + // Get core wrap like discord_desktop_core-1 |
| 39 | + dFiles, err = os.ReadDir(filepath.Join(proposed, versionDir, "modules")) |
| 40 | + if err != nil { |
| 41 | + return nil |
| 42 | + } |
| 43 | + candidates = utils.Filter(dFiles, func(file fs.DirEntry) bool { |
| 44 | + return file.IsDir() && strings.HasPrefix(file.Name(), "discord_desktop_core") |
| 45 | + }) |
| 46 | + if len(candidates) == 0 { |
| 47 | + return nil |
| 48 | + } |
| 49 | + coreWrap := candidates[len(candidates)-1].Name() |
| 50 | + |
| 51 | + finalPath = filepath.Join(proposed, versionDir, "modules", coreWrap, "discord_desktop_core") |
| 52 | + } |
| 53 | + |
| 54 | + // Handle app-* directories (e.g., app-1.0.9002) |
| 55 | + if strings.HasPrefix(selected, "app-") { |
| 56 | + dFiles, err := os.ReadDir(filepath.Join(proposed, "modules")) |
| 57 | + if err != nil { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + candidates := utils.Filter(dFiles, func(file fs.DirEntry) bool { |
| 62 | + return file.IsDir() && strings.HasPrefix(file.Name(), "discord_desktop_core") |
| 63 | + }) |
| 64 | + if len(candidates) == 0 { |
| 65 | + return nil |
| 66 | + } |
| 67 | + coreWrap := candidates[len(candidates)-1].Name() |
| 68 | + finalPath = filepath.Join(proposed, "modules", coreWrap, "discord_desktop_core") |
| 69 | + } |
| 70 | + |
| 71 | + if selected == "discord_desktop_core" { |
| 72 | + finalPath = proposed |
| 73 | + } |
| 74 | + |
| 75 | + // Verify the path and core.asar exist |
| 76 | + if utils.Exists(finalPath) && utils.Exists(filepath.Join(finalPath, "core.asar")) { |
| 77 | + return &DiscordInstall{ |
| 78 | + CorePath: finalPath, |
| 79 | + Channel: GetChannel(finalPath), |
| 80 | + Version: GetVersion(finalPath), |
| 81 | + IsFlatpak: false, |
| 82 | + IsSnap: false, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// validateUnixStyleInstall validates a Unix-style Discord installation path (Linux native, macOS). |
| 90 | +// Unix Discord has a flatter structure: discord/0.0.35/modules/discord_desktop_core |
| 91 | +func validateUnixStyleInstall(proposed string, detectFlatpak bool, detectSnap bool) *DiscordInstall { |
| 92 | + var finalPath = "" |
| 93 | + var selected = filepath.Base(proposed) |
| 94 | + |
| 95 | + if strings.HasPrefix(strings.ToLower(selected), "discord") { |
| 96 | + // Get version dir like 0.0.35 |
| 97 | + dFiles, err := os.ReadDir(proposed) |
| 98 | + if err != nil { |
| 99 | + return nil |
| 100 | + } |
| 101 | + |
| 102 | + candidates := utils.Filter(dFiles, func(file fs.DirEntry) bool { |
| 103 | + return file.IsDir() && versionRegex.MatchString(file.Name()) |
| 104 | + }) |
| 105 | + if len(candidates) == 0 { |
| 106 | + return nil |
| 107 | + } |
| 108 | + sort.Slice(candidates, func(i, j int) bool { return candidates[i].Name() < candidates[j].Name() }) |
| 109 | + versionDir := candidates[len(candidates)-1].Name() |
| 110 | + finalPath = filepath.Join(proposed, versionDir, "modules", "discord_desktop_core") |
| 111 | + } |
| 112 | + |
| 113 | + // Handle version directories (e.g., 0.0.35) |
| 114 | + if len(strings.Split(selected, ".")) == 3 { |
| 115 | + finalPath = filepath.Join(proposed, "modules", "discord_desktop_core") |
| 116 | + } |
| 117 | + |
| 118 | + if selected == "modules" { |
| 119 | + finalPath = filepath.Join(proposed, "discord_desktop_core") |
| 120 | + } |
| 121 | + |
| 122 | + if selected == "discord_desktop_core" { |
| 123 | + finalPath = proposed |
| 124 | + } |
| 125 | + |
| 126 | + // Verify the path and core.asar exist |
| 127 | + if utils.Exists(finalPath) && utils.Exists(filepath.Join(finalPath, "core.asar")) { |
| 128 | + isFlatpak := false |
| 129 | + isSnap := false |
| 130 | + |
| 131 | + if detectFlatpak { |
| 132 | + isFlatpak = strings.Contains(finalPath, "com.discordapp.") |
| 133 | + } |
| 134 | + if detectSnap { |
| 135 | + isSnap = strings.Contains(finalPath, "snap/") |
| 136 | + } |
| 137 | + |
| 138 | + return &DiscordInstall{ |
| 139 | + CorePath: finalPath, |
| 140 | + Channel: GetChannel(finalPath), |
| 141 | + Version: GetVersion(finalPath), |
| 142 | + IsFlatpak: isFlatpak, |
| 143 | + IsSnap: isSnap, |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments