From c9309c926cd2562d813c21e741b624239ded0652 Mon Sep 17 00:00:00 2001 From: Christoph Fiehe Date: Wed, 23 Oct 2024 15:26:31 +0200 Subject: [PATCH] Command to replace the whole staged source list added. Signed-off-by: Christoph Fiehe --- cmd/publish.go | 1 + cmd/publish_source_add.go | 2 +- cmd/publish_source_replace.go | 88 +++++++++++++++++++ completion.d/aptly | 2 +- .../PublishSourceReplace1Test_gold | 5 ++ system/t06_publish/source.py | 16 ++++ 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 cmd/publish_source_replace.go create mode 100644 system/t06_publish/PublishSourceReplace1Test_gold diff --git a/cmd/publish.go b/cmd/publish.go index 887929d6..4217ff87 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -52,6 +52,7 @@ func makeCmdPublishSource() *commander.Command { makeCmdPublishSourceDrop(), makeCmdPublishSourceList(), makeCmdPublishSourceRemove(), + makeCmdPublishSourceReplace(), makeCmdPublishSourceUpdate(), }, } diff --git a/cmd/publish_source_add.go b/cmd/publish_source_add.go index a5633460..4c1e9989 100644 --- a/cmd/publish_source_add.go +++ b/cmd/publish_source_add.go @@ -48,7 +48,7 @@ func aptlyPublishSourceAdd(cmd *commander.Command, args []string) error { } context.Progress().Printf("Adding component '%s' with source '%s' [%s]...\n", component, name, published.SourceKind) - sources[component] = names[i] + sources[component] = name } err = collectionFactory.PublishedRepoCollection().Update(published) diff --git a/cmd/publish_source_replace.go b/cmd/publish_source_replace.go new file mode 100644 index 00000000..10d5b588 --- /dev/null +++ b/cmd/publish_source_replace.go @@ -0,0 +1,88 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/aptly-dev/aptly/deb" + "github.com/smira/commander" + "github.com/smira/flag" +) + +func aptlyPublishSourceReplace(cmd *commander.Command, args []string) error { + if len(args) < 2 { + cmd.Usage() + return commander.ErrCommandError + } + + distribution := args[0] + names := args[1:] + components := strings.Split(context.Flags().Lookup("component").Value.String(), ",") + + if len(names) != len(components) { + return fmt.Errorf("mismatch in number of components (%d) and sources (%d)", len(components), len(names)) + } + + prefix := context.Flags().Lookup("prefix").Value.String() + storage, prefix := deb.ParsePrefix(prefix) + + collectionFactory := context.NewCollectionFactory() + published, err := collectionFactory.PublishedRepoCollection().ByStoragePrefixDistribution(storage, prefix, distribution) + if err != nil { + return fmt.Errorf("unable to add: %s", err) + } + + err = collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory) + if err != nil { + return fmt.Errorf("unable to add: %s", err) + } + + revision := published.ObtainRevision() + sources := revision.Sources + context.Progress().Printf("Clearing staged source list...\n") + clear(sources) + + for i, component := range components { + name := names[i] + context.Progress().Printf("Adding component '%s' with source '%s' [%s]...\n", component, name, published.SourceKind) + + sources[component] = name + } + + err = collectionFactory.PublishedRepoCollection().Update(published) + if err != nil { + return fmt.Errorf("unable to save to DB: %s", err) + } + + context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n", + distribution, published.StoragePrefix()) + + return err +} + +func makeCmdPublishSourceReplace() *commander.Command { + cmd := &commander.Command{ + Run: aptlyPublishSourceReplace, + UsageLine: "replace ", + Short: "replace staged source list of published repository", + Long: ` +The command replaces the staged source list of the published repository. + +The flag -component is mandatory. Use a comma-separated list of components, if +multiple components should be modified. The number of given components must be +equal to the number of given sources, e.g.: + + aptly publish source replace -component=main,contrib wheezy wheezy-main wheezy-contrib + +Example: + + $ aptly publish source replace -component=contrib wheezy ppa wheezy-contrib + +`, + Flag: *flag.NewFlagSet("aptly-publish-source-add", flag.ExitOnError), + } + cmd.Flag.String("prefix", ".", "publishing prefix in the form of [:]") + cmd.Flag.String("component", "", "component names to add (for multi-component publishing, separate components with commas)") + + return cmd +} diff --git a/completion.d/aptly b/completion.d/aptly index b7966bc2..3cb54ac5 100644 --- a/completion.d/aptly +++ b/completion.d/aptly @@ -61,7 +61,7 @@ _aptly() db_subcommands="cleanup recover" mirror_subcommands="create drop edit show list rename search update" publish_subcommands="drop list repo snapshot switch update source" - publish_source_subcommands="drop list add remove update" + publish_source_subcommands="drop list add remove update replace" snapshot_subcommands="create diff drop filter list merge pull rename search show verify" repo_subcommands="add copy create drop edit import include list move remove rename search show" package_subcommands="search show" diff --git a/system/t06_publish/PublishSourceReplace1Test_gold b/system/t06_publish/PublishSourceReplace1Test_gold new file mode 100644 index 00000000..61d88254 --- /dev/null +++ b/system/t06_publish/PublishSourceReplace1Test_gold @@ -0,0 +1,5 @@ +Clearing staged source list... +Adding component 'main-new' with source 'snap2' [snapshot]... +Adding component 'test-new' with source 'snap3' [snapshot]... + +You can run 'aptly publish update maverick .' to update the content of the published repository. diff --git a/system/t06_publish/source.py b/system/t06_publish/source.py index 9f352e20..78c81c60 100644 --- a/system/t06_publish/source.py +++ b/system/t06_publish/source.py @@ -156,6 +156,22 @@ class PublishSourceUpdate3Test(BaseTest): gold_processor = BaseTest.expand_environ +class PublishSourceReplace1Test(BaseTest): + """ + publish source replace: Replace existing sources + """ + fixtureDB = True + fixturePool = True + fixtureCmds = [ + "aptly snapshot create snap1 from mirror gnuplot-maverick", + "aptly snapshot create snap2 empty", + "aptly snapshot create snap3 empty", + "aptly publish snapshot -keyring=${files}/aptly.pub -secret-keyring=${files}/aptly.sec -distribution=maverick -component=main,test snap1 snap2", + ] + runCmd = "aptly publish source replace -component=main-new,test-new maverick snap2 snap3" + gold_processor = BaseTest.expand_environ + + class PublishSourceRemove1Test(BaseTest): """ publish source remove: Remove single source