コード例 #1
0
ファイル: api.go プロジェクト: pombredanne/snappy-1
func (inst *packageInstruction) update() interface{} {
	// zomg :-(
	// TODO: query the store for just this package, instead of this

	flags := snappy.DoInstallGC
	if inst.LeaveOld {
		flags = 0
	}

	parts, err := snappy.ListUpdates()
	if err != nil {
		return err
	}

	for _, part := range parts {
		if snappy.QualifiedName(part) == inst.pkg {
			if _, err := part.Install(inst.prog, flags); err != nil {
				return err
			}
			return snappy.GarbageCollect(inst.pkg, flags, inst.prog)
		}
	}

	return "package is up to date"
}
コード例 #2
0
ファイル: api.go プロジェクト: alecu/snappy
// plural!
func getSnapsInfo(c *Command, r *http.Request) Response {
	route := c.d.router.Get(snapCmd.Path)
	if route == nil {
		return InternalError("router can't find route for snaps")
	}

	lock, err := lockfile.Lock(dirs.SnapLockFile, true)
	if err != nil {
		return InternalError("unable to acquire lock: %v", err)
	}
	defer lock.Unlock()

	// TODO: Marshal incrementally leveraging json.RawMessage.
	results := make(map[string]map[string]interface{})
	sources := make([]string, 0, 2)
	query := r.URL.Query()

	var includeStore, includeLocal bool
	if len(query["sources"]) > 0 {
		for _, v := range strings.Split(query["sources"][0], ",") {
			if v == "store" {
				includeStore = true
			} else if v == "local" {
				includeLocal = true
			}
		}
	} else {
		includeStore = true
		includeLocal = true
	}

	searchTerm := query.Get("q")

	var includeTypes []string
	if len(query["types"]) > 0 {
		includeTypes = strings.Split(query["types"][0], ",")
	}

	var bags map[string]*lightweight.PartBag

	if includeLocal {
		sources = append(sources, "local")
		bags = lightweight.AllPartBags()

		for _, v := range bags {
			m := v.Map(nil)
			name, _ := m["name"].(string)
			origin, _ := m["origin"].(string)

			resource := "no resource URL for this resource"
			url, err := route.URL("name", name, "origin", origin)
			if err == nil {
				resource = url.String()
			}

			fullname := name + "." + origin

			// strings.Contains(fullname, "") is true
			if !strings.Contains(fullname, searchTerm) {
				continue
			}

			results[fullname] = webify(m, resource)
		}
	}

	if includeStore {
		repo := newRemoteRepo()
		var found []snappy.Part

		// repo.Find("") finds all
		//
		// TODO: Instead of ignoring the error from Find:
		//   * if there are no results, return an error response.
		//   * If there are results at all (perhaps local), include a
		//     warning in the response
		found, _ = repo.Find(searchTerm)

		sources = append(sources, "store")

		sort.Sort(byQN(found))

		for _, part := range found {
			name := part.Name()
			origin := part.Origin()

			url, err := route.URL("name", name, "origin", origin)
			if err != nil {
				return InternalError("can't get route to details for %s.%s: %v", name, origin, err)
			}

			fullname := name + "." + origin
			qn := snappy.QualifiedName(part)
			results[fullname] = webify(bags[qn].Map(part), url.String())
		}
	}

	// TODO: it should be possible to search on	the "content" field on the store
	//       with multiple values, see:
	//       https://wiki.ubuntu.com/AppStore/Interfaces/ClickPackageIndex#Search
	if len(includeTypes) > 0 {
		for name, result := range results {
			if !resultHasType(result, includeTypes) {
				delete(results, name)
			}
		}
	}

	return SyncResponse(map[string]interface{}{
		"snaps":   results,
		"sources": sources,
		"paging": map[string]interface{}{
			"pages": 1,
			"page":  1,
			"count": len(results),
		},
	})
}
コード例 #3
0
ファイル: api.go プロジェクト: alecu/snappy
func (ps byQN) Less(a, b int) bool {
	return snappy.QualifiedName(ps[a]) < snappy.QualifiedName(ps[b])
}
コード例 #4
0
ファイル: api.go プロジェクト: robert-ancell/snapd
// plural!
func getSnapsInfo(c *Command, r *http.Request) Response {
	route := c.d.router.Get(snapCmd.Path)
	if route == nil {
		return InternalError("router can't find route for snaps")
	}

	lock, err := lockfile.Lock(dirs.SnapLockFile, true)
	if err != nil {
		return InternalError("unable to acquire lock: %v", err)
	}
	defer lock.Unlock()

	// TODO: Marshal incrementally leveraging json.RawMessage.
	results := make(map[string]map[string]interface{})
	sources := make([]string, 0, 2)
	query := r.URL.Query()

	var includeStore, includeLocal bool
	if len(query["sources"]) > 0 {
		for _, v := range strings.Split(query["sources"][0], ",") {
			if v == "store" {
				includeStore = true
			} else if v == "local" {
				includeLocal = true
			}
		}
	} else {
		includeStore = true
		includeLocal = true
	}

	var bags map[string]*lightweight.PartBag

	if includeLocal {
		sources = append(sources, "local")
		bags = lightweight.AllPartBags()

		for _, v := range bags {
			m := v.Map(nil)
			name, _ := m["name"].(string)
			origin, _ := m["origin"].(string)

			resource := "no resource URL for this resource"
			url, err := route.URL("name", name, "origin", origin)
			if err == nil {
				resource = url.String()
			}

			results[name+"."+origin] = webify(m, resource)
		}
	}

	if includeStore {
		// TODO: If there are no results (local or remote), report the error. If
		//       there are results at all, inform that the result is partial.
		found, _ := newRemoteRepo().All()
		if len(found) > 0 {
			sources = append(sources, "store")
		}

		sort.Sort(byQN(found))

		for _, part := range found {
			name := part.Name()
			origin := part.Origin()

			url, err := route.URL("name", name, "origin", origin)
			if err != nil {
				return InternalError("can't get route to details for %s.%s: %v", name, origin, err)
			}

			fullname := name + "." + origin
			qn := snappy.QualifiedName(part)
			results[fullname] = webify(bags[qn].Map(part), url.String())
		}
	}

	return SyncResponse(map[string]interface{}{
		"snaps":   results,
		"sources": sources,
		"paging": map[string]interface{}{
			"pages": 1,
			"page":  1,
			"count": len(results),
		},
	})
}
コード例 #5
0
ファイル: api.go プロジェクト: pombredanne/snappy-1
// plural!
func getPackagesInfo(c *Command, r *http.Request) Response {
	route := c.d.router.Get(packageCmd.Path)
	if route == nil {
		return InternalError(nil, "router can't find route for packages")
	}

	sources := make([]string, 1, 3)
	sources[0] = "local"
	// we're not worried if the remote repos error out
	found, _ := newRemoteRepo().All()
	if len(found) > 0 {
		sources = append(sources, "store")
	}

	upd, _ := newSystemRepo().Updates()
	if len(upd) > 0 {
		sources = append(sources, "system-image")
	}

	found = append(found, upd...)

	sort.Sort(byQN(found))

	bags := lightweight.AllPartBags()

	results := make(map[string]map[string]string)
	for _, part := range found {
		name := part.Name()
		origin := part.Origin()

		url, err := route.URL("name", name, "origin", origin)
		if err != nil {
			return InternalError(err, "can't get route to details for %s.%s: %v", name, origin, err)
		}

		fullname := name + "." + origin
		qn := snappy.QualifiedName(part)
		results[fullname] = webify(bags[qn].Map(part), url.String())
		delete(bags, qn)
	}

	for _, v := range bags {
		m := v.Map(nil)
		name := m["name"]
		origin := m["origin"]

		resource := "no resource URL for this resource"
		url, _ := route.URL("name", name, "origin", origin)
		if url != nil {
			resource = url.String()
		}

		results[name+"."+origin] = webify(m, resource)
	}

	return SyncResponse(map[string]interface{}{
		"packages": results,
		"sources":  sources,
		"paging": map[string]interface{}{
			"pages": 1,
			"page":  1,
			"count": len(results),
		},
	})
}