Skip to content

Commit e6d5209

Browse files
committed
#10 Complete Fetch ALL
1 parent b1d0c2d commit e6d5209

16 files changed

+6692
-6
lines changed

ai/Shubhamsaboo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
404: Not Found

ai/e2b-dev.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
404: Not Found

ai/hyp1231.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
404: Not Found

awesome.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"awesomes": [
3+
{
4+
"category": "build-x",
5+
"github_url": " https://github.com/codecrafters-io/build-your-own-x.git"
6+
},
7+
{
8+
"category": "ai",
9+
"github_url": "https://github.com/hyp1231/awesome-llm-powered-agent.git"
10+
},
11+
{
12+
"category": "ai",
13+
"github_url": "https://github.com/Shubhamsaboo/awesome-llm-apps.git"
14+
},
15+
{
16+
"category": "ai",
17+
"github_url": "https://github.com/e2b-dev/awesome-ai-agents.git"
18+
}
19+
],
20+
"outputDir": "/Users/patrick/workspace/personal/fluent-stacks/goway/qgops/docs/awesome"
21+
}

cmd/actions/awesome.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package actions
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
"log/slog"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/samber/lo"
12+
13+
"github.com/imroc/req/v3"
14+
15+
"github.com/fluent-qa/qgops/internal/utils/jsonutil"
16+
_ "github.com/fluent-qa/qgops/internal/utils/qhttp"
17+
)
18+
19+
var (
20+
FetchAllCmd = &cobra.Command{
21+
Use: "awesome-all",
22+
Short: "Fetch all awesome repositories",
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return FetchAllAwesome()
25+
},
26+
}
27+
28+
AddURLCmd = &cobra.Command{
29+
Use: "awesome-add <category> <github-url>",
30+
Short: "Add a new awesome repository URL",
31+
Args: cobra.ExactArgs(2),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return AddAwesomeURL(args[0], args[1])
34+
},
35+
}
36+
37+
FetchByCategoryCmd = &cobra.Command{
38+
Use: "awesome <category>",
39+
Short: "Fetch awesome repositories by category",
40+
Args: cobra.ExactArgs(1),
41+
Run: func(cmd *cobra.Command, args []string) {
42+
FetchAwesomeByCategory(args[0])
43+
},
44+
}
45+
)
46+
47+
// TODO: move to sqlite database instead of a configuration file
48+
49+
type AwesomeRepo struct {
50+
Category string `json:"category"`
51+
GithubURL string `json:"github_url"`
52+
}
53+
54+
type AwesomeRepos struct {
55+
Awesomes []AwesomeRepo `json:"awesomes,omitempty"`
56+
OutputDir string `json:"outputDir,omitempty"`
57+
}
58+
59+
func FetchAllAwesome() error {
60+
repos, _ := readConfigFile(AwesomeRepoConfig)
61+
lo.ForEach(repos.Awesomes, func(repo AwesomeRepo, index int) {
62+
_ = writeToLocalFolder(repos.OutputDir, repo)
63+
})
64+
return nil
65+
}
66+
67+
func AddAwesomeURL(category, githubUrl string) error {
68+
repos, _ := readConfigFile(AwesomeRepoConfig)
69+
repos.Awesomes = append(repos.Awesomes, AwesomeRepo{
70+
Category: category, GithubURL: githubUrl,
71+
})
72+
newConfig := jsonutil.ToString(repos)
73+
_ = os.WriteFile(AwesomeRepoConfig, []byte(newConfig), os.ModePerm.Perm())
74+
slog.Info("Add new awesome {category},{url}", category, githubUrl)
75+
return nil
76+
}
77+
78+
func FetchAwesomeByCategory(category string) {
79+
repos, _ := readConfigFile(AwesomeRepoConfig)
80+
categoryRepos := lo.Filter(repos.Awesomes, func(item AwesomeRepo, index int) bool {
81+
if item.Category == category {
82+
return true
83+
}
84+
return false
85+
})
86+
lo.ForEach(categoryRepos, func(repo AwesomeRepo, index int) {
87+
_ = writeToLocalFolder(repos.OutputDir, repo)
88+
})
89+
}
90+
91+
func readConfigFile(filePath string) (*AwesomeRepos, error) {
92+
awesomeRepoConfig := &AwesomeRepos{}
93+
jsonutil.ToStructFromFile(filePath, awesomeRepoConfig)
94+
return awesomeRepoConfig, nil
95+
}
96+
97+
func writeToLocalFolder(outputDirs string, repo AwesomeRepo) error {
98+
githubReadMeURL, repoName := convertToRawReadMeURL(repo.GithubURL)
99+
resp, err := req.C().R().Get(githubReadMeURL)
100+
//https://raw.githubusercontent.com/hyp1231/awesome-llm-powered-agent/refs/heads/main/README.md
101+
//https://raw.githubusercontent.com/hyp1231/awesome-llm-powered-agent/refs/heads/master/README.md
102+
if resp.StatusCode == 404 {
103+
resp, err = req.C().R().Get(strings.ReplaceAll(githubReadMeURL, "/main/", "/master/"))
104+
}
105+
if err != nil {
106+
return fmt.Errorf("failed to Get ReadMe file: %v", err)
107+
}
108+
// Create directory
109+
dirPath := filepath.Join(outputDirs, repo.Category)
110+
err = os.MkdirAll(dirPath, 0755)
111+
if err != nil {
112+
return fmt.Errorf("failed to create directory: %v", err)
113+
}
114+
115+
// Create file name
116+
fileName := repoName + ".md"
117+
readMeFilePath := filepath.Join(dirPath, fileName)
118+
file, err := os.OpenFile(readMeFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
119+
if err != nil {
120+
return err
121+
}
122+
defer func(file *os.File) {
123+
_ = file.Close()
124+
}(file)
125+
126+
if _, err = file.WriteString(string(resp.String())); err != nil {
127+
return err
128+
}
129+
return nil
130+
}
131+
132+
func convertToRawReadMeURL(githubUrl string) (string, string) {
133+
// Remove .git from the end if present
134+
githubUrl = strings.ReplaceAll(githubUrl, ".git", "")
135+
parts := strings.Split(githubUrl, "/")
136+
repoName := ""
137+
if len(parts) >= 2 {
138+
repoName = parts[len(parts)-1]
139+
}
140+
githubUrl = strings.ReplaceAll(githubUrl, " ", "")
141+
// Replace github.com with raw.githubusercontent.com
142+
rawURL := strings.Replace(githubUrl, "github.com", "raw.githubusercontent.com", 1)
143+
// Add /master/README.md to the end
144+
rawURL += "/refs/heads/main/README.md"
145+
146+
return rawURL, repoName
147+
}

cmd/actions/awesome.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"awesomes": [
3+
{
4+
"category": "build-x",
5+
"github_url": " https://github.com/codecrafters-io/build-your-own-x.git"
6+
},
7+
{
8+
"category": "ai",
9+
"github_url": "https://github.com/hyp1231/awesome-llm-powered-agent.git"
10+
},
11+
{
12+
"category": "ai",
13+
"github_url": "https://github.com/Shubhamsaboo/awesome-llm-apps.git"
14+
},
15+
{
16+
"category": "ai",
17+
"github_url": "https://github.com/e2b-dev/awesome-ai-agents.git"
18+
}
19+
],
20+
"outputDir": "/Users/patrick/workspace/personal/fluent-stacks/goway/qgops/docs/awesome"
21+
}

