github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/actors/plugin_repo/plugin_repo.go (about) 1 package plugin_repo 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 10 clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/models" 11 "github.com/cloudfoundry/cli/cf/models" 12 13 . "github.com/cloudfoundry/cli/cf/i18n" 14 ) 15 16 type PluginRepo interface { 17 GetPlugins([]models.PluginRepo) (map[string][]clipr.Plugin, []string) 18 } 19 20 type pluginRepo struct{} 21 22 func NewPluginRepo() PluginRepo { 23 return pluginRepo{} 24 } 25 26 func (r pluginRepo) GetPlugins(repos []models.PluginRepo) (map[string][]clipr.Plugin, []string) { 27 var pluginList clipr.PluginsJson 28 repoError := []string{} 29 repoPlugins := make(map[string][]clipr.Plugin) 30 31 for _, repo := range repos { 32 resp, err := http.Get(getListEndpoint(repo.Url)) 33 if err != nil { 34 repoError = append(repoError, fmt.Sprintf(T("Error requesting from")+" '%s' - %s", repo.Name, err.Error())) 35 continue 36 } else { 37 defer resp.Body.Close() 38 39 body, err := ioutil.ReadAll(resp.Body) 40 if err != nil { 41 repoError = append(repoError, fmt.Sprintf(T("Error reading response from")+" '%s' - %s ", repo.Name, err.Error())) 42 continue 43 } 44 45 pluginList = clipr.PluginsJson{Plugins: nil} 46 err = json.Unmarshal(body, &pluginList) 47 if err != nil { 48 repoError = append(repoError, fmt.Sprintf(T("Invalid json data from")+" '%s' - %s", repo.Name, err.Error())) 49 continue 50 } else if pluginList.Plugins == nil { 51 repoError = append(repoError, T("Invalid data from '{{.repoName}}' - plugin data does not exist", map[string]interface{}{"repoName": repo.Name})) 52 continue 53 } 54 55 } 56 57 repoPlugins[repo.Name] = pluginList.Plugins 58 } 59 60 return repoPlugins, repoError 61 } 62 63 func getListEndpoint(url string) string { 64 if strings.HasSuffix(url, "/") { 65 return url + "list" 66 } 67 return url + "/list" 68 }