Beispiel #1
0
func walkPkgsinfo(pkgsinfos *models.PkgsInfoCollection, pkgsinfoPath string) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()
		if !info.IsDir() {
			// Decode pkgsinfo
			pkgsinfo := &models.PkgsInfo{}
			err := plist.NewDecoder(file).Decode(pkgsinfo)
			if err != nil {
				log.Printf("simple-repo: failed to decode %v, skipping \n", info.Name())
				return nil
			}
			// set filename to relative path
			relpath, err := filepath.Rel(pkgsinfoPath, path)
			if err != nil {
				log.Printf("simple-repo: failed to get relative path %v, skipping \n", info.Name())
				return err
			}
			// use the relative path as the filename
			pkgsinfo.Filename = relpath
			// add to ManifestCollection
			*pkgsinfos = append(*pkgsinfos, pkgsinfo)
			return nil
		}
		return nil
	}
}
Beispiel #2
0
func walkManifests(manifests *models.ManifestCollection) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()
		if !info.IsDir() {
			// Decode manifest
			manifest := &models.Manifest{}
			err := plist.NewDecoder(file).Decode(manifest)
			if err != nil {
				log.Printf("git-repo: failed to decode %v, skipping \n", info.Name())
				return nil
			}
			// set filename to relative path + filename
			manifest.Filename = info.Name()
			// add to ManifestCollection
			*manifests = append(*manifests, manifest)
			return nil
		}
		return nil
	}
}
Beispiel #3
0
func readReportPlist(path string) (autopkgReport, error) {
	r := autopkgReport{}
	f, err := os.Open(path)
	if err != nil {
		return r, err
	}
	defer f.Close()
	return r, plist.NewDecoder(f).Decode(&r)
}
Beispiel #4
0
// decode an http request into a correct model type
func decodeRequest(r *http.Request, into interface{}) error {
	contentType := contentHeader(r)
	var err error
	switch contentType {
	case "application/xml", "application/xml; charset=utf-8":
		err = plist.NewDecoder(r.Body).Decode(into)
	case "application/json":
		err = json.NewDecoder(r.Body).Decode(into)
	default:
		err = fmt.Errorf("Incorrect Content-Type: %v", contentType)
	}
	return err
}
Beispiel #5
0
func TestManifestsShow(t *testing.T) {
	f := func(accept string) {
		req, err := client.NewRequest("manifests", "site_default", accept, "GET")
		if err != nil {
			t.Fatal(err)
		}
		setHeader(req, accept)
		resp, err := client.Do(req, nil)
		if err != nil {
			t.Fatal(err)
		}

		// check that xml and json are returned when an accept header is passed
		contentType := resp.Header.Get("Content-Type")
		if contentType != accept {
			t.Error("Expected", accept, "got", contentType)
		}

		// check that the API returns 200 OK
		if resp.StatusCode != 200 {
			t.Fatal("Expected", 200, "got", resp.StatusCode)
		}

		// check decoded response
		manifest := &models.Manifest{}
		switch accept {
		case xmlMedia:
			err = plist.NewDecoder(resp.Body).Decode(manifest)
		case jsonMedia:
			err = json.NewDecoder(resp.Body).Decode(manifest)
		}
		if err != nil {
			t.Fatal(err)
		}
		if manifest.Catalogs[0] != "production" {
			t.Error("Expected", "production", "got", manifest.Catalogs[0])
		}

	}

	f(xmlMedia)
	f(jsonMedia)
}