cmd/actions/awesome_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package actions
2+
3+
import (
4+
"fmt"
5+
"github.com/samber/lo"
6+
"log/slog"
7+
"testing"
8+
)
9+
10+
func Test_readConfigFile(t *testing.T) {
11+
12+
got, err := readConfigFile("awesome.json")
13+
if err != nil {
14+
t.Error(err)
15+
}
16+
fmt.Println(got)
17+
}
18+
19+
func Test_writeToLocal(t *testing.T) {
20+
got, err := readConfigFile("awesome.json")
21+
if err != nil {
22+
t.Error(err)
23+
}
24+
lo.ForEach(got.Awesomes, func(item AwesomeRepo, index int) {
25+
err := writeToLocalFolder("awesome", item)
26+
if err != nil {
27+
slog.Error(err.Error())
28+
}
29+
})
30+
}
31+
32+
func Test_AddUrl(t *testing.T) {
33+
AddAwesomeURL("ai", "https://github.com/hyp1231/awesome-llm-powered-agent.git")
34+
}
35+
36+
func Test_fetch_All(t *testing.T) {
37+
FetchAllAwesome()
38+
}

cmd/actions/constants.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ package actions
22

33
import "os"
44

5-
var WorkSpaceDir = os.Getenv("FLUENT_HOME")
5+
var (
6+
WorkSpaceDir = os.Getenv("FLUENT_HOME")
7+
AwesomeRepoConfig = "awesome.json"
8+
)

cmd/actions/starter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package actions
22

33
import (
4+
"github.com/fluent-qa/qgops/internal/utils/shell"
5+
"github.com/spf13/cobra"
46
_ "log/slog"
57
"path"
68
"strings"
7-
8-
"github.com/fluent-qa/qgops/internal/utils/shell"
9-
"github.com/spf13/cobra"
109
)
1110

1211
var starterActions = shell.LoadCommands(path.Join(WorkSpaceDir, "starters.json"))

cmd/fluent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ func init() {
99
base.CmdRoot.AddCommand(actions.StarterCmd)
1010
base.CmdRoot.AddCommand(actions.IssueCmd)
1111
base.CmdRoot.AddCommand(actions.UtilCmd)
12+
base.CmdRoot.AddCommand(actions.FetchAllCmd)
13+
base.CmdRoot.AddCommand(actions.AddURLCmd)
14+
base.CmdRoot.AddCommand(actions.FetchByCategoryCmd)
1215
}
1316
func main() {
1417
_ = base.CmdRoot.Execute()

0 commit comments

Comments
 (0)