Skip to content

Commit 688b4e7

Browse files
committed
fix: parse urls properly
1 parent 18b47ce commit 688b4e7

File tree

7 files changed

+52
-50
lines changed

7 files changed

+52
-50
lines changed

cmd/info.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/spf13/cobra"
77

88
"github.com/betterdiscord/cli/internal/betterdiscord"
9-
"github.com/betterdiscord/cli/internal/output"
109
)
1110

1211
func init() {
@@ -24,8 +23,6 @@ var infoCmd = &cobra.Command{
2423
return fmt.Errorf("BetterDiscord does not appear to be installed, try running 'bdcli install' first")
2524
}
2625

27-
output.Printf("📦 BetterDiscord Information:\n\n")
28-
2926
bdinstall.LogBuildinfo()
3027

3128
return nil

cmd/plugins.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,13 @@ var pluginsInfoCmd = &cobra.Command{
6363
Args: cobra.ExactArgs(1),
6464
RunE: func(cmd *cobra.Command, args []string) error {
6565
name := args[0]
66-
items, err := betterdiscord.ListAddons(betterdiscord.AddonPlugin)
67-
if err != nil {
68-
return err
69-
}
70-
71-
for _, item := range items {
72-
// Match by filename or meta name
73-
if item.BaseName == name || item.FullFilename == name || item.Meta.Name == name {
74-
betterdiscord.LogLocalAddonInfo(&item)
75-
return nil
76-
}
77-
}
66+
existing := betterdiscord.FindAddon(betterdiscord.AddonPlugin, name)
67+
if existing == nil {
68+
output.Printf("❌ Plugin '%s' not found.\n", name)
69+
return nil
70+
}
7871

79-
output.Printf("❌ Plugin '%s' not found.\n", name)
72+
betterdiscord.LogLocalAddonInfo(existing)
8073
return nil
8174
},
8275
}
@@ -103,7 +96,7 @@ var pluginsInstallCmd = &cobra.Command{
10396
if err != nil {
10497
return err
10598
}
106-
output.Printf("✅ Plugin installed at %s\n", resolved.URL)
99+
output.Printf("✅ Plugin installed at %s\n", resolved.Path)
107100
return nil
108101
},
109102
}
@@ -203,7 +196,7 @@ var pluginsUpdateCmd = &cobra.Command{
203196
if err != nil {
204197
return err
205198
}
206-
output.Printf("✅ Plugin updated at %s\n", resolved.URL)
199+
output.Printf("✅ Plugin updated at %s\n", resolved.Path)
207200
return nil
208201
},
209202
}

cmd/themes.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,13 @@ var themesInfoCmd = &cobra.Command{
5151
Args: cobra.ExactArgs(1),
5252
RunE: func(cmd *cobra.Command, args []string) error {
5353
name := args[0]
54-
items, err := betterdiscord.ListAddons(betterdiscord.AddonTheme)
55-
if err != nil {
56-
return err
57-
}
58-
59-
for _, item := range items {
60-
// Match by filename or meta name
61-
if item.BaseName == name || item.FullFilename == name || item.Meta.Name == name {
62-
betterdiscord.LogLocalAddonInfo(&item)
63-
return nil
64-
}
65-
}
54+
existing := betterdiscord.FindAddon(betterdiscord.AddonTheme, name)
55+
if existing == nil {
56+
output.Printf("❌ Theme '%s' not found.\n", name)
57+
return nil
58+
}
6659

67-
output.Printf("❌ Theme '%s' not found.\n", name)
60+
betterdiscord.LogLocalAddonInfo(existing)
6861
return nil
6962
},
7063
}
@@ -91,7 +84,7 @@ var themesInstallCmd = &cobra.Command{
9184
if err != nil {
9285
return err
9386
}
94-
output.Printf("✅ Theme installed at %s\n", resolved.URL)
87+
output.Printf("✅ Theme installed at %s\n", resolved.Path)
9588
return nil
9689
},
9790
}
@@ -191,7 +184,7 @@ var themesUpdateCmd = &cobra.Command{
191184
if err != nil {
192185
return err
193186
}
194-
output.Printf("✅ Theme updated at %s\n", resolved.URL)
187+
output.Printf("✅ Theme updated at %s\n", resolved.Path)
195188
return nil
196189
},
197190
}

