func packagesToKeys(packages []map[string]string) []aptly_model.Key { latestVersions := make(map[string]map[string]string) var keys []aptly_model.Key for _, currentPackage := range packages { glog.V(2).Infof("handle package %s %s", currentPackage["Package"], currentPackage["Version"]) name := currentPackage["Package"] if latestPackage, ok := latestVersions[name]; ok { var packageToDelete map[string]string glog.V(4).Infof("compare %s < %s", currentPackage["Version"], latestPackage["Version"]) if aptly_version.LessThan(aptly_version.Version(currentPackage["Version"]), aptly_version.Version(latestPackage["Version"])) { packageToDelete = currentPackage } else { glog.V(4).Infof("set latest version %s %s", currentPackage["Package"], currentPackage["Version"]) latestVersions[name] = currentPackage packageToDelete = latestPackage } keys = append(keys, aptly_model.Key(packageToDelete["Key"])) glog.V(2).Infof("mark package %s %s to delete", packageToDelete["Package"], packageToDelete["Version"]) } else { latestVersions[name] = currentPackage } } for _, p := range latestVersions { glog.V(2).Infof("keep package %s %s", p["Package"], p["Version"]) } return keys }
func (p *packageDeleter) findKeys(api aptly_model.API, repository aptly_model.Repository, packageName aptly_model.Package, version aptly_version.Version) ([]aptly_model.Key, error) { glog.V(2).Infof("PackageVersions - repo: %s package: %s", repository, packageName) url := fmt.Sprintf("%s/api/repos/%s/packages?format=details", api.APIUrl, repository) requestbuilder := p.newHTTPRequestBuilder(url) requestbuilder.AddBasicAuth(string(api.APIUsername), string(api.APIPassword)) requestbuilder.SetMethod("GET") requestbuilder.AddContentType("application/json") req, err := requestbuilder.Build() if err != nil { return nil, err } resp, err := p.executeRequest(req) if err != nil { return nil, err } content, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode/100 != 2 { return nil, fmt.Errorf("request to %s failed with status %d", url, resp.StatusCode) } var jsonStruct JSONStruct err = json.Unmarshal(content, &jsonStruct) if err != nil { return nil, err } var keys []aptly_model.Key for _, info := range jsonStruct { if info["Package"] == string(packageName) && info["Version"] == string(version) { key := info["Key"] glog.V(2).Infof("found key: %s", key) keys = append(keys, aptly_model.Key(key)) } } return keys, nil }