Skip to content

Commit 7297108

Browse files
authored
Update repo.go
1 parent 445c09c commit 7297108

File tree

1 file changed

+28
-56
lines changed

1 file changed

+28
-56
lines changed

src/repo.go

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
65
"io"
76
"net/http"
87
"os"
98
"path/filepath"
10-
"strings"
9+
10+
"gopkg.in/yaml.v3"
1111
)
1212

1313
// Repo structure: map[section]map[category]map[name]url
1414
type Repo map[string]map[string]map[string]string
1515

16-
const (
17-
repoURL = "https://raw.githubusercontent.com/Bytes-Repository/bytes.io/main/repository/bytes.io"
18-
)
19-
20-
func refreshRepo(localPath string) error {
21-
resp, err := http.Get(repoURL)
16+
func refreshRepo(url, localPath string) error {
17+
resp, err := http.Get(url)
2218
if err != nil {
2319
return err
2420
}
@@ -40,65 +36,41 @@ func refreshRepo(localPath string) error {
4036
}
4137

4238
func parseRepo(path string) (Repo, error) {
43-
f, err := os.Open(path)
39+
data, err := os.ReadFile(path)
4440
if err != nil {
4541
return nil, err
4642
}
47-
defer f.Close()
43+
var raw map[string]interface{}
44+
if err := yaml.Unmarshal(data, &raw); err != nil {
45+
return nil, err
46+
}
4847
repo := make(Repo)
49-
var currentSection string
50-
var currentCategory string
51-
scanner := bufio.NewScanner(f)
52-
for scanner.Scan() {
53-
fullLine := scanner.Text()
54-
line := strings.TrimSpace(fullLine)
55-
if line == "" {
48+
for section, secVal := range raw {
49+
secMap, ok := secVal.(map[string]interface{})
50+
if !ok {
5651
continue
5752
}
58-
indentLevel := len(fullLine) - len(line)
59-
if strings.HasPrefix(line, "=") && strings.Contains(line, "[") {
60-
parts := strings.SplitN(line, " [", 2)
61-
name := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(parts[0]), "="))
62-
if indentLevel == 0 {
63-
// New section
64-
repo[name] = make(map[string]map[string]string)
65-
currentSection = name
66-
currentCategory = ""
67-
} else {
68-
// New category
69-
if currentSection != "" {
70-
repo[currentSection][name] = make(map[string]string)
71-
currentCategory = name
72-
}
73-
}
74-
} else if strings.Contains(line, "=>") {
75-
// Package
76-
parts := strings.SplitN(line, "=>", 2)
77-
name := strings.TrimSpace(parts[0])
78-
url := ""
79-
if len(parts) > 1 {
80-
url = strings.TrimSpace(parts[1])
53+
repo[section] = make(map[string]map[string]string)
54+
for category, catVal := range secMap {
55+
if catVal == nil {
56+
repo[section][category] = make(map[string]string)
57+
continue
8158
}
82-
if name == "" {
59+
catMap, ok := catVal.(map[string]interface{})
60+
if !ok {
8361
continue
8462
}
85-
if currentSection != "" && currentCategory != "" {
86-
repo[currentSection][currentCategory][name] = url
87-
} else if currentSection != "" {
88-
// Direct package in section
89-
if _, ok := repo[currentSection][""]; !ok {
90-
repo[currentSection][""] = make(map[string]string)
63+
repo[section][category] = make(map[string]string)
64+
for name, urlVal := range catMap {
65+
url := ""
66+
if urlVal != nil {
67+
if s, ok := urlVal.(string); ok {
68+
url = s
69+
}
9170
}
92-
repo[currentSection][""][name] = url
93-
}
94-
} else if line == "]" {
95-
// End of category or section
96-
if indentLevel > 0 {
97-
currentCategory = ""
98-
} else {
99-
currentSection = ""
71+
repo[section][category][name] = url
10072
}
10173
}
10274
}
103-
return repo, scanner.Err()
75+
return repo, nil
10476
}

0 commit comments

Comments
 (0)