(repos []models.PluginRepo)
| 26 | } |
| 27 | |
| 28 | func (r pluginRepo) GetPlugins(repos []models.PluginRepo) (map[string][]clipr.Plugin, []string) { |
| 29 | var pluginList clipr.PluginsJson |
| 30 | var repoError []string |
| 31 | repoPlugins := make(map[string][]clipr.Plugin) |
| 32 | |
| 33 | for _, repo := range repos { |
| 34 | // resp, err := http.Get(getListEndpoint(repo.URL)) |
| 35 | client := &http.Client{} |
| 36 | req, err := http.NewRequest("GET", getListEndpoint(repo.URL), nil) |
| 37 | if err != nil { |
| 38 | repoError = append(repoError, fmt.Sprintf(T("Error requesting from")+" '%s' - %s", repo.Name, err.Error())) |
| 39 | continue |
| 40 | } |
| 41 | |
| 42 | req.Header.Set("User-Agent", "golang_user_agent/1.0") |
| 43 | resp, err := client.Do(req) |
| 44 | if err != nil { |
| 45 | repoError = append(repoError, fmt.Sprintf(T("Error requesting from")+" '%s' - %s", repo.Name, err.Error())) |
| 46 | continue |
| 47 | } else { |
| 48 | defer func(Body io.ReadCloser) { |
| 49 | err := Body.Close() |
| 50 | if err != nil { |
| 51 | repoError = append(repoError, fmt.Sprintf(T("Error closing body")+" '%s' - %s", repo.Name, err.Error())) |
| 52 | } |
| 53 | }(resp.Body) |
| 54 | |
| 55 | body, err := io.ReadAll(resp.Body) |
| 56 | if err != nil { |
| 57 | repoError = append(repoError, fmt.Sprintf(T("Error reading response from")+" '%s' - %s ", repo.Name, err.Error())) |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | pluginList = clipr.PluginsJson{Plugins: nil} |
| 62 | err = json.Unmarshal(body, &pluginList) |
| 63 | if err != nil { |
| 64 | repoError = append(repoError, fmt.Sprintf(T("Invalid json data from")+" '%s' - %s", repo.Name, err.Error())) |
| 65 | continue |
| 66 | } else if pluginList.Plugins == nil { |
| 67 | repoError = append(repoError, T("Invalid data from '{{.repoName}}' - plugin data does not exist", map[string]interface{}{"repoName": repo.Name})) |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | repoPlugins[repo.Name] = pluginList.Plugins |
| 74 | } |
| 75 | |
| 76 | return repoPlugins, repoError |
| 77 | } |
| 78 | |
| 79 | func getListEndpoint(url string) string { |
| 80 | if strings.HasSuffix(url, "/") { |
nothing calls this directly
no test coverage detected