func configMulti(c *Command, r *http.Request) Response { route := c.d.router.Get(operationCmd.Path) if route == nil { return InternalError(nil, "router can't find route for operation") } decoder := json.NewDecoder(r.Body) var pkgmap map[string]string if err := decoder.Decode(&pkgmap); err != nil { return BadRequest(err, "can't decode request body into map[string]string: %v", err) } return AsyncResponse(c.d.AddTask(func() interface{} { lock, err := lockfile.Lock(dirs.SnapLockFile, true) if err != nil { return err } defer lock.Unlock() rspmap := make(map[string]*configSubtask, len(pkgmap)) bags := lightweight.AllPartBags() for pkg, cfg := range pkgmap { out := errorResult{} sub := configSubtask{Status: TaskFailed, Output: &out} rspmap[pkg] = &sub bag, ok := bags[pkg] if !ok { out.Str = snappy.ErrPackageNotFound.Error() out.Obj = snappy.ErrPackageNotFound continue } part, _ := bag.Load(bag.ActiveIndex()) if part == nil { out.Str = snappy.ErrSnapNotActive.Error() out.Obj = snappy.ErrSnapNotActive continue } config, err := part.Config([]byte(cfg)) if err != nil { out.Msg = "Config failed" out.Str = err.Error() out.Obj = err continue } sub.Status = TaskSucceeded sub.Output = config } return rspmap }).Map(route)) }
func ExamplePartBag() { d, _ := ioutil.TempDir("", "test-xyzzy-") defer os.RemoveAll(d) dirs.SetRootDir(d) os.MkdirAll(filepath.Join(dirs.SnapDataDir, "foo.bar", "0.1"), 0755) os.MkdirAll(filepath.Join(dirs.SnapDataDir, "foo.bar", "0.2"), 0755) os.MkdirAll(filepath.Join(dirs.SnapDataDir, "foo.bar", "0.5"), 0755) os.MkdirAll(filepath.Join(dirs.SnapDataDir, "baz", "0.4"), 0755) os.MkdirAll(filepath.Join(dirs.SnapDataDir, "qux", "0.5"), 0755) os.MkdirAll(filepath.Join(dirs.SnapOemDir, "qux", "0.5"), 0755) bags := lightweight.AllPartBags() for _, k := range []string{"foo.bar", "baz", "qux"} { bag := bags[k] fmt.Printf("Found %d versions for %s, type %q: %s\n", len(bag.Versions), bag.QualifiedName(), bag.Type, bag.Versions) } // Output: // Found 3 versions for foo.bar, type "app": [0.5 0.2 0.1] // Found 1 versions for baz, type "framework": [0.4] // Found 1 versions for qux, type "oem": [0.5] }
// 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), }, }) }