func aptlySnapshotMerge(cmd *commander.Command, args []string) error { var err error if len(args) < 2 { cmd.Usage() return commander.ErrCommandError } sources := make([]*deb.Snapshot, len(args)-1) for i := 0; i < len(args)-1; i++ { sources[i], err = context.CollectionFactory().SnapshotCollection().ByName(args[i+1]) if err != nil { return fmt.Errorf("unable to load snapshot: %s", err) } err = context.CollectionFactory().SnapshotCollection().LoadComplete(sources[i]) if err != nil { return fmt.Errorf("unable to load snapshot: %s", err) } } latest := context.Flags().Lookup("latest").Value.Get().(bool) noRemove := context.Flags().Lookup("no-remove").Value.Get().(bool) if noRemove && latest { return fmt.Errorf("-no-remove and -latest can't be specified together") } overrideMatching := !latest && !noRemove result := sources[0].RefList() for i := 1; i < len(sources); i++ { result = result.Merge(sources[i].RefList(), overrideMatching, false) } if latest { result.FilterLatestRefs() } sourceDescription := make([]string, len(sources)) for i, s := range sources { sourceDescription[i] = fmt.Sprintf("'%s'", s.Name) } // Create <destination> snapshot destination := deb.NewSnapshotFromRefList(args[0], sources, result, fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", "))) err = context.CollectionFactory().SnapshotCollection().Add(destination) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } fmt.Printf("\nSnapshot %s successfully created.\nYou can run 'aptly publish snapshot %s' to publish snapshot as Debian repository.\n", destination.Name, destination.Name) return err }
func (a *Api) SnapshotCreate(snapshotName, description string, sourceSnapshots []string, packageRefs []string) (*deb.Snapshot, error) { var err error snapshotCollection := a.Ctx().CollectionFactory().SnapshotCollection() snapshotCollection.Lock() defer snapshotCollection.Unlock() sources := make([]*deb.Snapshot, len(sourceSnapshots)) for i := range sourceSnapshots { sources[i], err = snapshotCollection.ByName(sourceSnapshots[i]) if err != nil { return nil, err } err = snapshotCollection.LoadComplete(sources[i]) if err != nil { return nil, err } } list := deb.NewPackageList() // verify package refs and build package list for _, ref := range packageRefs { var p *deb.Package p, err = a.Ctx().CollectionFactory().PackageCollection().ByKey([]byte(ref)) if err != nil { if err == database.ErrNotFound { return nil, fmt.Errorf("package %s: %s", ref, err) } else { return nil, err } } err = list.Add(p) if err != nil { return nil, err } } snapshot := deb.NewSnapshotFromRefList(snapshotName, sources, deb.NewPackageRefListFromPackageList(list), description) err = snapshotCollection.Add(snapshot) if err != nil { return nil, err } return snapshot, nil }
// POST /api/snapshots func apiSnapshotsCreate(c *gin.Context) { var ( err error snapshot *deb.Snapshot ) var b struct { Name string `binding:"required"` Description string SourceSnapshots []string PackageRefs []string } if !c.Bind(&b) { return } if b.Description == "" { if len(b.SourceSnapshots)+len(b.PackageRefs) == 0 { b.Description = "Created as empty" } } snapshotCollection := context.CollectionFactory().SnapshotCollection() snapshotCollection.Lock() defer snapshotCollection.Unlock() sources := make([]*deb.Snapshot, len(b.SourceSnapshots)) for i := range b.SourceSnapshots { sources[i], err = snapshotCollection.ByName(b.SourceSnapshots[i]) if err != nil { c.Fail(404, err) return } err = snapshotCollection.LoadComplete(sources[i]) if err != nil { c.Fail(500, err) return } } list := deb.NewPackageList() // verify package refs and build package list for _, ref := range b.PackageRefs { var p *deb.Package p, err = context.CollectionFactory().PackageCollection().ByKey([]byte(ref)) if err != nil { if err == database.ErrNotFound { c.Fail(404, fmt.Errorf("package %s: %s", ref, err)) } else { c.Fail(500, err) } return } err = list.Add(p) if err != nil { c.Fail(400, err) return } } snapshot = deb.NewSnapshotFromRefList(b.Name, sources, deb.NewPackageRefListFromPackageList(list), b.Description) err = snapshotCollection.Add(snapshot) if err != nil { c.Fail(400, err) return } c.JSON(201, snapshot) }