internal/betterdiscord/addons.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type AddonEntry struct {
3535
// ResolvedAddon holds both local and store metadata for an addon.
3636
type ResolvedAddon struct {
3737
Store *models.StoreAddon // Metadata from store (nil if not found)
38-
URL string // Download URL
38+
Path string // Local install path
3939
}
4040

4141
// ListAddons returns the locally installed addons for the given kind.
@@ -124,7 +124,7 @@ func InstallAddon(kind AddonKind, identifier string) (*ResolvedAddon, error) {
124124
if err != nil {
125125
return nil, err
126126
}
127-
resolved.URL = dest
127+
resolved.Path = dest
128128
return resolved, nil
129129
}
130130

@@ -152,7 +152,7 @@ func InstallAddon(kind AddonKind, identifier string) (*ResolvedAddon, error) {
152152
return nil, err
153153
}
154154

155-
resolved.URL = dest
155+
resolved.Path = dest
156156
LogAddonInfo(addon)
157157
return resolved, nil
158158
}
@@ -177,7 +177,10 @@ func RemoveAddon(kind AddonKind, identifier string) error {
177177

178178
// UpdateAddon removes then installs again, returning resolved metadata.
179179
func UpdateAddon(kind AddonKind, identifier string) (*ResolvedAddon, error) {
180-
_ = RemoveAddon(kind, identifier)
180+
err := RemoveAddon(kind, identifier)
181+
if err != nil {
182+
return nil, err
183+
}
181184
return InstallAddon(kind, identifier)
182185
}
183186

internal/betterdiscord/buildinfo.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package betterdiscord
22

33
import (
44
"bufio"
5+
"io"
56
"os"
67
"regexp"
78

@@ -65,7 +66,7 @@ func (i *BDInstall) ReadBuildinfo() (bi Buildinfo, err error) {
6566
var tail []byte
6667

6768
for {
68-
n, err := reader.Read(buf)
69+
n, readErr := reader.Read(buf)
6970
if n > 0 {
7071
// Combine tail + new chunk
7172
window := append(tail, buf[:n]...)
@@ -97,8 +98,11 @@ func (i *BDInstall) ReadBuildinfo() (bi Buildinfo, err error) {
9798
}
9899
}
99100

100-
if err != nil {
101-
break
101+
if readErr != nil {
102+
if readErr == io.EOF {
103+
break
104+
}
105+
return NewBuildinfo(), readErr
102106
}
103107
}
104108

internal/betterdiscord/store.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package betterdiscord
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"strconv"
78
"strings"
9+
"time"
810

911
"github.com/betterdiscord/cli/internal/models"
1012
"github.com/betterdiscord/cli/internal/output"
@@ -14,7 +16,7 @@ import (
1416
// FetchAddonFromStore queries the BetterDiscord Store API by name or ID.
1517
// Returns addon metadata including download URL.
1618
func FetchAddonFromStore(identifier string) (*models.StoreAddon, error) {
17-
apiURL := fmt.Sprintf("https://api.betterdiscord.app/v3/store/%s", identifier)
19+
apiURL := fmt.Sprintf("https://api.betterdiscord.app/v3/store/%s", url.PathEscape(identifier))
1820

1921
addon, err := utils.DownloadJSON[models.StoreAddon](apiURL)
2022
if err != nil {
@@ -37,6 +39,7 @@ func GetAddonDownloadURL(id int) (s string, err error) {
3739

3840
// Create client that follows redirects and returns the final URL
3941
client := &http.Client{
42+
Timeout: 10 * time.Second,
4043
CheckRedirect: func(req *http.Request, via []*http.Request) error {
4144
// Allow up to 10 redirects
4245
if len(via) >= 10 {
@@ -126,10 +129,19 @@ func ResolveAddonIdentifier(identifier string) (int, string, bool) {
126129
// FetchAddonsOfType fetches all addons of a specific type from the store.
127130
// Kind can be "plugin", "theme", or "addon" for all types.
128131
func FetchAddonsOfType(kind string) ([]models.StoreAddon, error) {
129-
endpoint := kind
130-
if kind == "" {
131-
endpoint = "addons"
132-
}
132+
k := strings.ToLower(kind)
133+
134+
var endpoint string
135+
switch k {
136+
case "", "addon", "addons":
137+
endpoint = "addons"
138+
case "plugin", "plugins":
139+
endpoint = "plugins"
140+
case "theme", "themes":
141+
endpoint = "themes"
142+
default:
143+
return nil, fmt.Errorf("invalid addon kind %q (expected plugin[s], theme[s], or addon[s])", kind)
144+
}
133145
apiURL := fmt.Sprintf("https://api.betterdiscord.app/v3/store/%s", endpoint)
134146

135147
addons, err := utils.DownloadJSON[[]models.StoreAddon](apiURL)

internal/output/output.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ var (
1717
)
1818

1919
// SetWriters overrides the output writers (useful for tests).
20-
func SetWriters(out, err io.Writer) {
21-
if out != nil {
22-
stdOut = out
20+
func SetWriters(stdout, stderr io.Writer) {
21+
if stdout != nil {
22+
stdOut = stdout
2323
}
24-
if err != nil {
25-
stdErr = err
24+
if stderr != nil {
25+
stdErr = stderr
2626
}
2727
}
2828

0 commit comments

Comments
 (0)