From 69b48de0af1e5799d3eb7eb7d3de96b3cc2e408a Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Sun, 19 Jan 2014 12:57:15 +0400 Subject: [PATCH] Command aptly snapshot merge. --- cmd_snapshot.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/cmd_snapshot.go b/cmd_snapshot.go index fd6862f0..fd4fac64 100644 --- a/cmd_snapshot.go +++ b/cmd_snapshot.go @@ -427,6 +427,54 @@ func aptlySnapshotDiff(cmd *commander.Command, args []string) error { return err } +func aptlySnapshotMerge(cmd *commander.Command, args []string) error { + var err error + if len(args) < 2 { + cmd.Usage() + return err + } + + snapshotCollection := debian.NewSnapshotCollection(context.database) + + sources := make([]*debian.Snapshot, len(args)-1) + + for i := 0; i < len(args)-1; i++ { + sources[i], err = snapshotCollection.ByName(args[i+1]) + if err != nil { + return fmt.Errorf("unable to load snapshot: %s", err) + } + + err = snapshotCollection.LoadComplete(sources[i]) + if err != nil { + return fmt.Errorf("unable to load snapshot: %s", err) + } + } + + result := sources[0].RefList() + + for i := 1; i < len(sources); i++ { + result = result.Merge(sources[i].RefList()) + } + + sourceDescription := make([]string, len(sources)) + for i, s := range sources { + sourceDescription[i] = fmt.Sprintf("'%s'", s.Name) + } + + // Create snapshot + destination := debian.NewSnapshotFromRefList(args[0], sources, result, + fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", "))) + + err = 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 makeCmdSnapshotCreate() *commander.Command { cmd := &commander.Command{ Run: aptlySnapshotCreate, @@ -524,6 +572,22 @@ Displays list of missing and new packages, difference in package versions betwee return cmd } +func makeCmdSnapshotMerge() *commander.Command { + cmd := &commander.Command{ + Run: aptlySnapshotMerge, + UsageLine: "merge [...]", + Short: "merges snapshots into one, replacing matching packages", + Long: ` +Merge merges several snapshots into one. Merge happens from left to right. Packages with the same +name-architecture pair are replaced during merge (package from latest snapshot on the list wins). +If specified with only one snapshot, merge copies source into destination. +`, + Flag: *flag.NewFlagSet("aptly-snapshot-merge", flag.ExitOnError), + } + + return cmd +} + func makeCmdSnapshot() *commander.Command { return &commander.Command{ UsageLine: "snapshot", @@ -535,6 +599,7 @@ func makeCmdSnapshot() *commander.Command { makeCmdSnapshotVerify(), makeCmdSnapshotPull(), makeCmdSnapshotDiff(), + makeCmdSnapshotMerge(), //makeCmdSnapshotDestroy(), }, Flag: *flag.NewFlagSet("aptly-snapshot", flag.ExitOnError),