|
| 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 | +} |
0 commit comments